Skip to content

Commit

Permalink
feature: Ping/Pong stream flags
Browse files Browse the repository at this point in the history
Signed-off-by: Valery Piashchynski <piashchynski.valery@gmail.com>
  • Loading branch information
rustatian committed Aug 22, 2023
1 parent 014c308 commit fd36dce
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 5,237 deletions.
24 changes: 22 additions & 2 deletions pkg/frame/frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const WORD = 4
type Frame struct {
// Payload, max length 4.2GB.
payload []byte

// Header
header []byte
}
Expand Down Expand Up @@ -128,10 +127,31 @@ func (*Frame) SetStreamFlag(header []byte) {

func (*Frame) IsStream(header []byte) bool {
_ = header[11]
return header[10]&STREAM == 1
return header[10]&STREAM != 0
}

func (*Frame) IsPing(header []byte) bool {
_ = header[11]
return header[10]&PING != 0
}

func (*Frame) SetPingBit(header []byte) {
_ = header[11]
header[10] |= PING
}

func (*Frame) IsPong(header []byte) bool {
_ = header[11]
return header[10]&PONG != 0
}

func (*Frame) SetPongBit(header []byte) {
_ = header[11]
header[10] |= PONG
}

func (*Frame) SetStopBit(header []byte) {
_ = header[11]
header[10] |= STOP
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/frame/frame_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ const (
Version1 byte = 0x01

/*
10th byte
10th byte, stream
*/

// STREAM bit
STREAM byte = 0x01
// STOP command
STOP byte = 0x02
// PING command
PING byte = 0x04
// PONG command
PONG byte = 0x08
)
75 changes: 75 additions & 0 deletions pkg/frame/frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,81 @@ func TestFrame_Bytes(t *testing.T) {
assert.Equal(t, []uint32{323423432}, rf.ReadOptions(rf.Header()))
}

func TestFrame_NotPingPong(t *testing.T) {
nf := NewFrame()
nf.WriteVersion(nf.Header(), 1)
nf.WriteFlags(nf.Header(), CONTROL, CodecGob)
nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload))))

nf.WriteOptions(nf.HeaderPtr(), 323423432)
assert.Equal(t, []uint32{323423432}, nf.ReadOptions(nf.Header()))
nf.WritePayload([]byte(TestPayload))

nf.WriteCRC(nf.Header())
assert.Equal(t, true, nf.VerifyCRC(nf.Header()))
data := nf.Bytes()

rf := ReadFrame(data)

assert.False(t, rf.IsPing(rf.Header()))
assert.False(t, rf.IsPong(rf.Header()))
assert.Equal(t, rf.ReadVersion(rf.Header()), nf.ReadVersion(nf.Header()))
assert.Equal(t, rf.ReadFlags(), nf.ReadFlags())
assert.Equal(t, rf.ReadPayloadLen(rf.Header()), nf.ReadPayloadLen(nf.Header()))
assert.Equal(t, true, rf.VerifyCRC(rf.Header()))
assert.Equal(t, []uint32{323423432}, rf.ReadOptions(rf.Header()))
}

func TestFrame_Ping(t *testing.T) {
nf := NewFrame()
nf.WriteVersion(nf.Header(), 1)
nf.WriteFlags(nf.Header(), CONTROL, CodecGob)
nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload))))

nf.WriteOptions(nf.HeaderPtr(), 323423432)
assert.Equal(t, []uint32{323423432}, nf.ReadOptions(nf.Header()))
nf.WritePayload([]byte(TestPayload))
nf.SetPingBit(nf.Header())

nf.WriteCRC(nf.Header())
assert.Equal(t, true, nf.VerifyCRC(nf.Header()))
data := nf.Bytes()

rf := ReadFrame(data)

assert.True(t, rf.IsPing(rf.Header()))
assert.Equal(t, rf.ReadVersion(rf.Header()), nf.ReadVersion(nf.Header()))
assert.Equal(t, rf.ReadFlags(), nf.ReadFlags())
assert.Equal(t, rf.ReadPayloadLen(rf.Header()), nf.ReadPayloadLen(nf.Header()))
assert.Equal(t, true, rf.VerifyCRC(rf.Header()))
assert.Equal(t, []uint32{323423432}, rf.ReadOptions(rf.Header()))
}

func TestFrame_Pong(t *testing.T) {
nf := NewFrame()
nf.WriteVersion(nf.Header(), 1)
nf.WriteFlags(nf.Header(), CONTROL, CodecGob)
nf.WritePayloadLen(nf.Header(), uint32(len([]byte(TestPayload))))

nf.WriteOptions(nf.HeaderPtr(), 323423432)
assert.Equal(t, []uint32{323423432}, nf.ReadOptions(nf.Header()))
nf.WritePayload([]byte(TestPayload))
nf.SetPongBit(nf.Header())

nf.WriteCRC(nf.Header())
assert.Equal(t, true, nf.VerifyCRC(nf.Header()))
data := nf.Bytes()

rf := ReadFrame(data)

assert.True(t, rf.IsPong(rf.Header()))
assert.Equal(t, rf.ReadVersion(rf.Header()), nf.ReadVersion(nf.Header()))
assert.Equal(t, rf.ReadFlags(), nf.ReadFlags())
assert.Equal(t, rf.ReadPayloadLen(rf.Header()), nf.ReadPayloadLen(nf.Header()))
assert.Equal(t, true, rf.VerifyCRC(rf.Header()))
assert.Equal(t, []uint32{323423432}, rf.ReadOptions(rf.Header()))
}

func BenchmarkCRC32(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
Expand Down
14 changes: 0 additions & 14 deletions pkg/shared_memory/interface.go

This file was deleted.

188 changes: 0 additions & 188 deletions pkg/shared_memory/posix/posix_shm.go

This file was deleted.

Loading

0 comments on commit fd36dce

Please sign in to comment.