1 Star 0 Fork 0

golangx / hc

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
jsonrpc_client.go 2.71 KB
Copy Edit Raw Blame History
luohancai authored 2020-12-09 18:17 +08:00 . jsonrpc支持base
package hc
import (
"encoding/json"
"fmt"
"io"
"net"
"sync"
)
type clientCodec struct {
dec *json.Decoder // for reading JSON values
enc *json.Encoder // for writing JSON values
c io.Closer
// temporary work space
req clientRequest
resp clientResponse
// JSON-RPC responses include the request id but not the request method.
// Package rpc expects both.
// We save the request method in pending when sending a request
// and then look it up by request ID when filling out the rpc Response.
mutex sync.Mutex // protects pending
pending map[uint64]string // map request id to method name
}
// NewClientCodec returns a new ClientCodec using JSON-RPC on conn.
func NewClientCodec(conn io.ReadWriteCloser) ClientCodec {
return &clientCodec{
dec: json.NewDecoder(conn),
enc: json.NewEncoder(conn),
c: conn,
pending: make(map[uint64]string),
}
}
type clientRequest struct {
Method string `json:"method"`
Base *Base `json:"base"`
Params [1]interface{} `json:"params"`
ID uint64 `json:"id"`
}
func (c *clientCodec) WriteRequest(r *Request, base *Base, param interface{}) error {
c.mutex.Lock()
c.pending[r.Seq] = r.ServiceMethod
c.mutex.Unlock()
c.req.Method = r.ServiceMethod
c.req.Base = base
c.req.Params[0] = param
c.req.ID = r.Seq
return c.enc.Encode(&c.req)
}
type clientResponse struct {
ID uint64 `json:"id"`
Result *json.RawMessage `json:"result"`
Error interface{} `json:"error"`
}
func (r *clientResponse) reset() {
r.ID = 0
r.Result = nil
r.Error = nil
}
func (c *clientCodec) ReadResponseHeader(r *Response) error {
c.resp.reset()
if err := c.dec.Decode(&c.resp); err != nil {
return err
}
c.mutex.Lock()
r.ServiceMethod = c.pending[c.resp.ID]
delete(c.pending, c.resp.ID)
c.mutex.Unlock()
r.Error = ""
r.Seq = c.resp.ID
if c.resp.Error != nil || c.resp.Result == nil {
x, ok := c.resp.Error.(string)
if !ok {
return fmt.Errorf("invalid error %v", c.resp.Error)
}
if x == "" {
x = "unspecified error"
}
r.Error = x
}
return nil
}
func (c *clientCodec) ReadResponseBody(x interface{}) error {
if x == nil {
return nil
}
return json.Unmarshal(*c.resp.Result, x)
}
func (c *clientCodec) Close() error {
return c.c.Close()
}
// NewJSONClient returns a new Client to handle requests to the
// set of services at the other end of the connection.
func NewJSONClient(conn io.ReadWriteCloser) *Client {
return NewClientWithCodec(NewClientCodec(conn))
}
// JSONDial connects to a JSON-RPC server at the specified network address.
func JSONDial(network, address string) (*Client, error) {
conn, err := net.Dial(network, address)
if err != nil {
return nil, err
}
return NewJSONClient(conn), err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/golangx/hc.git
git@gitee.com:golangx/hc.git
golangx
hc
hc
v0.0.4

Search