Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Redesign flexible buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bzEq committed Dec 15, 2023
1 parent 6e38cd1 commit 7c4fcd9
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions core/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ func (self *NetPort) Pack(b *iovec.IoVec) error {
}

type RawNetPort struct {
C net.Conn
timeout time.Duration
C net.Conn
timeout time.Duration
lastRead int
buf []byte
}

func (self *RawNetPort) Pack(b *iovec.IoVec) error {
Expand All @@ -63,12 +65,23 @@ func (self *RawNetPort) Unpack(b *iovec.IoVec) error {
if err := self.C.SetReadDeadline(time.Now().Add(self.timeout)); err != nil {
return err
}
buf := make([]byte, DEFAULT_BUFFER_SIZE)
nr, err := self.C.Read(buf)
if len(self.buf) <= self.lastRead {
l := self.lastRead
if l < DEFAULT_UDP_BUFFER_SIZE || l < DEFAULT_BUFFER_SIZE {
l = DEFAULT_BUFFER_SIZE
} else {
l = self.lastRead * 2
}
self.buf = make([]byte, l)
}
nr, err := self.C.Read(self.buf)
if err != nil {
self.lastRead = 0
return err
}
b.Take(buf[:nr])
b.Take(self.buf[:nr])
self.buf = self.buf[nr:]
self.lastRead = nr
return nil
}

Expand Down

0 comments on commit 7c4fcd9

Please sign in to comment.