2022-01-31 20:55:38 +03:00
|
|
|
//Package rpc provides abstract rpc server
|
|
|
|
//
|
|
|
|
//Copyright (C) 2022 Alexander Kiryukhin <i@neonxp.dev>
|
|
|
|
//
|
2022-03-24 18:37:38 +03:00
|
|
|
//This file is part of go.neonxp.dev/jsonrpc2 project.
|
2022-01-31 20:55:38 +03:00
|
|
|
//
|
|
|
|
//This program is free software: you can redistribute it and/or modify
|
|
|
|
//it under the terms of the GNU General Public License as published by
|
|
|
|
//the Free Software Foundation, either version 3 of the License, or
|
|
|
|
//(at your option) any later version.
|
|
|
|
//
|
|
|
|
//This program 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 General Public License for more details.
|
|
|
|
//
|
|
|
|
//You should have received a copy of the GNU General Public License
|
|
|
|
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
package rpc
|
2022-01-31 02:31:43 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"sync"
|
2022-05-21 20:38:21 +03:00
|
|
|
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
|
|
|
"go.neonxp.dev/jsonrpc2/transport"
|
2022-01-31 02:31:43 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const version = "2.0"
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
type RpcServer struct {
|
2022-01-31 02:31:43 +03:00
|
|
|
Logger Logger
|
|
|
|
IgnoreNotifications bool
|
|
|
|
handlers map[string]Handler
|
2022-05-21 20:38:21 +03:00
|
|
|
transports []transport.Transport
|
2022-01-31 02:31:43 +03:00
|
|
|
mu sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
func New() *RpcServer {
|
|
|
|
return &RpcServer{
|
2022-01-31 02:31:43 +03:00
|
|
|
Logger: nopLogger{},
|
|
|
|
IgnoreNotifications: true,
|
|
|
|
handlers: map[string]Handler{},
|
2022-05-21 20:38:21 +03:00
|
|
|
transports: []transport.Transport{},
|
2022-01-31 02:31:43 +03:00
|
|
|
mu: sync.RWMutex{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
func (r *RpcServer) Register(method string, handler Handler) {
|
2022-01-31 02:31:43 +03:00
|
|
|
r.mu.Lock()
|
|
|
|
defer r.mu.Unlock()
|
|
|
|
r.handlers[method] = handler
|
|
|
|
}
|
|
|
|
|
2022-05-21 20:38:21 +03:00
|
|
|
func (r *RpcServer) AddTransport(transport transport.Transport) {
|
|
|
|
r.transports = append(r.transports, transport)
|
2022-01-31 02:31:43 +03:00
|
|
|
}
|
|
|
|
|
2022-05-21 20:38:21 +03:00
|
|
|
func (r *RpcServer) Run(ctx context.Context) error {
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
for _, t := range r.transports {
|
|
|
|
eg.Go(func(t transport.Transport) func() error {
|
|
|
|
return func() error { return t.Run(ctx, r) }
|
|
|
|
}(t))
|
2022-01-31 02:31:43 +03:00
|
|
|
}
|
2022-05-21 20:38:21 +03:00
|
|
|
return eg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *RpcServer) Resolve(ctx context.Context, rd io.Reader, w io.Writer) {
|
|
|
|
dec := json.NewDecoder(rd)
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
mu := sync.Mutex{}
|
2022-01-31 02:31:43 +03:00
|
|
|
wg := sync.WaitGroup{}
|
2022-05-21 20:38:21 +03:00
|
|
|
for {
|
|
|
|
req := new(rpcRequest)
|
|
|
|
if err := dec.Decode(req); err != nil {
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
r.Logger.Logf("Can't read body: %v", err)
|
|
|
|
WriteError(ErrCodeParseError, enc)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(req *rpcRequest) {
|
2022-01-31 02:31:43 +03:00
|
|
|
defer wg.Done()
|
2022-05-21 20:38:21 +03:00
|
|
|
resp := r.callMethod(ctx, req)
|
|
|
|
if req.Id == nil {
|
2022-01-31 02:31:43 +03:00
|
|
|
// notification request
|
|
|
|
return
|
|
|
|
}
|
2022-05-21 20:38:21 +03:00
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if err := enc.Encode(resp); err != nil {
|
|
|
|
r.Logger.Logf("Can't write response: %v", err)
|
|
|
|
WriteError(ErrCodeInternalError, enc)
|
|
|
|
}
|
|
|
|
if w, canFlush := w.(Flusher); canFlush {
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
}(req)
|
2022-01-31 02:31:43 +03:00
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:17:31 +03:00
|
|
|
func (r *RpcServer) callMethod(ctx context.Context, req *rpcRequest) *rpcResponse {
|
2022-01-31 02:31:43 +03:00
|
|
|
r.mu.RLock()
|
|
|
|
h, ok := r.handlers[req.Method]
|
|
|
|
r.mu.RUnlock()
|
|
|
|
if !ok {
|
|
|
|
return &rpcResponse{
|
|
|
|
Jsonrpc: version,
|
2022-05-22 03:27:50 +03:00
|
|
|
Error: ErrorFromCode(ErrCodeMethodNotFound),
|
2022-01-31 02:31:43 +03:00
|
|
|
Id: req.Id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
resp, err := h(ctx, req.Params)
|
|
|
|
if err != nil {
|
|
|
|
r.Logger.Logf("User error %v", err)
|
|
|
|
return &rpcResponse{
|
|
|
|
Jsonrpc: version,
|
|
|
|
Error: err,
|
|
|
|
Id: req.Id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &rpcResponse{
|
|
|
|
Jsonrpc: version,
|
|
|
|
Result: resp,
|
|
|
|
Id: req.Id,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-21 20:38:21 +03:00
|
|
|
func WriteError(code int, enc *json.Encoder) {
|
|
|
|
enc.Encode(rpcResponse{
|
2022-01-31 02:31:43 +03:00
|
|
|
Jsonrpc: version,
|
2022-05-22 03:27:50 +03:00
|
|
|
Error: ErrorFromCode(code),
|
2022-01-31 02:31:43 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type rpcRequest struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Method string `json:"method"`
|
|
|
|
Params json.RawMessage `json:"params"`
|
|
|
|
Id any `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type rpcResponse struct {
|
|
|
|
Jsonrpc string `json:"jsonrpc"`
|
|
|
|
Result json.RawMessage `json:"result,omitempty"`
|
|
|
|
Error error `json:"error,omitempty"`
|
|
|
|
Id any `json:"id,omitempty"`
|
|
|
|
}
|
2022-05-21 20:38:21 +03:00
|
|
|
|
|
|
|
type Flusher interface {
|
|
|
|
// Flush sends any buffered data to the client.
|
|
|
|
Flush()
|
|
|
|
}
|