diff options
author | Felix Lange <fjl@twurst.com> | 2016-07-12 23:47:15 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2016-07-23 05:21:27 +0800 |
commit | 91b769042857f542b2792b23ec407e1c9bd4fe8d (patch) | |
tree | f6730b3e85a7ac5ca98f9a716505349958fcacd3 /rpc/inproc.go | |
parent | bb01bea4e276dad359815c682a2dee730737f4dc (diff) | |
download | go-tangerine-91b769042857f542b2792b23ec407e1c9bd4fe8d.tar go-tangerine-91b769042857f542b2792b23ec407e1c9bd4fe8d.tar.gz go-tangerine-91b769042857f542b2792b23ec407e1c9bd4fe8d.tar.bz2 go-tangerine-91b769042857f542b2792b23ec407e1c9bd4fe8d.tar.lz go-tangerine-91b769042857f542b2792b23ec407e1c9bd4fe8d.tar.xz go-tangerine-91b769042857f542b2792b23ec407e1c9bd4fe8d.tar.zst go-tangerine-91b769042857f542b2792b23ec407e1c9bd4fe8d.zip |
rpc: add new client, use it everywhere
The new client implementation supports concurrent requests,
subscriptions and replaces the various ad hoc RPC clients
throughout go-ethereum.
Diffstat (limited to 'rpc/inproc.go')
-rw-r--r-- | rpc/inproc.go | 49 |
1 files changed, 11 insertions, 38 deletions
diff --git a/rpc/inproc.go b/rpc/inproc.go index 250f5c787..f72b97497 100644 --- a/rpc/inproc.go +++ b/rpc/inproc.go @@ -17,45 +17,18 @@ package rpc import ( - "encoding/json" - "io" "net" -) - -// inProcClient is an in-process buffer stream attached to an RPC server. -type inProcClient struct { - server *Server - cl io.Closer - enc *json.Encoder - dec *json.Decoder -} -// Close tears down the request channel of the in-proc client. -func (c *inProcClient) Close() { - c.cl.Close() -} - -// NewInProcRPCClient creates an in-process buffer stream attachment to a given -// RPC server. -func NewInProcRPCClient(handler *Server) Client { - p1, p2 := net.Pipe() - go handler.ServeCodec(NewJSONCodec(p1), OptionMethodInvocation|OptionSubscriptions) - return &inProcClient{handler, p2, json.NewEncoder(p2), json.NewDecoder(p2)} -} - -// Send marshals a message into a json format and injects in into the client -// request channel. -func (c *inProcClient) Send(msg interface{}) error { - return c.enc.Encode(msg) -} - -// Recv reads a message from the response channel and tries to parse it into the -// given msg interface. -func (c *inProcClient) Recv(msg interface{}) error { - return c.dec.Decode(msg) -} + "golang.org/x/net/context" +) -// Returns the collection of modules the RPC server offers. -func (c *inProcClient) SupportedModules() (map[string]string, error) { - return SupportedModules(c) +// NewInProcClient attaches an in-process connection to the given RPC server. +func DialInProc(handler *Server) *Client { + initctx := context.Background() + c, _ := newClient(initctx, func(context.Context) (net.Conn, error) { + p1, p2 := net.Pipe() + go handler.ServeCodec(NewJSONCodec(p1), OptionMethodInvocation|OptionSubscriptions) + return p2, nil + }) + return c } |