Skip to content

Commit 3a67fc8

Browse files
committed
Make Header.Version/Status uint16
One byte is a little on the small side, better changing this now before people start using this.
1 parent bed19df commit 3a67fc8

File tree

4 files changed

+19
-12
lines changed

4 files changed

+19
-12
lines changed

client.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func StartClientRaw(opts ClientRawOptions) (*ClientRaw, error) {
131131
// ClientRaw is a raw RPC client.
132132
// Raw means that the client doesn't do any type conversion, a byte slice is what you get.
133133
type ClientRaw struct {
134-
version uint8
134+
version uint16
135135

136136
conn conn
137137

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

303303
// The server to start.
304304
Cmd string

client_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ func TestExecTyped(t *testing.T) {
143143
_, err = client.Execute(model.ExampleRequest{Text: "world"})
144144
c.Assert(err, qt.IsNil)
145145
c.Assert(len(logMessages), qt.Equals, 2)
146+
c.Assert(string(logMessages[0].Body), qt.Equals, "first log message")
147+
c.Assert(logMessages[0].Header.Status, qt.Equals, uint16(0))
148+
c.Assert(logMessages[0].Header.Version, qt.Equals, uint16(32))
146149
c.Assert(client.Close(), qt.IsNil)
147150
})
148151

examples/servers/typed/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,15 @@ func main() {
4242
d.Send(
4343
execrpc.Message{
4444
Header: execrpc.Header{
45-
Status: 150,
45+
Version: 32,
46+
Status: 150,
4647
},
4748
Body: []byte("first log message"),
4849
},
4950
execrpc.Message{
5051
Header: execrpc.Header{
51-
Status: 150,
52+
Version: 32,
53+
Status: 150,
5254
},
5355
Body: []byte("second log message"),
5456
})

message.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,33 @@ func (m *Message) Write(w io.Writer) error {
3333
// ID and Size are set by the system.
3434
type Header struct {
3535
ID uint32
36-
Version uint8
37-
Status uint8
36+
Version uint16
37+
Status uint16
3838
Size uint32
3939
}
4040

41+
const headerSize = 12
42+
4143
// Read reads the header from the reader.
4244
func (h *Header) Read(r io.Reader) error {
43-
buf := make([]byte, 10)
45+
buf := make([]byte, headerSize)
4446
_, err := io.ReadFull(r, buf)
4547
if err != nil {
4648
return err
4749
}
4850
h.ID = binary.BigEndian.Uint32(buf[0:4])
49-
h.Version = buf[4]
50-
h.Status = buf[5]
51+
h.Version = binary.BigEndian.Uint16(buf[4:6])
52+
h.Status = binary.BigEndian.Uint16(buf[6:8])
5153
h.Size = binary.BigEndian.Uint32(buf[6:])
5254
return nil
5355
}
5456

5557
// Write writes the header to the writer.
5658
func (h Header) Write(w io.Writer) error {
57-
buff := make([]byte, 10)
59+
buff := make([]byte, headerSize)
5860
binary.BigEndian.PutUint32(buff[0:4], h.ID)
59-
buff[4] = h.Version
60-
buff[5] = h.Status
61+
binary.BigEndian.PutUint16(buff[4:6], h.Version)
62+
binary.BigEndian.PutUint16(buff[6:8], h.Status)
6163
binary.BigEndian.PutUint32(buff[6:], h.Size)
6264
_, err := w.Write(buff)
6365
return err

0 commit comments

Comments
 (0)