Skip to content

Commit

Permalink
increase max packet size to 16mb
Browse files Browse the repository at this point in the history
  • Loading branch information
felipejfc committed Apr 23, 2019
1 parent f3bc827 commit aa452e1
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
2 changes: 1 addition & 1 deletion conn/codec/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import "errors"
// Codec constants.
const (
HeadLength = 4
MaxPacketSize = 64 * 1024
MaxPacketSize = 1 << 24 //16MB
)

// ErrPacketSizeExcced is the error used for encode/decode.
Expand Down
6 changes: 0 additions & 6 deletions conn/codec/pomelo_packet_decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ var forwardTables = map[string]struct {
"test_kick_type": {[]byte{packet.Kick}, nil},

"test_wrong_packet_type": {[]byte{0x06}, packet.ErrWrongPomeloPacketType},

"test_max_packet_size_handshake_type": {[]byte{packet.Handshake, 0xFF, 0xFF, 0xFF}, ErrPacketSizeExcced},
"test_max_packet_size_handshake_ack_type": {[]byte{packet.HandshakeAck, 0xFF, 0xFF, 0xFF}, ErrPacketSizeExcced},
"test_max_packet_size_heartbeat_type": {[]byte{packet.Heartbeat, 0xFF, 0xFF, 0xFF}, ErrPacketSizeExcced},
"test_max_packet_size_data_type": {[]byte{packet.Data, 0xFF, 0xFF, 0xFF}, ErrPacketSizeExcced},
"test_max_packet_size_kick_type": {[]byte{packet.Kick, 0xFF, 0xFF, 0xFF}, ErrPacketSizeExcced},
}

var (
Expand Down
4 changes: 4 additions & 0 deletions conn/codec/pomelo_packet_encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func (e *PomeloPacketEncoder) Encode(typ packet.Type, data []byte) ([]byte, erro
return nil, packet.ErrWrongPomeloPacketType
}

if len(data) > MaxPacketSize {
return nil, ErrPacketSizeExcced
}

p := &packet.Packet{Type: typ, Length: len(data)}
buf := make([]byte, p.Length+HeadLength)
buf[0] = byte(p.Type)
Expand Down
13 changes: 9 additions & 4 deletions conn/codec/pomelo_packet_encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func helperConcatBytes(packetType packet.Type, length, data []byte) []byte {
return bytes
}

var tooBigData = make([]byte, 1<<25)

var encodeTables = map[string]struct {
packetType packet.Type
length []byte
Expand All @@ -27,6 +29,7 @@ var encodeTables = map[string]struct {
}{
"test_encode_handshake": {packet.Handshake, []byte{0x00, 0x00, 0x02}, []byte{0x01, 0x00}, nil},
"test_invalid_packet_type": {0xff, nil, nil, packet.ErrWrongPomeloPacketType},
"test_too_big_packet": {packet.Data, nil, tooBigData, ErrPacketSizeExcced},
}

func TestEncode(t *testing.T) {
Expand All @@ -37,10 +40,12 @@ func TestEncode(t *testing.T) {
ppe := NewPomeloPacketEncoder()

encoded, err := ppe.Encode(table.packetType, table.data)

expectedEncoded := helperConcatBytes(table.packetType, table.length, table.data)
assert.Equal(t, expectedEncoded, encoded)
assert.Equal(t, table.err, err)
if table.err != nil {
assert.Equal(t, table.err, err)
} else {
expectedEncoded := helperConcatBytes(table.packetType, table.length, table.data)
assert.Equal(t, expectedEncoded, encoded)
}
})
}
}

0 comments on commit aa452e1

Please sign in to comment.