gearman-go/worker/outpack.go

48 lines
958 B
Go
Raw Normal View History

2013-08-30 12:36:57 +08:00
package worker
import (
2013-08-30 18:01:10 +08:00
"encoding/binary"
2013-08-30 12:36:57 +08:00
)
// Worker side job
type outPack struct {
2013-12-24 16:35:17 +08:00
dataType uint32
data []byte
handle string
2013-08-30 12:36:57 +08:00
}
func getOutPack() (outpack *outPack) {
2013-08-30 18:01:10 +08:00
// TODO pool
return &outPack{}
2013-08-30 12:36:57 +08:00
}
// Encode a job to byte slice
func (outpack *outPack) Encode() (data []byte) {
2013-08-30 12:36:57 +08:00
var l int
if outpack.dataType == dtWorkFail {
l = len(outpack.handle)
2013-08-30 12:36:57 +08:00
} else {
l = len(outpack.data)
if outpack.handle != "" {
l += len(outpack.handle) + 1
2013-08-30 12:36:57 +08:00
}
}
data = getBuffer(l + minPacketLength)
binary.BigEndian.PutUint32(data[:4], req)
binary.BigEndian.PutUint32(data[4:8], outpack.dataType)
binary.BigEndian.PutUint32(data[8:minPacketLength], uint32(l))
i := minPacketLength
if outpack.handle != "" {
hi := len(outpack.handle) + i
copy(data[i:hi], []byte(outpack.handle))
if outpack.dataType != dtWorkFail {
2013-08-30 18:01:10 +08:00
data[hi] = '\x00'
2013-08-30 12:36:57 +08:00
}
2013-12-24 14:35:33 +08:00
i = hi + 1
}
if outpack.dataType != dtWorkFail {
2013-12-24 14:35:33 +08:00
copy(data[i:], outpack.data)
2013-08-30 12:36:57 +08:00
}
return
}