aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/comms/comms.go
blob: 2af63e55dc4d5e7b8f700426d57edf163b72c22b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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"
)

const (
    jsonrpcver           = "2.0"
    maxHttpSizeReqLength = 1024 * 1024 // 1MB
)

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("comms 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
        }
    }
}