2015-06-08 08:41:04 +00:00
|
|
|
package comms
|
|
|
|
|
2015-06-16 09:16:50 +00: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-17 14:22:35 +00:00
|
|
|
"strings"
|
2015-06-16 09:16:50 +00:00
|
|
|
)
|
|
|
|
|
2015-06-16 11:07:13 +00:00
|
|
|
const (
|
|
|
|
maxHttpSizeReqLength = 1024 * 1024 // 1MB
|
|
|
|
)
|
|
|
|
|
2015-06-17 14:22:35 +00:00
|
|
|
var (
|
|
|
|
// List with all API's which are offered over the in proc interface by default
|
|
|
|
DefaultInProcApis = api.AllApis
|
|
|
|
|
|
|
|
// List with all API's which are offered over the IPC interface by default
|
|
|
|
DefaultIpcApis = api.AllApis
|
|
|
|
|
|
|
|
// List with API's which are offered over thr HTTP/RPC interface by default
|
|
|
|
DefaultHttpRpcApis = strings.Join([]string{
|
|
|
|
api.DbApiName, api.EthApiName, api.NetApiName, api.Web3ApiName,
|
|
|
|
}, ",")
|
|
|
|
)
|
|
|
|
|
2015-06-08 08:41:04 +00:00
|
|
|
type EthereumClient interface {
|
2015-06-17 14:22:35 +00:00
|
|
|
// Close underlaying connection
|
2015-06-08 08:41:04 +00:00
|
|
|
Close()
|
2015-06-17 14:22:35 +00:00
|
|
|
// Send request
|
2015-06-08 08:41:04 +00:00
|
|
|
Send(interface{}) error
|
2015-06-17 14:22:35 +00:00
|
|
|
// Receive response
|
2015-06-08 08:41:04 +00:00
|
|
|
Recv() (interface{}, error)
|
|
|
|
}
|
2015-06-16 09:16:50 +00: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 11:07:13 +00:00
|
|
|
glog.V(logger.Error).Infof("comms recv err - %v\n", err)
|
2015-06-16 09:16:50 +00: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|