json.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package codec
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net"
  21. "time"
  22. "github.com/ethereum/go-ethereum/rpc/shared"
  23. )
  24. const (
  25. READ_TIMEOUT = 60 // in seconds
  26. MAX_REQUEST_SIZE = 1024 * 1024
  27. MAX_RESPONSE_SIZE = 1024 * 1024
  28. )
  29. // Json serialization support
  30. type JsonCodec struct {
  31. c net.Conn
  32. d *json.Decoder
  33. }
  34. // Create new JSON coder instance
  35. func NewJsonCoder(conn net.Conn) ApiCoder {
  36. return &JsonCodec{
  37. c: conn,
  38. d: json.NewDecoder(conn),
  39. }
  40. }
  41. // Read incoming request and parse it to RPC request
  42. func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, err error) {
  43. deadline := time.Now().Add(READ_TIMEOUT * time.Second)
  44. if err := self.c.SetDeadline(deadline); err != nil {
  45. return nil, false, err
  46. }
  47. var incoming json.RawMessage
  48. err = self.d.Decode(&incoming)
  49. if err == nil {
  50. isBatch = incoming[0] == '['
  51. if isBatch {
  52. requests = make([]*shared.Request, 0)
  53. err = json.Unmarshal(incoming, &requests)
  54. } else {
  55. requests = make([]*shared.Request, 1)
  56. var singleRequest shared.Request
  57. if err = json.Unmarshal(incoming, &singleRequest); err == nil {
  58. requests[0] = &singleRequest
  59. }
  60. }
  61. return
  62. }
  63. self.c.Close()
  64. return nil, false, err
  65. }
  66. func (self *JsonCodec) ReadResponse() (interface{}, error) {
  67. bytesInBuffer := 0
  68. buf := make([]byte, MAX_RESPONSE_SIZE)
  69. deadline := time.Now().Add(READ_TIMEOUT * time.Second)
  70. if err := self.c.SetDeadline(deadline); err != nil {
  71. return nil, err
  72. }
  73. for {
  74. n, err := self.c.Read(buf[bytesInBuffer:])
  75. if err != nil {
  76. return nil, err
  77. }
  78. bytesInBuffer += n
  79. var failure shared.ErrorResponse
  80. if err = json.Unmarshal(buf[:bytesInBuffer], &failure); err == nil && failure.Error != nil {
  81. return failure, fmt.Errorf(failure.Error.Message)
  82. }
  83. var success shared.SuccessResponse
  84. if err = json.Unmarshal(buf[:bytesInBuffer], &success); err == nil {
  85. return success, nil
  86. }
  87. }
  88. self.c.Close()
  89. return nil, fmt.Errorf("Unable to read response")
  90. }
  91. // Decode data
  92. func (self *JsonCodec) Decode(data []byte, msg interface{}) error {
  93. return json.Unmarshal(data, msg)
  94. }
  95. // Encode message
  96. func (self *JsonCodec) Encode(msg interface{}) ([]byte, error) {
  97. return json.Marshal(msg)
  98. }
  99. // Parse JSON data from conn to obj
  100. func (self *JsonCodec) WriteResponse(res interface{}) error {
  101. data, err := json.Marshal(res)
  102. if err != nil {
  103. self.c.Close()
  104. return err
  105. }
  106. bytesWritten := 0
  107. for bytesWritten < len(data) {
  108. n, err := self.c.Write(data[bytesWritten:])
  109. if err != nil {
  110. self.c.Close()
  111. return err
  112. }
  113. bytesWritten += n
  114. }
  115. return nil
  116. }
  117. // Close decoder and encoder
  118. func (self *JsonCodec) Close() {
  119. self.c.Close()
  120. }