2019-07-03 17:39:07 +00:00
|
|
|
package jsonrpc
|
2019-06-28 13:49:34 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-28 14:53:01 +00:00
|
|
|
"context"
|
2019-06-28 13:49:34 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2019-06-28 14:05:10 +00:00
|
|
|
"fmt"
|
2019-07-23 18:49:09 +00:00
|
|
|
"net/http"
|
2019-06-28 13:49:34 +00:00
|
|
|
"reflect"
|
2019-07-03 14:32:31 +00:00
|
|
|
"sync/atomic"
|
2019-07-08 19:07:16 +00:00
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2019-07-08 19:07:16 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-18 10:46:32 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-06-28 13:49:34 +00:00
|
|
|
)
|
|
|
|
|
2019-07-08 19:07:16 +00:00
|
|
|
var log = logging.Logger("rpc")
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
var (
|
2019-07-01 20:00:22 +00:00
|
|
|
errorType = reflect.TypeOf(new(error)).Elem()
|
2019-06-28 14:53:01 +00:00
|
|
|
contextType = reflect.TypeOf(new(context.Context)).Elem()
|
|
|
|
)
|
|
|
|
|
2019-07-02 19:08:30 +00:00
|
|
|
// ErrClient is an error which occurred on the client side the library
|
2019-06-28 14:05:10 +00:00
|
|
|
type ErrClient struct {
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *ErrClient) Error() string {
|
|
|
|
return fmt.Sprintf("RPC client error: %s", e.err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// Unwrap unwraps the actual error
|
2019-06-28 14:05:10 +00:00
|
|
|
func (e *ErrClient) Unwrap(err error) error {
|
|
|
|
return e.err
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
type clientResponse struct {
|
2019-07-22 20:28:40 +00:00
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Result json.RawMessage `json:"result"`
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Error *respError `json:"error,omitempty"`
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
type clientRequest struct {
|
|
|
|
req request
|
|
|
|
ready chan clientResponse
|
2019-07-22 18:13:41 +00:00
|
|
|
|
2019-07-22 23:58:43 +00:00
|
|
|
// retCh provides a context and sink for handling incoming channel messages
|
|
|
|
retCh func() (context.Context, func([]byte, bool))
|
2019-07-12 15:29:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-03 12:30:21 +00:00
|
|
|
// ClientCloser is used to close Client from further use
|
|
|
|
type ClientCloser func()
|
|
|
|
|
2019-07-02 17:45:03 +00:00
|
|
|
// NewClient creates new josnrpc 2.0 client
|
|
|
|
//
|
|
|
|
// handler must be pointer to a struct with function fields
|
2019-07-23 19:38:10 +00:00
|
|
|
// Returned value closes the client connection
|
2019-07-02 17:45:03 +00:00
|
|
|
// TODO: Example
|
2019-07-23 18:49:09 +00:00
|
|
|
func NewClient(addr string, namespace string, handler interface{}, requestHeader http.Header) (ClientCloser, error) {
|
2019-06-28 13:49:34 +00:00
|
|
|
htyp := reflect.TypeOf(handler)
|
|
|
|
if htyp.Kind() != reflect.Ptr {
|
2019-07-22 22:56:48 +00:00
|
|
|
return nil, xerrors.New("expected handler to be a pointer")
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
typ := htyp.Elem()
|
|
|
|
if typ.Kind() != reflect.Struct {
|
2019-07-22 22:56:48 +00:00
|
|
|
return nil, xerrors.New("handler should be a struct")
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
val := reflect.ValueOf(handler)
|
|
|
|
|
2019-07-03 14:32:31 +00:00
|
|
|
var idCtr int64
|
2019-06-28 13:49:34 +00:00
|
|
|
|
2019-07-23 18:49:09 +00:00
|
|
|
conn, _, err := websocket.DefaultDialer.Dial(addr, requestHeader)
|
2019-07-12 15:29:41 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
stop := make(chan struct{})
|
|
|
|
requests := make(chan clientRequest)
|
|
|
|
|
2019-07-12 17:12:38 +00:00
|
|
|
handlers := map[string]rpcHandler{}
|
2019-07-23 01:20:48 +00:00
|
|
|
go (&wsConn{
|
|
|
|
conn: conn,
|
|
|
|
handler: handlers,
|
|
|
|
requests: requests,
|
|
|
|
stop: stop,
|
|
|
|
}).handleWsConn(context.TODO())
|
2019-07-12 15:29:41 +00:00
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
for i := 0; i < typ.NumField(); i++ {
|
|
|
|
f := typ.Field(i)
|
|
|
|
ftyp := f.Type
|
|
|
|
if ftyp.Kind() != reflect.Func {
|
2019-07-22 22:56:48 +00:00
|
|
|
return nil, xerrors.New("handler field not a func")
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-08 12:46:30 +00:00
|
|
|
valOut, errOut, nout := processFuncOut(ftyp)
|
2019-06-28 13:49:34 +00:00
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
processResponse := func(resp clientResponse, rval reflect.Value) []reflect.Value {
|
2019-06-28 13:49:34 +00:00
|
|
|
out := make([]reflect.Value, nout)
|
|
|
|
|
|
|
|
if valOut != -1 {
|
2019-07-22 18:13:41 +00:00
|
|
|
out[valOut] = rval
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
if errOut != -1 {
|
2019-06-28 14:53:01 +00:00
|
|
|
out[errOut] = reflect.New(errorType).Elem()
|
2019-06-28 13:49:34 +00:00
|
|
|
if resp.Error != nil {
|
2019-07-02 13:49:10 +00:00
|
|
|
out[errOut].Set(reflect.ValueOf(resp.Error))
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-07-01 20:00:22 +00:00
|
|
|
processError := func(err error) []reflect.Value {
|
2019-06-28 14:05:10 +00:00
|
|
|
out := make([]reflect.Value, nout)
|
|
|
|
|
|
|
|
if valOut != -1 {
|
|
|
|
out[valOut] = reflect.New(ftyp.Out(valOut)).Elem()
|
|
|
|
}
|
|
|
|
if errOut != -1 {
|
2019-06-28 14:53:01 +00:00
|
|
|
out[errOut] = reflect.New(errorType).Elem()
|
2019-06-28 14:05:10 +00:00
|
|
|
out[errOut].Set(reflect.ValueOf(&ErrClient{err}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:53:01 +00:00
|
|
|
hasCtx := 0
|
|
|
|
if ftyp.NumIn() > 0 && ftyp.In(0) == contextType {
|
|
|
|
hasCtx = 1
|
|
|
|
}
|
2019-07-22 18:13:41 +00:00
|
|
|
retCh := valOut != -1 && ftyp.Out(valOut).Kind() == reflect.Chan
|
2019-06-28 14:53:01 +00:00
|
|
|
|
2019-06-28 13:49:34 +00:00
|
|
|
fn := reflect.MakeFunc(ftyp, func(args []reflect.Value) (results []reflect.Value) {
|
2019-07-03 14:32:31 +00:00
|
|
|
id := atomic.AddInt64(&idCtr, 1)
|
2019-07-08 12:46:30 +00:00
|
|
|
params := make([]param, len(args)-hasCtx)
|
2019-06-28 14:53:01 +00:00
|
|
|
for i, arg := range args[hasCtx:] {
|
2019-07-08 12:46:30 +00:00
|
|
|
params[i] = param{
|
2019-06-28 13:49:34 +00:00
|
|
|
v: arg,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-22 23:58:43 +00:00
|
|
|
var ctx context.Context
|
|
|
|
if hasCtx == 1 {
|
|
|
|
ctx = args[0].Interface().(context.Context)
|
|
|
|
}
|
|
|
|
|
2019-07-22 18:13:41 +00:00
|
|
|
var retVal reflect.Value
|
2019-07-22 23:58:43 +00:00
|
|
|
|
|
|
|
// if the function returns a channel, we need to provide a sink for the
|
|
|
|
// messages
|
|
|
|
var chCtor func() (context.Context, func([]byte, bool))
|
|
|
|
|
2019-07-22 18:13:41 +00:00
|
|
|
if retCh {
|
2019-07-22 22:46:28 +00:00
|
|
|
retVal = reflect.Zero(ftyp.Out(valOut))
|
2019-07-22 23:58:43 +00:00
|
|
|
|
|
|
|
chCtor = func() (context.Context, func([]byte, bool)) {
|
2019-07-22 18:13:41 +00:00
|
|
|
// unpack chan type to make sure it's reflect.BothDir
|
|
|
|
ctyp := reflect.ChanOf(reflect.BothDir, ftyp.Out(valOut).Elem())
|
|
|
|
ch := reflect.MakeChan(ctyp, 0) // todo: buffer?
|
|
|
|
retVal = ch.Convert(ftyp.Out(valOut))
|
|
|
|
|
2019-07-22 23:58:43 +00:00
|
|
|
return ctx, func(result []byte, ok bool) {
|
2019-07-22 18:13:41 +00:00
|
|
|
if !ok {
|
2019-07-22 23:58:43 +00:00
|
|
|
// remote channel closed, close ours too
|
2019-07-22 18:13:41 +00:00
|
|
|
ch.Close()
|
|
|
|
return
|
|
|
|
}
|
2019-07-22 23:58:43 +00:00
|
|
|
|
2019-07-22 18:13:41 +00:00
|
|
|
val := reflect.New(ftyp.Out(valOut).Elem())
|
|
|
|
if err := json.Unmarshal(result, val.Interface()); err != nil {
|
|
|
|
log.Errorf("error unmarshaling chan response: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-07-22 23:58:43 +00:00
|
|
|
ch.Send(val.Elem()) // todo: select on ctx is probably a good idea
|
2019-07-22 18:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-08 12:46:30 +00:00
|
|
|
req := request{
|
2019-06-28 13:49:34 +00:00
|
|
|
Jsonrpc: "2.0",
|
2019-07-02 19:08:30 +00:00
|
|
|
ID: &id,
|
2019-06-28 13:49:34 +00:00
|
|
|
Method: namespace + "." + f.Name,
|
|
|
|
Params: params,
|
|
|
|
}
|
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
rchan := make(chan clientResponse, 1)
|
|
|
|
requests <- clientRequest{
|
|
|
|
req: req,
|
|
|
|
ready: rchan,
|
2019-07-22 18:13:41 +00:00
|
|
|
|
|
|
|
retCh: chCtor,
|
2019-07-08 19:07:16 +00:00
|
|
|
}
|
2019-07-15 16:21:48 +00:00
|
|
|
var ctxDone <-chan struct{}
|
|
|
|
var resp clientResponse
|
|
|
|
|
2019-07-22 23:58:43 +00:00
|
|
|
if ctx != nil {
|
|
|
|
ctxDone = ctx.Done()
|
2019-07-15 16:21:48 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:13:41 +00:00
|
|
|
// wait for response, handle context cancellation
|
2019-07-15 16:21:48 +00:00
|
|
|
loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case resp = <-rchan:
|
|
|
|
break loop
|
|
|
|
case <-ctxDone: // send cancel request
|
|
|
|
ctxDone = nil
|
|
|
|
|
|
|
|
requests <- clientRequest{
|
|
|
|
req: request{
|
|
|
|
Jsonrpc: "2.0",
|
2019-07-15 16:32:43 +00:00
|
|
|
Method: wsCancel,
|
|
|
|
Params: []param{{v: reflect.ValueOf(id)}},
|
2019-07-15 16:21:48 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-08 19:07:16 +00:00
|
|
|
|
2019-07-22 18:13:41 +00:00
|
|
|
if valOut != -1 && !retCh {
|
|
|
|
retVal = reflect.New(ftyp.Out(valOut))
|
2019-07-15 16:21:48 +00:00
|
|
|
|
|
|
|
if resp.Result != nil {
|
|
|
|
log.Debugw("rpc result", "type", ftyp.Out(valOut))
|
2019-07-22 18:13:41 +00:00
|
|
|
if err := json.Unmarshal(resp.Result, retVal.Interface()); err != nil {
|
2019-07-18 10:54:26 +00:00
|
|
|
return processError(xerrors.Errorf("unmarshaling result: %w", err))
|
2019-07-15 16:21:48 +00:00
|
|
|
}
|
2019-07-12 15:29:41 +00:00
|
|
|
}
|
2019-07-22 18:13:41 +00:00
|
|
|
|
|
|
|
retVal = retVal.Elem()
|
2019-07-02 19:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.ID != *req.ID {
|
2019-06-28 14:05:10 +00:00
|
|
|
return processError(errors.New("request and response id didn't match"))
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:13:41 +00:00
|
|
|
return processResponse(resp, retVal)
|
2019-06-28 13:49:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
val.Elem().Field(i).Set(fn)
|
|
|
|
}
|
2019-07-03 14:32:31 +00:00
|
|
|
|
2019-07-12 15:29:41 +00:00
|
|
|
return func() {
|
|
|
|
close(stop)
|
|
|
|
}, nil
|
2019-06-28 13:49:34 +00:00
|
|
|
}
|