Skip to content

Commit

Permalink
WSStreamConn impl the LocalAddr func
Browse files Browse the repository at this point in the history
  • Loading branch information
rkonfj committed Apr 21, 2023
1 parent 6feaea8 commit 3508bbf
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 4 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ func (c *NhooyrWSConn) Write(ctx context.Context, p []byte) error {
return c.Conn.Write(ctx, websocket.MessageBinary, p)
}

func (c *NhooyrWSConn) LocalAddr() net.Addr {
return nil
}

func (c *NhooyrWSConn) Close(code int, reason string) error {
return c.Conn.Close(websocket.StatusCode(code), reason)
}
25 changes: 13 additions & 12 deletions spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type TohClient interface {
type WSConn interface {
Read(ctx context.Context) ([]byte, error)
Write(ctx context.Context, p []byte) error
LocalAddr() net.Addr
Close(code int, reason string) error
}

Expand All @@ -42,17 +43,6 @@ func NewWSStreamConn(wsConn WSConn, addr net.Addr) *WSStreamConn {
// Read can be made to time out and return an error after a fixed
// time limit; see SetDeadline and SetReadDeadline.
func (c *WSStreamConn) Read(b []byte) (n int, err error) {
ctx := context.Background()
if c.readDeadline != nil {
_ctx, cancel := context.WithDeadline(context.Background(), *c.readDeadline)
ctx = _ctx
defer cancel()
} else if c.deadline != nil {
_ctx, cancel := context.WithDeadline(context.Background(), *c.deadline)
ctx = _ctx
defer cancel()
}

if len(c.buf) > 0 {
if len(c.buf) <= len(b) {
n := copy(b, c.buf)
Expand All @@ -64,6 +54,17 @@ func (c *WSStreamConn) Read(b []byte) (n int, err error) {
return len(b), nil
}

ctx := context.Background()
if c.readDeadline != nil {
_ctx, cancel := context.WithDeadline(context.Background(), *c.readDeadline)
ctx = _ctx
defer cancel()
} else if c.deadline != nil {
_ctx, cancel := context.WithDeadline(context.Background(), *c.deadline)
ctx = _ctx
defer cancel()
}

wsb, err := c.wsConn.Read(ctx)
if err != nil {
if strings.Contains(err.Error(), "StatusBadGateway") ||
Expand Down Expand Up @@ -110,7 +111,7 @@ func (c *WSStreamConn) Close() error {

// LocalAddr returns the local network address, if known.
func (c *WSStreamConn) LocalAddr() net.Addr {
return nil
return c.wsConn.LocalAddr()
}

// RemoteAddr returns the remote network address, if known.
Expand Down

0 comments on commit 3508bbf

Please sign in to comment.