gearman-go/client/request.go

48 lines
1.0 KiB
Go
Raw Normal View History

2013-08-29 16:51:23 +08:00
// Copyright 2013 Xing Xing <mikespook@gmail.com>.
// All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
package client
import (
"encoding/binary"
)
// request
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-08-29 18:08:05 +08:00
l := len(req.Data) // length of data
tl := l + MIN_PACKET_LEN // add 12 bytes head
2013-08-29 16:51:23 +08:00
data = getBuffer(tl)
copy(data[:4], REQ_STR)
binary.BigEndian.PutUint32(data[4:8], req.DataType)
binary.BigEndian.PutUint32(data[8:12], uint32(l))
2013-08-29 18:08:05 +08:00
copy(data[MIN_PACKET_LEN:], req.Data)
2013-08-29 16:51:23 +08:00
return
}
func getRequest() (req *request) {
// TODO add a pool
req = &request{}
return
}
func getJob(funcname, id string, data []byte) (req *request) {
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))
copy(req.Data[a+b+1:a+b+c+1], data)
return
}