2016-02-09 06:10:40 -06:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package rpc
|
|
|
|
|
2016-03-01 05:47:36 -06:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
)
|
2016-02-09 06:10:40 -06:00
|
|
|
|
|
|
|
// inProcClient is an in-process buffer stream attached to an RPC server.
|
|
|
|
type inProcClient struct {
|
|
|
|
server *Server
|
2016-03-01 05:47:36 -06:00
|
|
|
cl io.Closer
|
|
|
|
enc *json.Encoder
|
|
|
|
dec *json.Decoder
|
2016-02-09 06:10:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close tears down the request channel of the in-proc client.
|
|
|
|
func (c *inProcClient) Close() {
|
2016-03-01 05:47:36 -06:00
|
|
|
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()
|
2016-03-29 08:07:40 -05:00
|
|
|
go handler.ServeCodec(NewJSONCodec(p1), OptionMethodInvocation|OptionSubscriptions)
|
2016-03-01 05:47:36 -06:00
|
|
|
return &inProcClient{handler, p2, json.NewEncoder(p2), json.NewDecoder(p2)}
|
2016-02-09 06:10:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Send marshals a message into a json format and injects in into the client
|
|
|
|
// request channel.
|
|
|
|
func (c *inProcClient) Send(msg interface{}) error {
|
2016-03-01 05:47:36 -06:00
|
|
|
return c.enc.Encode(msg)
|
2016-02-09 06:10:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-03-01 05:47:36 -06:00
|
|
|
return c.dec.Decode(msg)
|
2016-02-09 06:10:40 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the collection of modules the RPC server offers.
|
|
|
|
func (c *inProcClient) SupportedModules() (map[string]string, error) {
|
|
|
|
return SupportedModules(c)
|
|
|
|
}
|