Skip to content

Commit

Permalink
Return bytes written to buffer instead of conn
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevop committed Nov 23, 2023
1 parent 357a1e6 commit e178015
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ type Client struct {

// ClientConnection contains the connection transport and metadata for the client.
type ClientConnection struct {
Conn net.Conn // the net.Conn used to establish the connection
bconn *bufio.ReadWriter // a buffered net.Conn for reading packets
outbuf *bytes.Buffer // a buffer for writing packets
Remote string // the remote address of the client
Listener string // listener id of the client
Inline bool // if true, the client is the built-in 'inline' embedded client
Conn net.Conn // the net.Conn used to establish the connection
bconn *bufio.Reader // a buffered net.Conn for reading packets
outbuf *bytes.Buffer // a buffer for writing packets
Remote string // the remote address of the client
Listener string // listener id of the client
Inline bool // if true, the client is the built-in 'inline' embedded client
}

// ClientProperties contains the properties which define the client behaviour.
Expand Down Expand Up @@ -181,11 +181,8 @@ func newClient(c net.Conn, o *ops) *Client {

if c != nil {
cl.Net = ClientConnection{
Conn: c,
bconn: bufio.NewReadWriter(
bufio.NewReaderSize(c, o.options.ClientNetReadBufferSize),
bufio.NewWriterSize(c, o.options.ClientNetWriteBufferSize),
),
Conn: c,
bconn: bufio.NewReaderSize(c, o.options.ClientNetReadBufferSize),
Remote: c.RemoteAddr().String(),
}
}
Expand Down Expand Up @@ -594,34 +591,27 @@ func (cl *Client) WritePacket(pk packets.Packet) error {
}

// first write to buffer, then flush buffer
buf.WriteTo(cl.Net.outbuf) // will always be successful
n, err = cl.Net.outbuf.WriteTo(cl.Net.Conn)
if err != nil {
return
}
cl.Net.outbuf = nil
n, _ = buf.WriteTo(cl.Net.outbuf) // will always be successful
err = cl.flushOutbuf()
return
}

// there are more writes in the queue
if cl.Net.outbuf == nil {
if buf.Len() < cl.ops.options.ClientNetWriteBufferSize {
if l := buf.Len(); l < cl.ops.options.ClientNetWriteBufferSize {
cl.Net.outbuf = buf
return 0, nil
return int64(l), nil
}

return buf.WriteTo(cl.Net.Conn)
}

buf.WriteTo(cl.Net.outbuf) // will always be successful
n, _ = buf.WriteTo(cl.Net.outbuf) // will always be successful
if cl.Net.outbuf.Len() < cl.ops.options.ClientNetWriteBufferSize {
return 0, nil
}

n, err = cl.Net.outbuf.WriteTo(cl.Net.Conn)
if err != nil {
return
}
cl.Net.outbuf = nil

err = cl.flushOutbuf()
return
}()
if err != nil {
Expand All @@ -638,3 +628,15 @@ func (cl *Client) WritePacket(pk packets.Packet) error {

return err
}

func (cl *Client) flushOutbuf() (err error) {
if cl.Net.outbuf == nil {
return
}

_, err = cl.Net.outbuf.WriteTo(cl.Net.Conn)
if err == nil {
cl.Net.outbuf = nil
}
return
}

0 comments on commit e178015

Please sign in to comment.