Skip to content

Commit

Permalink
Fix Read not implemented for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
nekohasekai committed Dec 12, 2024
1 parent 618d3f9 commit 3d24a0d
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion tun_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 3d24a0d

Please sign in to comment.