2013-08-29 16:51:23 +08:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
)
|
|
|
|
|
2013-12-20 15:24:22 +08:00
|
|
|
// Request from client
|
2013-08-29 16:51:23 +08:00
|
|
|
type request struct {
|
2013-08-29 18:08:05 +08:00
|
|
|
DataType uint32
|
|
|
|
Data []byte
|
2013-08-29 16:51:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a Request to byte slice
|
|
|
|
func (req *request) Encode() (data []byte) {
|
2013-12-26 15:28:42 +08:00
|
|
|
l := len(req.Data) // length of data
|
|
|
|
tl := l + minPacketLength // add 12 bytes head
|
2013-08-29 16:51:23 +08:00
|
|
|
data = getBuffer(tl)
|
2013-12-26 15:28:42 +08:00
|
|
|
copy(data[:4], reqStr)
|
2013-08-29 16:51:23 +08:00
|
|
|
binary.BigEndian.PutUint32(data[4:8], req.DataType)
|
|
|
|
binary.BigEndian.PutUint32(data[8:12], uint32(l))
|
2013-12-26 15:28:42 +08:00
|
|
|
copy(data[minPacketLength:], req.Data)
|
2013-08-29 16:51:23 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRequest() (req *request) {
|
|
|
|
// TODO add a pool
|
|
|
|
req = &request{}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-08-30 11:20:51 +08:00
|
|
|
func getJob(id string, funcname, data []byte) (req *request) {
|
2013-08-29 16:51:23 +08:00
|
|
|
req = getRequest()
|
|
|
|
a := len(funcname)
|
|
|
|
b := len(id)
|
|
|
|
c := len(data)
|
|
|
|
l := a + b + c + 2
|
|
|
|
req.Data = getBuffer(l)
|
|
|
|
copy(req.Data[0:a], funcname)
|
|
|
|
copy(req.Data[a+1:a+b+1], []byte(id))
|
2013-08-30 11:47:53 +08:00
|
|
|
copy(req.Data[a+b+2:], data)
|
2013-08-29 16:51:23 +08:00
|
|
|
return
|
|
|
|
}
|