Skip to content

Commit

Permalink
No longer request a 2-byte buffer in advance for udp conn
Browse files Browse the repository at this point in the history
  • Loading branch information
mohanson committed Oct 2, 2024
1 parent af079d1 commit 2b9be05
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions protocol/ashe/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,11 @@ func NewTCPConn(c io.ReadWriteCloser) *TCPConn {
// UDPConn is an implementation of the Conn interface for udp network connections.
type UDPConn struct {
io.ReadWriteCloser
b []byte
}

// NewUDPConn returns a new UDPConn.
func NewUDPConn(c io.ReadWriteCloser) *UDPConn {
return &UDPConn{ReadWriteCloser: c, b: make([]byte, 2)}
return &UDPConn{ReadWriteCloser: c}
}

// Read reads up to len(p) bytes into p.
Expand All @@ -89,8 +88,9 @@ func (c *UDPConn) Write(p []byte) (int, error) {
// Maximum udp payload size is 65527(equal to 65535 - 8) bytes in theoretically. The 8 in the formula means the udp
// header, which contains source port, destination port, length and checksum.
doa.Doa(len(p) <= 65527)
binary.BigEndian.PutUint16(c.b, uint16(len(p)))
_, err := c.ReadWriteCloser.Write(c.b[:2])
b := make([]byte, 2)
binary.BigEndian.PutUint16(b, uint16(len(p)))
_, err := c.ReadWriteCloser.Write(b)
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 2b9be05

Please sign in to comment.