diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-08-14 07:25:33 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2015-08-14 07:25:33 +0800 |
commit | 28b14d3e6d43cb27019e21d0a93a80e7bee1de8c (patch) | |
tree | 9995026e87221a80cb5171364ffa734459c16b16 /rpc/codec | |
parent | 73c4e6005c3e47342a4631955ca6fd2782925886 (diff) | |
parent | f9cbd16f27e393d4937354ee31435e0a2f689484 (diff) | |
download | dexon-28b14d3e6d43cb27019e21d0a93a80e7bee1de8c.tar dexon-28b14d3e6d43cb27019e21d0a93a80e7bee1de8c.tar.gz dexon-28b14d3e6d43cb27019e21d0a93a80e7bee1de8c.tar.bz2 dexon-28b14d3e6d43cb27019e21d0a93a80e7bee1de8c.tar.lz dexon-28b14d3e6d43cb27019e21d0a93a80e7bee1de8c.tar.xz dexon-28b14d3e6d43cb27019e21d0a93a80e7bee1de8c.tar.zst dexon-28b14d3e6d43cb27019e21d0a93a80e7bee1de8c.zip |
Merge pull request #1635 from bas-vk/useragent
support for user agents
Diffstat (limited to 'rpc/codec')
-rw-r--r-- | rpc/codec/codec.go | 2 | ||||
-rw-r--r-- | rpc/codec/json.go | 39 |
2 files changed, 25 insertions, 16 deletions
diff --git a/rpc/codec/codec.go b/rpc/codec/codec.go index 2fdb0d8f3..786080b44 100644 --- a/rpc/codec/codec.go +++ b/rpc/codec/codec.go @@ -31,6 +31,8 @@ type ApiCoder interface { ReadRequest() ([]*shared.Request, bool, error) // Parse response message from underlying stream ReadResponse() (interface{}, error) + // Read raw message from underlying stream + Recv() (interface{}, error) // Encode response to encoded form in underlying stream WriteResponse(interface{}) error // Decode single message from data diff --git a/rpc/codec/json.go b/rpc/codec/json.go index d811b2096..cfc449143 100644 --- a/rpc/codec/json.go +++ b/rpc/codec/json.go @@ -21,6 +21,7 @@ import ( "fmt" "net" "time" + "strings" "github.com/ethereum/go-ethereum/rpc/shared" ) @@ -73,35 +74,41 @@ func (self *JsonCodec) ReadRequest() (requests []*shared.Request, isBatch bool, return nil, false, err } -func (self *JsonCodec) ReadResponse() (interface{}, error) { - bytesInBuffer := 0 - buf := make([]byte, MAX_RESPONSE_SIZE) +func (self *JsonCodec) Recv() (interface{}, error) { + var msg json.RawMessage + err := self.d.Decode(&msg) + if err != nil { + self.c.Close() + return nil, err + } - deadline := time.Now().Add(READ_TIMEOUT * time.Second) - if err := self.c.SetDeadline(deadline); err != nil { + return msg, err +} + +func (self *JsonCodec) ReadResponse() (interface{}, error) { + in, err := self.Recv() + if err != nil { return nil, err } - for { - n, err := self.c.Read(buf[bytesInBuffer:]) - if err != nil { - return nil, err + if msg, ok := in.(json.RawMessage); ok { + var req *shared.Request + if err = json.Unmarshal(msg, &req); err == nil && strings.HasPrefix(req.Method, "agent_") { + return req, nil } - bytesInBuffer += n - var failure shared.ErrorResponse - if err = json.Unmarshal(buf[:bytesInBuffer], &failure); err == nil && failure.Error != nil { + var failure *shared.ErrorResponse + if err = json.Unmarshal(msg, &failure); err == nil && failure.Error != nil { return failure, fmt.Errorf(failure.Error.Message) } - var success shared.SuccessResponse - if err = json.Unmarshal(buf[:bytesInBuffer], &success); err == nil { + var success *shared.SuccessResponse + if err = json.Unmarshal(msg, &success); err == nil { return success, nil } } - self.c.Close() - return nil, fmt.Errorf("Unable to read response") + return in, err } // Decode data |