mirror of
https://github.com/SagerNet/sing-tun.git
synced 2025-04-02 03:17:38 +03:00
Fix Read not implemented for windows
This commit is contained in:
parent
618d3f9a52
commit
d38f9adaef
1 changed files with 34 additions and 1 deletions
|
@ -353,7 +353,40 @@ func (t *NativeTun) Start() error {
|
|||
}
|
||||
|
||||
func (t *NativeTun) Read(p []byte) (n int, err error) {
|
||||
return 0, os.ErrInvalid
|
||||
t.running.Add(1)
|
||||
defer t.running.Done()
|
||||
retry:
|
||||
if t.close.Load() == 1 {
|
||||
return 0, os.ErrClosed
|
||||
}
|
||||
start := nanotime()
|
||||
shouldSpin := t.rate.current.Load() >= spinloopRateThreshold && uint64(start-t.rate.nextStartTime.Load()) <= rateMeasurementGranularity*2
|
||||
for {
|
||||
if t.close.Load() == 1 {
|
||||
return 0, os.ErrClosed
|
||||
}
|
||||
var packet []byte
|
||||
packet, err = t.session.ReceivePacket()
|
||||
switch err {
|
||||
case nil:
|
||||
n = copy(p, packet)
|
||||
t.session.ReleaseReceivePacket(packet)
|
||||
t.rate.update(uint64(n))
|
||||
return
|
||||
case windows.ERROR_NO_MORE_ITEMS:
|
||||
if !shouldSpin || uint64(nanotime()-start) >= spinloopDuration {
|
||||
windows.WaitForSingleObject(t.readWait, windows.INFINITE)
|
||||
goto retry
|
||||
}
|
||||
procyield(1)
|
||||
continue
|
||||
case windows.ERROR_HANDLE_EOF:
|
||||
return 0, os.ErrClosed
|
||||
case windows.ERROR_INVALID_DATA:
|
||||
return 0, errors.New("send ring corrupt")
|
||||
}
|
||||
return 0, fmt.Errorf("read failed: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *NativeTun) ReadPacket() ([]byte, func(), error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue