response.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package rpc
  2. import (
  3. "bytes"
  4. "encoding/binary"
  5. "errors"
  6. "git.nspix.com/golang/micro/gateway/rpc/codec"
  7. "git.nspix.com/golang/micro/helper/unsafestr"
  8. "io"
  9. )
  10. const (
  11. SuccessCode = 0
  12. )
  13. type Response struct {
  14. codec codec.Codec
  15. code uint16 //状态码
  16. message string //错误消息
  17. error error //错误实体
  18. Body []byte //内容
  19. }
  20. //获取状态码
  21. func (r *Response) StatusCode() int {
  22. return int(r.code)
  23. }
  24. //获取错误
  25. func (r *Response) Error() (err error) {
  26. if r.error != nil {
  27. return r.error
  28. }
  29. if r.code != SuccessCode {
  30. r.error = errors.New(r.message)
  31. err = r.error
  32. }
  33. return
  34. }
  35. func (r *Response) SetBody(i interface{}) (err error) {
  36. r.Body, err = r.codec.Encode(i)
  37. return
  38. }
  39. //解码内容
  40. func (r *Response) Decode(i interface{}) (err error) {
  41. return r.codec.Decode(r.Body, i)
  42. }
  43. func (r *Response) Bytes() []byte {
  44. buf := &bytes.Buffer{}
  45. if _, err := r.WriteTo(buf); err == nil {
  46. return buf.Bytes()
  47. }
  48. return nil
  49. }
  50. // |--code--|--msg length--|--body length--|--msg--|--body--|
  51. // |-- 2 --|-- 2 ----------| --2-----------|
  52. func (r *Response) WriteTo(w io.Writer) (n int64, err error) {
  53. var (
  54. m int
  55. )
  56. if err = binary.Write(w, binary.LittleEndian, r.code); err != nil {
  57. return
  58. }
  59. n += 2
  60. msgLen := len(r.message)
  61. bodyLen := len(r.Body)
  62. if err = binary.Write(w, binary.LittleEndian, uint16(msgLen)); err != nil {
  63. return
  64. }
  65. n += 2
  66. if err = binary.Write(w, binary.LittleEndian, uint16(bodyLen)); err != nil {
  67. return
  68. }
  69. n += 2
  70. if m, err = w.Write(unsafestr.StringToBytes(r.message)); err == nil {
  71. n += int64(m)
  72. }
  73. if m, err = w.Write(r.Body); err == nil {
  74. n += int64(m)
  75. }
  76. return
  77. }
  78. func ReadResponse(b []byte) (resp *Response, err error) {
  79. resp = &Response{
  80. codec: codec.DefaultCodec,
  81. }
  82. if b == nil || len(b) <= 6 {
  83. err = io.ErrShortBuffer
  84. return
  85. }
  86. var (
  87. p []byte
  88. msgLen uint16
  89. bodyLen uint16
  90. )
  91. resp.code = binary.LittleEndian.Uint16(b[:2])
  92. msgLen = binary.LittleEndian.Uint16(b[2:4])
  93. bodyLen = binary.LittleEndian.Uint16(b[4:6])
  94. p = b[6:]
  95. if msgLen > 0 {
  96. if len(p) < int(msgLen) {
  97. err = io.ErrShortBuffer
  98. return
  99. }
  100. resp.message = unsafestr.BytesToString(p[:msgLen])
  101. p = p[msgLen:]
  102. }
  103. if bodyLen > 0 {
  104. if len(p) < int(bodyLen) {
  105. err = io.ErrShortBuffer
  106. return
  107. }
  108. resp.Body = p[:bodyLen]
  109. }
  110. return
  111. }
  112. func NewResponse() *Response {
  113. return &Response{
  114. codec: codec.DefaultCodec,
  115. }
  116. }