made ipc handler generic and reusable

This commit is contained in:
Bas van Kervel 2015-06-16 11:16:50 +02:00
parent 9cf7913c61
commit 60c2ccd99c
2 changed files with 39 additions and 28 deletions

View File

@ -1,7 +1,45 @@
package comms 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 { type EthereumClient interface {
Close() Close()
Send(interface{}) error Send(interface{}) error
Recv() (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
}
}
}

View File

@ -3,7 +3,6 @@
package comms package comms
import ( import (
"io"
"net" "net"
"os" "os"
@ -11,7 +10,6 @@ import (
"github.com/ethereum/go-ethereum/logger/glog" "github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/rpc/api" "github.com/ethereum/go-ethereum/rpc/api"
"github.com/ethereum/go-ethereum/rpc/codec" "github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
) )
func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) { func newIpcClient(cfg IpcConfig, codec codec.Codec) (*ipcClient, error) {
@ -40,32 +38,7 @@ func startIpc(cfg IpcConfig, codec codec.Codec, api api.EthereumApi) error {
continue continue
} }
go func(conn net.Conn) { go handle(conn, api, codec)
codec := codec.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("IPC send err - %v\n", err)
codec.Close()
return
}
}
}(conn)
} }
os.Remove(cfg.Endpoint) os.Remove(cfg.Endpoint)