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.
 
 
 

63 lines
1.4 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. "bytes"
  8. "fmt"
  9. "encoding/binary"
  10. )
  11. // Worker side job
  12. type InPack struct {
  13. DataType uint32
  14. Data []byte
  15. Handle, UniqueId, Fn string
  16. a *agent
  17. }
  18. // Create a new job
  19. func getInPack() (resp *InPack) {
  20. return &InPack{}
  21. }
  22. // Decode job from byte slice
  23. func decodeInPack(data []byte) (resp *InPack, l int, err error) {
  24. if len(data) < MIN_PACKET_LEN { // valid package should not less 12 bytes
  25. err = fmt.Errorf("Invalid data: %V", data)
  26. return
  27. }
  28. dl := int(binary.BigEndian.Uint32(data[8:12]))
  29. dt := data[MIN_PACKET_LEN : dl+MIN_PACKET_LEN]
  30. if len(dt) != int(dl) { // length not equal
  31. err = fmt.Errorf("Invalid data: %V", data)
  32. return
  33. }
  34. resp = getInPack()
  35. resp.DataType = binary.BigEndian.Uint32(data[4:8])
  36. switch resp.DataType {
  37. case JOB_ASSIGN:
  38. s := bytes.SplitN(dt, []byte{'\x00'}, 3)
  39. if len(s) == 3 {
  40. resp.Handle = string(s[0])
  41. resp.Fn = string(s[1])
  42. resp.Data = s[2]
  43. }
  44. case JOB_ASSIGN_UNIQ:
  45. s := bytes.SplitN(dt, []byte{'\x00'}, 4)
  46. if len(s) == 4 {
  47. resp.Handle = string(s[0])
  48. resp.Fn = string(s[1])
  49. resp.UniqueId = string(s[2])
  50. resp.Data = s[3]
  51. }
  52. default:
  53. resp.Data = dt
  54. }
  55. l = dl + MIN_PACKET_LEN
  56. return
  57. }