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.
 
 
 

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