mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-09-02 00:11:56 +03:00
Correctly manage partial writes
This commit is contained in:
@@ -10,11 +10,9 @@ type Conn struct {
|
||||
|
||||
Encryptor cipher.Stream
|
||||
Decryptor cipher.Stream
|
||||
|
||||
writeBuf []byte
|
||||
}
|
||||
|
||||
func (c *Conn) Read(p []byte) (int, error) {
|
||||
func (c Conn) Read(p []byte) (int, error) {
|
||||
n, err := c.Conn.Read(p)
|
||||
if err != nil {
|
||||
return n, err // nolint: wrapcheck
|
||||
@@ -25,9 +23,16 @@ func (c *Conn) Read(p []byte) (int, error) {
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (c *Conn) Write(p []byte) (int, error) {
|
||||
c.writeBuf = append(c.writeBuf[:0], p...)
|
||||
c.Encryptor.XORKeyStream(c.writeBuf, p)
|
||||
func (c Conn) Write(p []byte) (int, error) {
|
||||
buf := acquireBytesBuffer()
|
||||
defer releaseBytesBuffer(buf)
|
||||
|
||||
return c.Conn.Write(c.writeBuf)
|
||||
buf.Write(p)
|
||||
|
||||
payload := buf.Bytes()
|
||||
c.Encryptor.XORKeyStream(payload, payload)
|
||||
|
||||
n, err := buf.WriteTo(c.Conn)
|
||||
|
||||
return int(n), err // nolint: wrapcheck
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user