Fix Read not implemented for windows

This commit is contained in:
世界 2024-12-12 20:56:58 +08:00
parent 618d3f9a52
commit d38f9adaef
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -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) {