mirror of
https://github.com/fumiama/water.git
synced 2026-06-11 05:30:26 +08:00
add retry handler
This commit is contained in:
@@ -284,13 +284,16 @@ func openTap(config Config) (ifce *Interface, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type wintunRWC struct {
|
type wintunRWC struct {
|
||||||
ad *wintun.Adapter
|
ad wintun.Adapter
|
||||||
s wintun.Session
|
s wintun.Session
|
||||||
readmu sync.Mutex
|
readwait windows.Handle
|
||||||
readbuf []byte
|
readmu sync.Mutex
|
||||||
|
readbuf []byte
|
||||||
|
isclosed bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *wintunRWC) Close() error {
|
func (w *wintunRWC) Close() error {
|
||||||
|
w.isclosed = true
|
||||||
w.s.End()
|
w.s.End()
|
||||||
return w.ad.Close()
|
return w.ad.Close()
|
||||||
}
|
}
|
||||||
@@ -329,6 +332,10 @@ func (w *wintunRWC) Read(b []byte) (int, error) {
|
|||||||
w.readbuf = nil
|
w.readbuf = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
RETRY:
|
||||||
|
if w.isclosed {
|
||||||
|
return 0, errors.New("wintun is closed")
|
||||||
|
}
|
||||||
packet, err := w.s.ReceivePacket()
|
packet, err := w.s.ReceivePacket()
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
@@ -339,7 +346,8 @@ func (w *wintunRWC) Read(b []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
w.s.ReleaseReceivePacket(packet)
|
w.s.ReleaseReceivePacket(packet)
|
||||||
case windows.ERROR_NO_MORE_ITEMS:
|
case windows.ERROR_NO_MORE_ITEMS:
|
||||||
return n, nil
|
windows.WaitForSingleObject(w.readwait, windows.INFINITE)
|
||||||
|
goto RETRY
|
||||||
}
|
}
|
||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
@@ -368,5 +376,5 @@ func openDev(config Config) (ifce *Interface, err error) {
|
|||||||
ad.Close()
|
ad.Close()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
return &Interface{ReadWriteCloser: &wintunRWC{s: s, ad: ad}, name: config.InterfaceName}, nil
|
return &Interface{ReadWriteCloser: &wintunRWC{s: s, ad: *ad, readwait: s.ReadWaitEvent()}, name: config.InterfaceName}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user