1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
| package main
import ( "bytes" "encoding/binary" "fmt" "io" "log" )
const ( PRIVATE_HEAD uint32 = 0x55AA55AA PROTOCOL_LENGTH uint32 = 16 )
type BufferData struct { recevData []byte hasHead bool totalLen uint32 }
type CProtocalData struct { Header uint32 Cmd uint32 Len uint32 Crc uint32 Data []byte }
type CDataRecver struct { buff BufferData recvDataVector []CProtocalData dataArrivedChan chan []byte }
func NewCDataRecver() *CDataRecver { return &CDataRecver{ dataArrivedChan: make(chan []byte, 100), } }
func (c *CDataRecver) SlotDataArrived(array []byte) { c.buff.recevData = append(c.buff.recevData, array...) c.checkBufferHasHead(&c.buff) size := uint32(len(c.buff.recevData)) if size >= c.buff.totalLen { c.parseBufferData(&c.buff, &c.recvDataVector) } log.Printf("current recv: %d, total size: %d", len(array), len(c.buff.recevData)) }
func (c *CDataRecver) checkBufferHasHead(bufferData *BufferData) { if bufferData.hasHead { return } index := bytes.Index(bufferData.recevData, uint32ToBytes(PRIVATE_HEAD)) if index == -1 { bufferData.recevData = bufferData.recevData[:0] return } if index > 0 { bufferData.recevData = bufferData.recevData[index:] } if len(bufferData.recevData) < int(PROTOCOL_LENGTH) { return } header, cmd, length, crc, _ := parseProtocolData(bufferData.recevData) if !checkCRC(header, cmd, length, crc) { log.Println("wrong crc") bufferData.recevData = bufferData.recevData[:0] bufferData.totalLen = 0 bufferData.hasHead = false return } bufferData.hasHead = true bufferData.totalLen = length }
func checkCRC(header, cmd, length, crc uint32) bool { rightCRC := header + cmd + length - PROTOCOL_LENGTH return rightCRC == crc }
func (c *CDataRecver) parseBufferData(bufferData *BufferData, vector *[]CProtocalData) { for { index := bytes.Index(bufferData.recevData, uint32ToBytes(PRIVATE_HEAD)) if index == -1 || len(bufferData.recevData) == 0 { return } if index > 0 { bufferData.recevData = bufferData.recevData[index:] } if len(bufferData.recevData) < int(PROTOCOL_LENGTH) { break } header, cmd, length, crc, data := parseProtocolData(bufferData.recevData) if !checkCRC(header, cmd, length, crc) { log.Println("wrong crc") bufferData.recevData = bufferData.recevData[:0] bufferData.hasHead = false bufferData.totalLen = 0 break } dataSize := uint32(len(data)) + PROTOCOL_LENGTH if length > dataSize { break } *vector = append(*vector, CProtocalData{ Header: header, Cmd: cmd, Len: length, Crc: crc, Data: data, }) bufferData.recevData = bufferData.recevData[dataSize:] bufferData.hasHead = false if len(bufferData.recevData) < 4 { break } } }
func parseProtocolData(data []byte) (header, cmd, length, crc uint32, protocolData []byte) { reader := bytes.NewReader(data) binary.Read(reader, binary.BigEndian, &header) binary.Read(reader, binary.BigEndian, &cmd) binary.Read(reader, binary.BigEndian, &length) binary.Read(reader, binary.BigEndian, &crc) protocolData = make([]byte, length-PROTOCOL_LENGTH) io.ReadFull(reader, protocolData) return }
func uint32ToBytes(n uint32) []byte { b := make([]byte, 4) binary.BigEndian.PutUint32(b, n) return b }
func main() { receiver := NewCDataRecver() go func() { testData := []byte{0x55, 0xAA, 0x55, 0xAA, 0, 0, 0, 1, 0, 0, 0, 20, 0, 0, 0, 60, 1, 2, 3, 4} receiver.dataArrivedChan <- testData }() for data := range receiver.dataArrivedChan { receiver.SlotDataArrived(data) } fmt.Println("Received data:", receiver.recvDataVector) }
|