gearman-go/worker/outpack.go

51 lines
1.0 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
2013-12-20 15:24:22 +08:00
type OutPack struct {
2013-08-30 18:01:10 +08:00
DataType uint32
2013-08-30 12:36:57 +08:00
Data []byte
Handle, UniqueId, Fn string
}
2013-12-20 15:24:22 +08:00
func getOutPack() (req *OutPack) {
2013-08-30 18:01:10 +08:00
// TODO pool
2013-12-20 15:24:22 +08:00
return &OutPack{}
2013-08-30 12:36:57 +08:00
}
// Encode a job to byte slice
2013-12-20 15:24:22 +08:00
func (req *OutPack) Encode() (data []byte) {
2013-08-30 12:36:57 +08:00
var l int
2013-08-30 18:01:10 +08:00
if req.DataType == WORK_FAIL {
l = len(req.Handle)
2013-08-30 12:36:57 +08:00
} else {
2013-08-30 18:01:10 +08:00
l = len(req.Data)
if req.Handle != "" {
l += len(req.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], req.DataType)
binary.BigEndian.PutUint32(data[8:MIN_PACKET_LEN], uint32(l))
i := MIN_PACKET_LEN
if req.Handle != "" {
hi := len(req.Handle) + i
copy(data[i:hi], []byte(req.Handle))
if req.DataType != WORK_FAIL {
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
}
2013-08-30 18:01:10 +08:00
copy(data[i:], req.Data)
2013-08-30 12:36:57 +08:00
return
}