You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

51 lines
1.0 KiB

  1. // Copyright 2011 Xing Xing <mikespook@gmail.com>
  2. // All rights reserved.
  3. // Use of this source code is governed by a MIT
  4. // license that can be found in the LICENSE file.
  5. package worker
  6. import (
  7. "encoding/binary"
  8. )
  9. // Worker side job
  10. type OutPack struct {
  11. DataType uint32
  12. Data []byte
  13. Handle, UniqueId, Fn string
  14. }
  15. func getOutPack() (req *OutPack) {
  16. // TODO pool
  17. return &OutPack{}
  18. }
  19. // Encode a job to byte slice
  20. func (req *OutPack) Encode() (data []byte) {
  21. var l int
  22. if req.DataType == WORK_FAIL {
  23. l = len(req.Handle)
  24. } else {
  25. l = len(req.Data)
  26. if req.Handle != "" {
  27. l += len(req.Handle) + 1
  28. }
  29. }
  30. data = getBuffer(l + MIN_PACKET_LEN)
  31. binary.BigEndian.PutUint32(data[:4], REQ)
  32. binary.BigEndian.PutUint32(data[4:8], req.DataType)
  33. binary.BigEndian.PutUint32(data[8:MIN_PACKET_LEN], uint32(l))
  34. i := MIN_PACKET_LEN
  35. if req.Handle != "" {
  36. hi := len(req.Handle) + i
  37. copy(data[i:hi], []byte(req.Handle))
  38. if req.DataType != WORK_FAIL {
  39. data[hi] = '\x00'
  40. }
  41. i = i + hi
  42. }
  43. copy(data[i:], req.Data)
  44. return
  45. }