MCPcopy
hub / github.com/lonng/nano / Encode

Function Encode

internal/codec/codec.go:114–127  ·  view source on GitHub ↗

Encode create a packet.Packet from the raw bytes slice and then encode to network bytes slice Protocol refs: https://github.com/NetEase/pomelo/wiki/Communication-Protocol - -|-------- --------|- - --------|------------------------|-------- 1 byte packet type, 3 bytes packet data l

(typ packet.Type, data []byte)

Source from the content-addressed store, hash-verified

112// --------|------------------------|--------
113// 1 byte packet type, 3 bytes packet data length(big end), and data segment
114func Encode(typ packet.Type, data []byte) ([]byte, error) {
115 if typ < packet.Handshake || typ > packet.Kick {
116 return nil, packet.ErrWrongPacketType
117 }
118
119 p := &packet.Packet{Type: typ, Length: len(data)}
120 buf := make([]byte, p.Length+HeadLength)
121 buf[0] = byte(p.Type)
122
123 copy(buf[1:HeadLength], intToBytes(p.Length))
124 copy(buf[HeadLength:], data)
125
126 return buf, nil
127}
128
129// Decode packet data length byte to int(Big end)
130func bytesToInt(b []byte) int {

Callers 6

initFunction · 0.92
sendMessageMethod · 0.92
cacheFunction · 0.92
writeMethod · 0.92
TestPackFunction · 0.70
BenchmarkDecoder_DecodeFunction · 0.70

Calls 1

intToBytesFunction · 0.85

Tested by 2

TestPackFunction · 0.56
BenchmarkDecoder_DecodeFunction · 0.56

Used in the wild real call sites across dependent graphs

searching dependent graphs…