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-12-24 14:39:04 +08:00
|
|
|
// "fmt"
|
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 {
|
|
|
|
dataType uint32
|
|
|
|
data []byte
|
|
|
|
handle, uniqueId, fn 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-21 11:09:13 +08:00
|
|
|
if outpack.dataType == WORK_FAIL {
|
|
|
|
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-08-30 18:01:10 +08:00
|
|
|
data = getBuffer(l + MIN_PACKET_LEN)
|
|
|
|
binary.BigEndian.PutUint32(data[:4], REQ)
|
2013-12-21 11:09:13 +08:00
|
|
|
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
|
2013-12-21 11:09:13 +08:00
|
|
|
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-12-24 14:35:33 +08:00
|
|
|
i = hi + 1
|
|
|
|
}
|
|
|
|
if outpack.dataType != WORK_FAIL {
|
|
|
|
copy(data[i:], outpack.data)
|
2013-08-30 12:36:57 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|