mirror of
https://github.com/hybridgroup/gobot.git
synced 2025-04-27 13:48:56 +08:00
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
![]() |
package client
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/binary"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type nwFrameGenerator struct {
|
||
|
seq map[byte]byte
|
||
|
hlen int
|
||
|
mutex *sync.Mutex
|
||
|
}
|
||
|
|
||
|
func newNetworkFrameGenerator() *nwFrameGenerator {
|
||
|
nwg := nwFrameGenerator{
|
||
|
seq: make(map[byte]byte), // each frame id has it's own sequence number
|
||
|
hlen: 7, // size of ARNETWORKAL_Frame_t header
|
||
|
mutex: &sync.Mutex{},
|
||
|
}
|
||
|
return &nwg
|
||
|
}
|
||
|
|
||
|
// generate the "NetworkFrame" as bytes buffer
|
||
|
func (nwg *nwFrameGenerator) generate(cmd *bytes.Buffer, frameType byte, id byte) *bytes.Buffer {
|
||
|
nwg.mutex.Lock()
|
||
|
defer nwg.mutex.Unlock()
|
||
|
|
||
|
// func networkFrameGenerator() func(*bytes.Buffer, byte, byte) NetworkFrame {
|
||
|
//
|
||
|
// ARNETWORKAL_Frame_t
|
||
|
//
|
||
|
// uint8 type - frame type ARNETWORK_FRAME_TYPE
|
||
|
// uint8 id - identifier of the buffer sending the frame
|
||
|
// uint8 seq - sequence number of the frame
|
||
|
// uint32 size - size of the frame
|
||
|
//
|
||
|
|
||
|
if _, ok := nwg.seq[id]; !ok {
|
||
|
nwg.seq[id] = 0
|
||
|
}
|
||
|
|
||
|
nwg.seq[id]++
|
||
|
|
||
|
if nwg.seq[id] > 255 {
|
||
|
nwg.seq[id] = 0
|
||
|
}
|
||
|
|
||
|
ret := &bytes.Buffer{}
|
||
|
ret.WriteByte(frameType)
|
||
|
ret.WriteByte(id)
|
||
|
ret.WriteByte(nwg.seq[id])
|
||
|
|
||
|
size := &bytes.Buffer{}
|
||
|
if err := binary.Write(size, binary.LittleEndian, uint32(cmd.Len()+nwg.hlen)); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
ret.Write(size.Bytes())
|
||
|
ret.Write(cmd.Bytes())
|
||
|
|
||
|
return ret
|
||
|
}
|