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
|
2013-12-21 11:09:13 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-21 11:09:13 +08:00
|
|
|
func getOutPack() (outpack *outPack) {
|
2013-08-30 18:01:10 +08:00
|
|
|
// TODO pool
|
2013-12-21 11:09:13 +08:00
|
|
|
return &outPack{}
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Encode a job to byte slice
|
2013-12-21 11:09:13 +08:00
|
|
|
func (outpack *outPack) Encode() (data []byte) {
|
2013-08-30 12:36:57 +08:00
|
|
|
var l int
|
2013-12-26 12:32:27 +08:00
|
|
|
if outpack.dataType == dtWorkFail {
|
2013-12-21 11:09:13 +08:00
|
|
|
l = len(outpack.handle)
|
2013-08-30 12:36:57 +08:00
|
|
|
} else {
|
2013-12-21 11:09:13 +08:00
|
|
|
l = len(outpack.data)
|
|
|
|
if outpack.handle != "" {
|
|
|
|
l += len(outpack.handle) + 1
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-26 12:32:27 +08:00
|
|
|
data = getBuffer(l + minPacketLength)
|
|
|
|
binary.BigEndian.PutUint32(data[:4], req)
|
2013-12-21 11:09:13 +08:00
|
|
|
binary.BigEndian.PutUint32(data[4:8], outpack.dataType)
|
2013-12-26 12:32:27 +08:00
|
|
|
binary.BigEndian.PutUint32(data[8:minPacketLength], uint32(l))
|
|
|
|
i := minPacketLength
|
2013-12-21 11:09:13 +08:00
|
|
|
if outpack.handle != "" {
|
|
|
|
hi := len(outpack.handle) + i
|
|
|
|
copy(data[i:hi], []byte(outpack.handle))
|
2013-12-26 12:32:27 +08:00
|
|
|
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
|
|
|
|
}
|
2013-12-26 12:32:27 +08:00
|
|
|
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
|
|
|
|
}
|