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.
 
 
 

54 lines
1.1 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. // "fmt"
  8. "encoding/binary"
  9. )
  10. // Worker side job
  11. type outPack struct {
  12. dataType uint32
  13. data []byte
  14. handle string
  15. }
  16. func getOutPack() (outpack *outPack) {
  17. // TODO pool
  18. return &outPack{}
  19. }
  20. // Encode a job to byte slice
  21. func (outpack *outPack) Encode() (data []byte) {
  22. var l int
  23. if outpack.dataType == WORK_FAIL {
  24. l = len(outpack.handle)
  25. } else {
  26. l = len(outpack.data)
  27. if outpack.handle != "" {
  28. l += len(outpack.handle) + 1
  29. }
  30. }
  31. data = getBuffer(l + MIN_PACKET_LEN)
  32. binary.BigEndian.PutUint32(data[:4], REQ)
  33. binary.BigEndian.PutUint32(data[4:8], outpack.dataType)
  34. binary.BigEndian.PutUint32(data[8:MIN_PACKET_LEN], uint32(l))
  35. i := MIN_PACKET_LEN
  36. if outpack.handle != "" {
  37. hi := len(outpack.handle) + i
  38. copy(data[i:hi], []byte(outpack.handle))
  39. if outpack.dataType != WORK_FAIL {
  40. data[hi] = '\x00'
  41. }
  42. i = hi + 1
  43. }
  44. if outpack.dataType != WORK_FAIL {
  45. copy(data[i:], outpack.data)
  46. }
  47. return
  48. }