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.
 
 
 

87 lines
1.8 KiB

  1. // Copyright 2011 - 2012 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 common
  6. import (
  7. "bytes"
  8. "encoding/binary"
  9. )
  10. const (
  11. NETWORK = "tcp"
  12. // queue size
  13. QUEUE_SIZE = 8
  14. // read buffer size
  15. BUFFER_SIZE = 1024
  16. // min packet length
  17. PACKET_LEN = 12
  18. // \x00REQ
  19. REQ = 5391697
  20. REQ_STR = "\x00REQ"
  21. // \x00RES
  22. RES = 5391699
  23. RES_STR = "\x00RES"
  24. // package data type
  25. CAN_DO = 1
  26. CANT_DO = 2
  27. RESET_ABILITIES = 3
  28. PRE_SLEEP = 4
  29. NOOP = 6
  30. JOB_CREATED = 8
  31. GRAB_JOB = 9
  32. NO_JOB = 10
  33. JOB_ASSIGN = 11
  34. WORK_STATUS = 12
  35. WORK_COMPLETE = 13
  36. WORK_FAIL = 14
  37. GET_STATUS = 15
  38. ECHO_REQ = 16
  39. ECHO_RES = 17
  40. ERROR = 19
  41. STATUS_RES = 20
  42. SET_CLIENT_ID = 22
  43. CAN_DO_TIMEOUT = 23
  44. WORK_EXCEPTION = 25
  45. WORK_DATA = 28
  46. WORK_WARNING = 29
  47. GRAB_JOB_UNIQ = 30
  48. JOB_ASSIGN_UNIQ = 31
  49. SUBMIT_JOB = 7
  50. SUBMIT_JOB_BG = 18
  51. SUBMIT_JOB_HIGH = 21
  52. SUBMIT_JOB_HIGH_BG = 32
  53. SUBMIT_JOB_LOW = 33
  54. SUBMIT_JOB_LOW_BG = 34
  55. )
  56. // Decode [4]byte to uint32
  57. func BytesToUint32(buf [4]byte) uint32 {
  58. var r uint32
  59. b := bytes.NewBuffer(buf[:])
  60. err := binary.Read(b, binary.BigEndian, &r)
  61. if err != nil {
  62. return 0
  63. }
  64. return r
  65. }
  66. // Encode uint32 to [4]byte
  67. func Uint32ToBytes(i uint32) [4]byte {
  68. buf := new(bytes.Buffer)
  69. err := binary.Write(buf, binary.BigEndian, i)
  70. if err != nil {
  71. return [4]byte{0, 0, 0, 0}
  72. }
  73. var r [4]byte
  74. for k, v := range buf.Bytes() {
  75. r[k] = v
  76. }
  77. return r
  78. }