2015-06-08 11:41:04 +03:00
|
|
|
package comms
|
|
|
|
|
2015-06-16 12:16:50 +03:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2015-06-16 14:07:13 +03:00
|
|
|
const (
|
|
|
|
jsonrpcver = "2.0"
|
|
|
|
maxHttpSizeReqLength = 1024 * 1024 // 1MB
|
|
|
|
)
|
|
|
|
|
2015-06-08 11:41:04 +03:00
|
|
|
type EthereumClient interface {
|
|
|
|
Close()
|
|
|
|
Send(interface{}) error
|
|
|
|
Recv() (interface{}, error)
|
|
|
|
}
|
2015-06-16 12:16:50 +03:00
|
|
|
|
|
|
|
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 {
|
2015-06-16 14:07:13 +03:00
|
|
|
glog.V(logger.Error).Infof("comms recv err - %v\n", err)
|
2015-06-16 12:16:50 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|