diff options
author | Bas van Kervel <bas@ethdev.com> | 2015-06-16 17:16:50 +0800 |
---|---|---|
committer | Bas van Kervel <bas@ethdev.com> | 2015-06-22 14:54:21 +0800 |
commit | 60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17 (patch) | |
tree | 24ab79f06b1d51b8c0967e6e5f6bedd46ebc710d /rpc/comms/comms.go | |
parent | 9cf7913c61c6f9c1f36a44778a75e6a91a741be4 (diff) | |
download | go-tangerine-60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17.tar go-tangerine-60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17.tar.gz go-tangerine-60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17.tar.bz2 go-tangerine-60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17.tar.lz go-tangerine-60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17.tar.xz go-tangerine-60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17.tar.zst go-tangerine-60c2ccd99cd9acdb628a9ba5e16ad0e7e52b7e17.zip |
made ipc handler generic and reusable
Diffstat (limited to 'rpc/comms/comms.go')
-rw-r--r-- | rpc/comms/comms.go | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/rpc/comms/comms.go b/rpc/comms/comms.go index 244f5a7a6..3f458adfd 100644 --- a/rpc/comms/comms.go +++ b/rpc/comms/comms.go @@ -1,7 +1,45 @@ package comms +import ( + "io" + "net" + + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/rpc/api" + "github.com/ethereum/go-ethereum/rpc/codec" + "github.com/ethereum/go-ethereum/rpc/shared" +) + type EthereumClient interface { Close() Send(interface{}) error Recv() (interface{}, error) } + +func handle(conn net.Conn, api api.EthereumApi, c codec.Codec) { + codec := c.New(conn) + + for { + req, err := codec.ReadRequest() + if err == io.EOF { + codec.Close() + return + } else if err != nil { + glog.V(logger.Error).Infof("IPC recv err - %v\n", err) + codec.Close() + return + } + + var rpcResponse interface{} + res, err := api.Execute(req) + + rpcResponse = shared.NewRpcResponse(req.Id, req.Jsonrpc, res, err) + err = codec.WriteResponse(rpcResponse) + if err != nil { + glog.V(logger.Error).Infof("comms send err - %v\n", err) + codec.Close() + return + } + } +} |