package example import ( "io" "zks.com/gotcp" ) // Packet: pacLen + pacType + pacData // Big endian: int32 + int32 + []byte type LtvPacketDelegate struct { pacLen uint32 pacType uint32 pacData []byte } func (p *LtvPacketDelegate) Serialize() []byte { buf := make([]byte, 8+len(p.pacData)) copy(buf[0:4], gotcp.Uint32ToBytes(p.pacLen)) copy(buf[4:8], gotcp.Uint32ToBytes(p.pacType)) copy(buf[8:], p.pacData) return buf } func (p *LtvPacketDelegate) GetLen() uint32 { return p.pacLen } func (p *LtvPacketDelegate) GetTypeInt() uint32 { return p.pacType } func (p *LtvPacketDelegate) GetTypeString() string { return "" } func (p *LtvPacketDelegate) GetData() []byte { return p.pacData } func NewPacket(pacType uint32, pacData []byte) *gotcp.Packet { packet := new(gotcp.Packet) packet.Delegate = &LtvPacketDelegate{ pacLen: uint32(8) + uint32(len(pacData)), pacType: pacType, pacData: pacData, } return packet } type LtvProtocol struct { } func (this *LtvProtocol) ReadPacket(r io.Reader, MaxPacketLength uint32) (*gotcp.Packet, error) { data := make([]byte, MaxPacketLength) // 设定缓存空间 readLengh, _ := r.Read(data) return NewPacket(1, data[:readLengh]), nil }