Skip to content

Commit

Permalink
fix: listener read frame bug
Browse files Browse the repository at this point in the history
  • Loading branch information
woorui committed Oct 29, 2023
1 parent e570fd5 commit c440e66
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
25 changes: 13 additions & 12 deletions pkg/listener/yquic/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"crypto/tls"
"errors"
"net"
"time"

"github.com/quic-go/quic-go"
"github.com/yomorun/yomo/core/frame"
Expand Down Expand Up @@ -80,18 +79,22 @@ func newFrameConn(
// If the Connection implemented by quic is closed, the quic ApplicationErrorCode is always 0x13.
const YomoCloseErrorCode = quic.ApplicationErrorCode(0x13)

// Context returns the context of the connection.
func (p *FrameConn) Context() context.Context {
return p.ctx

Check warning on line 84 in pkg/listener/yquic/listener.go

View check run for this annotation

Codecov / codecov/patch

pkg/listener/yquic/listener.go#L83-L84

Added lines #L83 - L84 were not covered by tests
}

// RemoteAddr returns the remote address of connection.
func (p *FrameConn) RemoteAddr() net.Addr {
return p.conn.RemoteAddr()

Check warning on line 89 in pkg/listener/yquic/listener.go

View check run for this annotation

Codecov / codecov/patch

pkg/listener/yquic/listener.go#L88-L89

Added lines #L88 - L89 were not covered by tests
}

// LocalAddr returns the local address of connection.
func (p *FrameConn) LocalAddr() net.Addr {
return p.conn.LocalAddr()

Check warning on line 94 in pkg/listener/yquic/listener.go

View check run for this annotation

Codecov / codecov/patch

pkg/listener/yquic/listener.go#L93-L94

Added lines #L93 - L94 were not covered by tests
}

// CloseWithError closes the connection.
func (p *FrameConn) CloseWithError(errString string) error {
select {
case <-p.ctx.Done():
Expand Down Expand Up @@ -122,7 +125,6 @@ func (p *FrameConn) framing() {
p.ctxCancel(convertErrorToConnectionClosed(err))
return
}

Check warning on line 127 in pkg/listener/yquic/listener.go

View check run for this annotation

Codecov / codecov/patch

pkg/listener/yquic/listener.go#L125-L127

Added lines #L125 - L127 were not covered by tests

p.frameCh <- f
}
}
Expand All @@ -137,30 +139,29 @@ func convertErrorToConnectionClosed(err error) error {
return err

Check warning on line 139 in pkg/listener/yquic/listener.go

View check run for this annotation

Codecov / codecov/patch

pkg/listener/yquic/listener.go#L139

Added line #L139 was not covered by tests
}

// ReadFrame reads a frame. it usually be called in a for-loop.
func (p *FrameConn) ReadFrame() (frame.Frame, error) {
select {
case <-time.After(time.Second):
return nil, errors.New("yomo: read frame timeout")
case <-p.ctx.Done():
return nil, context.Cause(p.ctx)
case <-p.frameCh:
return <-p.frameCh, nil
case ff := <-p.frameCh:
return ff, nil
}
}

// WriteFrame writes a frame to connection.
func (p *FrameConn) WriteFrame(f frame.Frame) error {
select {
case <-p.ctx.Done():
return context.Cause(p.ctx)
default:
}
b, err := p.codec.Encode(f)
if err != nil {
return err
}

Check warning on line 161 in pkg/listener/yquic/listener.go

View check run for this annotation

Codecov / codecov/patch

pkg/listener/yquic/listener.go#L160-L161

Added lines #L160 - L161 were not covered by tests

b, err := p.codec.Encode(f)
if err != nil {
return err
return p.prw.WritePacket(p.stream, f.Type(), b)
}

return p.prw.WritePacket(p.stream, f.Type(), b)
}

// Listener listens a net.PacketConn and accepts connections.
Expand Down
2 changes: 1 addition & 1 deletion pkg/listener/yquic/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestFrameConnection(t *testing.T) {
for {
f, err := fconn.ReadFrame()
if err != nil {
assert.Equal(t, &ErrConnClosed{"yomo: listener closed"}, err)
assert.Equal(t, &ErrConnClosed{CloseMessage}, err)
return
}
hf := f.(*frame.HandshakeFrame)
Expand Down

0 comments on commit c440e66

Please sign in to comment.