gearman-go/worker/outpack.go

51 lines
1.1 KiB
Go
Raw Normal View History

2013-08-30 12:36:57 +08:00
// Copyright 2011 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 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 {
dataType uint32
data []byte
handle, uniqueId, fn 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 == WORK_FAIL {
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
}
}
2013-08-30 18:01:10 +08:00
data = getBuffer(l + MIN_PACKET_LEN)
binary.BigEndian.PutUint32(data[:4], REQ)
binary.BigEndian.PutUint32(data[4:8], outpack.dataType)
2013-08-30 18:01:10 +08:00
binary.BigEndian.PutUint32(data[8:MIN_PACKET_LEN], uint32(l))
i := MIN_PACKET_LEN
if outpack.handle != "" {
hi := len(outpack.handle) + i
copy(data[i:hi], []byte(outpack.handle))
if outpack.dataType != WORK_FAIL {
2013-08-30 18:01:10 +08:00
data[hi] = '\x00'
2013-08-30 12:36:57 +08:00
}
2013-08-30 18:01:10 +08:00
i = i + hi
2013-08-30 12:36:57 +08:00
}
copy(data[i:], outpack.data)
2013-08-30 12:36:57 +08:00
return
}