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.

158 lines
2.7 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package types
  2. import (
  3. "io"
  4. "github.com/golang/protobuf/proto"
  5. "github.com/tendermint/go-wire"
  6. )
  7. func RequestEcho(message string) *Request {
  8. return &Request{
  9. Type: MessageType_Echo,
  10. Data: []byte(message),
  11. }
  12. }
  13. func RequestFlush() *Request {
  14. return &Request{
  15. Type: MessageType_Flush,
  16. }
  17. }
  18. func RequestInfo() *Request {
  19. return &Request{
  20. Type: MessageType_Info,
  21. }
  22. }
  23. func RequestSetOption(key string, value string) *Request {
  24. return &Request{
  25. Type: MessageType_SetOption,
  26. Key: key,
  27. Value: value,
  28. }
  29. }
  30. func RequestAppendTx(txBytes []byte) *Request {
  31. return &Request{
  32. Type: MessageType_AppendTx,
  33. Data: txBytes,
  34. }
  35. }
  36. func RequestCheckTx(txBytes []byte) *Request {
  37. return &Request{
  38. Type: MessageType_CheckTx,
  39. Data: txBytes,
  40. }
  41. }
  42. func RequestGetHash() *Request {
  43. return &Request{
  44. Type: MessageType_GetHash,
  45. }
  46. }
  47. func RequestQuery(queryBytes []byte) *Request {
  48. return &Request{
  49. Type: MessageType_Query,
  50. Data: queryBytes,
  51. }
  52. }
  53. //----------------------------------------
  54. func ResponseException(errStr string) *Response {
  55. return &Response{
  56. Type: MessageType_Exception,
  57. Error: errStr,
  58. }
  59. }
  60. func ResponseEcho(message string) *Response {
  61. return &Response{
  62. Type: MessageType_Echo,
  63. Data: []byte(message),
  64. }
  65. }
  66. func ResponseFlush() *Response {
  67. return &Response{
  68. Type: MessageType_Flush,
  69. }
  70. }
  71. func ResponseInfo(info string) *Response {
  72. return &Response{
  73. Type: MessageType_Info,
  74. Data: []byte(info),
  75. }
  76. }
  77. func ResponseSetOption(log string) *Response {
  78. return &Response{
  79. Type: MessageType_SetOption,
  80. Log: log,
  81. }
  82. }
  83. func ResponseAppendTx(code RetCode, result []byte, log string) *Response {
  84. return &Response{
  85. Type: MessageType_AppendTx,
  86. Code: uint32(code),
  87. Data: result,
  88. Log: log,
  89. }
  90. }
  91. func ResponseCheckTx(code RetCode, result []byte, log string) *Response {
  92. return &Response{
  93. Type: MessageType_CheckTx,
  94. Code: uint32(code),
  95. Data: result,
  96. Log: log,
  97. }
  98. }
  99. func ResponseGetHash(hash []byte, log string) *Response {
  100. return &Response{
  101. Type: MessageType_GetHash,
  102. Data: hash,
  103. Log: log,
  104. }
  105. }
  106. func ResponseQuery(code RetCode, result []byte, log string) *Response {
  107. return &Response{
  108. Type: MessageType_Query,
  109. Code: uint32(code),
  110. Data: result,
  111. Log: log,
  112. }
  113. }
  114. //----------------------------------------
  115. // Write proto message, length delimited
  116. func WriteMessage(msg proto.Message, w io.Writer) error {
  117. bz, err := proto.Marshal(msg)
  118. if err != nil {
  119. return err
  120. }
  121. var n int
  122. wire.WriteByteSlice(bz, w, &n, &err)
  123. return err
  124. }
  125. // Read proto message, length delimited
  126. func ReadMessage(r io.Reader, msg proto.Message) error {
  127. var n int
  128. var err error
  129. bz := wire.ReadByteSlice(r, 0, &n, &err)
  130. if err != nil {
  131. return err
  132. }
  133. err = proto.Unmarshal(bz, msg)
  134. return err
  135. }