go-ethereum/rpc/comms/comms.go

112 lines
2.4 KiB
Go
Raw Normal View History

2015-06-08 11:41:04 +03:00
package comms
2015-06-16 12:16:50 +03:00
import (
"io"
"net"
2015-06-18 19:23:13 +03:00
"fmt"
"strings"
2015-06-19 13:32:40 +03:00
"strconv"
2015-06-16 12:16:50 +03:00
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
)
2015-06-16 14:07:13 +03:00
const (
maxHttpSizeReqLength = 1024 * 1024 // 1MB
)
var (
// List with all API's which are offered over the in proc interface by default
2015-06-22 13:47:32 +03:00
DefaultInProcApis = shared.AllApis
// List with all API's which are offered over the IPC interface by default
2015-06-22 13:47:32 +03:00
DefaultIpcApis = shared.AllApis
// List with API's which are offered over thr HTTP/RPC interface by default
DefaultHttpRpcApis = strings.Join([]string{
2015-06-22 13:47:32 +03:00
shared.DbApiName, shared.EthApiName, shared.NetApiName, shared.Web3ApiName,
2015-06-18 19:23:13 +03:00
}, ",")
)
2015-06-08 11:41:04 +03:00
type EthereumClient interface {
// Close underlaying connection
2015-06-08 11:41:04 +03:00
Close()
// Send request
2015-06-08 11:41:04 +03:00
Send(interface{}) error
// Receive response
2015-06-08 11:41:04 +03:00
Recv() (interface{}, error)
2015-06-18 19:23:13 +03:00
// List with modules this client supports
SupportedModules() (map[string]string, error)
2015-06-08 11:41:04 +03:00
}
2015-06-16 12:16:50 +03:00
2015-06-22 13:47:32 +03:00
func handle(conn net.Conn, api shared.EthereumApi, c codec.Codec) {
2015-06-16 12:16:50 +03:00
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
}
}
}
2015-06-18 19:23:13 +03:00
// Endpoint must be in the form of:
// ${protocol}:${path}
// e.g. ipc:/tmp/geth.ipc
// rpc:localhost:8545
func ClientFromEndpoint(endpoint string, c codec.Codec) (EthereumClient, error) {
if strings.HasPrefix(endpoint, "ipc:") {
cfg := IpcConfig{
Endpoint: endpoint[4:],
}
return NewIpcClient(cfg, codec.JSON)
}
if strings.HasPrefix(endpoint, "rpc:") {
2015-06-19 13:32:40 +03:00
parts := strings.Split(endpoint, ":")
addr := "http://localhost"
port := uint(8545)
if len(parts) >= 3 {
addr = parts[1] + ":" + parts[2]
}
if len(parts) >= 4 {
p, err := strconv.Atoi(parts[3])
if err != nil {
return nil, err
}
port = uint(p)
}
cfg := HttpConfig{
ListenAddress: addr,
ListenPort: port,
}
2015-06-18 19:23:13 +03:00
2015-06-19 13:32:40 +03:00
return NewHttpClient(cfg, codec.JSON), nil
2015-06-18 19:23:13 +03:00
}
return nil, fmt.Errorf("Invalid endpoint")
}