Skip to content

Commit

Permalink
Make Header.Version/Status uint16
Browse files Browse the repository at this point in the history
One byte is a little on the small side, better changing this now before people start using this.
  • Loading branch information
bep committed Aug 20, 2022
1 parent bed19df commit 3a67fc8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func StartClientRaw(opts ClientRawOptions) (*ClientRaw, error) {
// ClientRaw is a raw RPC client.
// Raw means that the client doesn't do any type conversion, a byte slice is what you get.
type ClientRaw struct {
version uint8
version uint16

conn conn

Expand Down Expand Up @@ -298,7 +298,7 @@ type ClientOptions[Q, R any] struct {
// ClientRawOptions are options for the raw part of the client.
type ClientRawOptions struct {
// Version number passed to the server.
Version uint8
Version uint16

// The server to start.
Cmd string
Expand Down
3 changes: 3 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ func TestExecTyped(t *testing.T) {
_, err = client.Execute(model.ExampleRequest{Text: "world"})
c.Assert(err, qt.IsNil)
c.Assert(len(logMessages), qt.Equals, 2)
c.Assert(string(logMessages[0].Body), qt.Equals, "first log message")
c.Assert(logMessages[0].Header.Status, qt.Equals, uint16(0))
c.Assert(logMessages[0].Header.Version, qt.Equals, uint16(32))
c.Assert(client.Close(), qt.IsNil)
})

Expand Down
6 changes: 4 additions & 2 deletions examples/servers/typed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,15 @@ func main() {
d.Send(
execrpc.Message{
Header: execrpc.Header{
Status: 150,
Version: 32,
Status: 150,
},
Body: []byte("first log message"),
},
execrpc.Message{
Header: execrpc.Header{
Status: 150,
Version: 32,
Status: 150,
},
Body: []byte("second log message"),
})
Expand Down
18 changes: 10 additions & 8 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,33 @@ func (m *Message) Write(w io.Writer) error {
// ID and Size are set by the system.
type Header struct {
ID uint32
Version uint8
Status uint8
Version uint16
Status uint16
Size uint32
}

const headerSize = 12

// Read reads the header from the reader.
func (h *Header) Read(r io.Reader) error {
buf := make([]byte, 10)
buf := make([]byte, headerSize)
_, err := io.ReadFull(r, buf)
if err != nil {
return err
}
h.ID = binary.BigEndian.Uint32(buf[0:4])
h.Version = buf[4]
h.Status = buf[5]
h.Version = binary.BigEndian.Uint16(buf[4:6])
h.Status = binary.BigEndian.Uint16(buf[6:8])
h.Size = binary.BigEndian.Uint32(buf[6:])
return nil
}

// Write writes the header to the writer.
func (h Header) Write(w io.Writer) error {
buff := make([]byte, 10)
buff := make([]byte, headerSize)
binary.BigEndian.PutUint32(buff[0:4], h.ID)
buff[4] = h.Version
buff[5] = h.Status
binary.BigEndian.PutUint16(buff[4:6], h.Version)
binary.BigEndian.PutUint16(buff[6:8], h.Status)
binary.BigEndian.PutUint32(buff[6:], h.Size)
_, err := w.Write(buff)
return err
Expand Down

0 comments on commit 3a67fc8

Please sign in to comment.