Skip to content

Commit f3967a9

Browse files
committed
Fix Read not implemented for windows
1 parent 618d3f9 commit f3967a9

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

tun_windows.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,40 @@ func (t *NativeTun) Start() error {
353353
}
354354

355355
func (t *NativeTun) Read(p []byte) (n int, err error) {
356-
return 0, os.ErrInvalid
356+
t.running.Add(1)
357+
defer t.running.Done()
358+
retry:
359+
if t.close.Load() == 1 {
360+
return 0, os.ErrClosed
361+
}
362+
start := nanotime()
363+
shouldSpin := t.rate.current.Load() >= spinloopRateThreshold && uint64(start-t.rate.nextStartTime.Load()) <= rateMeasurementGranularity*2
364+
for {
365+
if t.close.Load() == 1 {
366+
return 0, os.ErrClosed
367+
}
368+
var packet []byte
369+
packet, err = t.session.ReceivePacket()
370+
switch err {
371+
case nil:
372+
n = copy(p, packet)
373+
t.session.ReleaseReceivePacket(packet)
374+
t.rate.update(uint64(n))
375+
return
376+
case windows.ERROR_NO_MORE_ITEMS:
377+
if !shouldSpin || uint64(nanotime()-start) >= spinloopDuration {
378+
windows.WaitForSingleObject(t.readWait, windows.INFINITE)
379+
goto retry
380+
}
381+
procyield(1)
382+
continue
383+
case windows.ERROR_HANDLE_EOF:
384+
return 0, os.ErrClosed
385+
case windows.ERROR_INVALID_DATA:
386+
return 0, errors.New("send ring corrupt")
387+
}
388+
return 0, fmt.Errorf("read failed: %w", err)
389+
}
357390
}
358391

359392
func (t *NativeTun) ReadPacket() ([]byte, func(), error) {

0 commit comments

Comments
 (0)