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.
 
 
 

48 lines
1.0 KiB

  1. // Copyright 2013 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 client
  6. import (
  7. "encoding/binary"
  8. )
  9. // request
  10. type request struct {
  11. DataType uint32
  12. Data []byte
  13. }
  14. // Encode a Request to byte slice
  15. func (req *request) Encode() (data []byte) {
  16. l := len(req.Data) // length of data
  17. tl := l + MIN_PACKET_LEN // add 12 bytes head
  18. data = getBuffer(tl)
  19. copy(data[:4], REQ_STR)
  20. binary.BigEndian.PutUint32(data[4:8], req.DataType)
  21. binary.BigEndian.PutUint32(data[8:12], uint32(l))
  22. copy(data[MIN_PACKET_LEN:], req.Data)
  23. return
  24. }
  25. func getRequest() (req *request) {
  26. // TODO add a pool
  27. req = &request{}
  28. return
  29. }
  30. func getJob(id string, funcname, data []byte) (req *request) {
  31. req = getRequest()
  32. a := len(funcname)
  33. b := len(id)
  34. c := len(data)
  35. l := a + b + c + 2
  36. req.Data = getBuffer(l)
  37. copy(req.Data[0:a], funcname)
  38. copy(req.Data[a+1:a+b+1], []byte(id))
  39. copy(req.Data[a+b+2:], data)
  40. return
  41. }