json.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package codec
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net"
  21. "time"
  22. "strings"
  23. "github.com/ethereum/go-ethereum/rpc/shared"
  24. )
  25. const (
  26. READ_TIMEOUT = 60 // in seconds
  27. MAX_REQUEST_SIZE = 1024 * 1024
  28. MAX_RESPONSE_SIZE = 1024 * 1024
  29. )
  30. // Json serialization support
  31. type JsonCodec struct {
  32. c net.Conn
  33. d *json.Decoder
  34. }
  35. // Create new JSON coder instance
  36. func NewJsonCoder(conn net.Conn) ApiCoder {
  37. return &JsonCodec{
  38. c: conn,
  39. d: json.NewDecoder(conn),
  40. }
  41. }
  42. // Read incoming request and parse it to RPC request
  43. func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, err error) {
  44. deadline := time.Now().Add(READ_TIMEOUT * time.Second)
  45. if err := self.c.SetDeadline(deadline); err != nil {
  46. return nil, false, err
  47. }
  48. var incoming json.RawMessage
  49. err = self.d.Decode(&incoming)
  50. if err == nil {
  51. isBatch = incoming[0] == '['
  52. if isBatch {
  53. requests = make([]*shared.Request, 0)
  54. err = json.Unmarshal(incoming, &requests)
  55. } else {
  56. requests = make([]*shared.Request, 1)
  57. var singleRequest shared.Request
  58. if err = json.Unmarshal(incoming, &singleRequest); err == nil {
  59. requests[0] = &singleRequest
  60. }
  61. }
  62. return
  63. }
  64. self.c.Close()
  65. return nil, false, err
  66. }
  67. func (self *JsonCodec) Recv() (interface{}, error) {
  68. var msg json.RawMessage
  69. err := self.d.Decode(&msg)
  70. if err != nil {
  71. self.c.Close()
  72. return nil, err
  73. }
  74. return msg, err
  75. }
  76. func (self *JsonCodec) ReadResponse() (interface{}, error) {
  77. in, err := self.Recv()
  78. if err != nil {
  79. return nil, err
  80. }
  81. if msg, ok := in.(json.RawMessage); ok {
  82. var req *shared.Request
  83. if err = json.Unmarshal(msg, &req); err == nil && strings.HasPrefix(req.Method, "agent_") {
  84. return req, nil
  85. }
  86. var failure *shared.ErrorResponse
  87. if err = json.Unmarshal(msg, &failure); err == nil && failure.Error != nil {
  88. return failure, fmt.Errorf(failure.Error.Message)
  89. }
  90. var success *shared.SuccessResponse
  91. if err = json.Unmarshal(msg, &success); err == nil {
  92. return success, nil
  93. }
  94. }
  95. return in, err
  96. }
  97. // Decode data
  98. func (self *JsonCodec) Decode(data []byte, msg interface{}) error {
  99. return json.Unmarshal(data, msg)
  100. }
  101. // Encode message
  102. func (self *JsonCodec) Encode(msg interface{}) ([]byte, error) {
  103. return json.Marshal(msg)
  104. }
  105. // Parse JSON data from conn to obj
  106. func (self *JsonCodec) WriteResponse(res interface{}) error {
  107. data, err := json.Marshal(res)
  108. if err != nil {
  109. self.c.Close()
  110. return err
  111. }
  112. bytesWritten := 0
  113. for bytesWritten < len(data) {
  114. n, err := self.c.Write(data[bytesWritten:])
  115. if err != nil {
  116. self.c.Close()
  117. return err
  118. }
  119. bytesWritten += n
  120. }
  121. return nil
  122. }
  123. // Close decoder and encoder
  124. func (self *JsonCodec) Close() {
  125. self.c.Close()
  126. }