Skip to content

Commit

Permalink
add code
Browse files Browse the repository at this point in the history
  • Loading branch information
lysShub committed May 25, 2024
1 parent ae1cd70 commit 17a2d4b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ require (
require (
github.com/google/uuid v1.2.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.1.0
golang.zx2c4.com/wireguard/windows v0.5.3
)

Expand Down
10 changes: 1 addition & 9 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ func (d *Handle) Recv(ip []byte, addr *Address) (int, error) {
// notice: recvLen not work, use windows.GetOverlappedResult
// todo: support batch recv
func (d *Handle) RecvEx(ip []byte, addr *Address, recvLen *uint32, ol *windows.Overlapped) error {
err := d.recvEx(ip, addr, recvLen, ol)
if err != nil {
return handleError(err)
}
return nil
}

func (d *Handle) recvEx(ip []byte, addr *Address, recvLen *uint32, ol *windows.Overlapped) error {
var ipPtr uintptr
if len(ip) > 0 {
ipPtr = uintptr(unsafe.Pointer(unsafe.SliceData(ip)))
Expand All @@ -82,7 +74,7 @@ func (d *Handle) recvEx(ip []byte, addr *Address, recvLen *uint32, ol *windows.O
uintptr(unsafe.Pointer(ol)), // lpOverlapped
)
if r1 == 0 {
return e
return handleError(e)
}
return nil
}
Expand Down
26 changes: 13 additions & 13 deletions handle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func Test_Recv_Error(t *testing.T) {
defer d.Close()

_, err = d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrClosed{}))
require.True(t, errors.Is(err, ErrClosed{}), err)
})

t.Run("recv/close", func(t *testing.T) {
Expand All @@ -148,7 +148,7 @@ func Test_Recv_Error(t *testing.T) {
require.NoError(t, d.Close())
}()
_, err = d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrClosed{}))
require.True(t, errors.Is(err, ErrClosed{}), err)
}
})

Expand All @@ -163,9 +163,9 @@ func Test_Recv_Error(t *testing.T) {
require.NoError(t, d.Close())
}()
_, err = d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrClosed{}))
require.True(t, errors.Is(err, ErrClosed{}), err)

require.True(t, errors.Is(d.Close(), ErrClosed{}))
require.True(t, errors.Is(d.Close(), ErrClosed{}), err)
}
})

Expand All @@ -181,14 +181,14 @@ func Test_Recv_Error(t *testing.T) {
}()
{
_, err = d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrClosed{}))
require.True(t, errors.Is(err, ErrClosed{}), err)
}
{
require.True(t, errors.Is(d.Close(), ErrClosed{}))
require.True(t, errors.Is(d.Close(), ErrClosed{}), err)
}
{
_, err = d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrClosed{}))
require.True(t, errors.Is(err, ErrClosed{}), err)
}
}
})
Expand All @@ -200,7 +200,7 @@ func Test_Recv_Error(t *testing.T) {
defer d.Close()

n, err := d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrShutdown{}))
require.True(t, errors.Is(err, ErrShutdown{}), err)
require.Zero(t, n)
})

Expand All @@ -215,7 +215,7 @@ func Test_Recv_Error(t *testing.T) {
}()

n, err := d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrShutdown{}))
require.True(t, errors.Is(err, ErrShutdown{}), err)
require.Zero(t, n)
})

Expand All @@ -230,7 +230,7 @@ func Test_Recv_Error(t *testing.T) {
}()

n, err := d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrShutdown{}))
require.True(t, errors.Is(err, ErrShutdown{}), err)
require.Zero(t, n)

require.NoError(t, d.Shutdown(Both))
Expand All @@ -248,15 +248,15 @@ func Test_Recv_Error(t *testing.T) {

{
n, err := d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrShutdown{}))
require.True(t, errors.Is(err, ErrShutdown{}), err)
require.Zero(t, n)
}
{
require.NoError(t, d.Shutdown(Both))
}
{
n, err := d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrShutdown{}))
require.True(t, errors.Is(err, ErrShutdown{}), err)
require.Zero(t, n)
}
})
Expand All @@ -270,7 +270,7 @@ func Test_Recv_Error(t *testing.T) {
require.NoError(t, err)

n, err := d.Recv(make([]byte, 1536), nil)
require.True(t, errors.Is(err, ErrClosed{}))
require.True(t, errors.Is(err, ErrClosed{}), err)
require.Zero(t, n)
})
}
Expand Down

0 comments on commit 17a2d4b

Please sign in to comment.