github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/codec/jsonrpc/server.go (about)

     1  // Licensed under the Apache License, Version 2.0 (the "License");
     2  // you may not use this file except in compliance with the License.
     3  // You may obtain a copy of the License at
     4  //
     5  //     https://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS,
     9  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    10  // See the License for the specific language governing permissions and
    11  // limitations under the License.
    12  //
    13  // Original source: github.com/micro/go-micro/v3/codec/jsonrpc/server.go
    14  
    15  package jsonrpc
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"io"
    21  
    22  	"github.com/tickoalcantara12/micro/v3/util/codec"
    23  )
    24  
    25  type serverCodec struct {
    26  	dec *json.Decoder // for reading JSON values
    27  	enc *json.Encoder // for writing JSON values
    28  	c   io.Closer
    29  
    30  	// temporary work space
    31  	req  serverRequest
    32  	resp serverResponse
    33  }
    34  
    35  type serverRequest struct {
    36  	Method string           `json:"method"`
    37  	Params *json.RawMessage `json:"params"`
    38  	ID     interface{}      `json:"id"`
    39  }
    40  
    41  type serverResponse struct {
    42  	ID     interface{} `json:"id"`
    43  	Result interface{} `json:"result"`
    44  	Error  interface{} `json:"error"`
    45  }
    46  
    47  func newServerCodec(conn io.ReadWriteCloser) *serverCodec {
    48  	return &serverCodec{
    49  		dec: json.NewDecoder(conn),
    50  		enc: json.NewEncoder(conn),
    51  		c:   conn,
    52  	}
    53  }
    54  
    55  func (r *serverRequest) reset() {
    56  	r.Method = ""
    57  	if r.Params != nil {
    58  		*r.Params = (*r.Params)[0:0]
    59  	}
    60  	if r.ID != nil {
    61  		r.ID = nil
    62  	}
    63  }
    64  
    65  func (c *serverCodec) ReadHeader(m *codec.Message) error {
    66  	c.req.reset()
    67  	if err := c.dec.Decode(&c.req); err != nil {
    68  		return err
    69  	}
    70  	m.Method = c.req.Method
    71  	m.Id = fmt.Sprintf("%v", c.req.ID)
    72  	c.req.ID = nil
    73  	return nil
    74  }
    75  
    76  func (c *serverCodec) ReadBody(x interface{}) error {
    77  	if x == nil {
    78  		return nil
    79  	}
    80  	var params [1]interface{}
    81  	params[0] = x
    82  	return json.Unmarshal(*c.req.Params, &params)
    83  }
    84  
    85  var null = json.RawMessage([]byte("null"))
    86  
    87  func (c *serverCodec) Write(m *codec.Message, x interface{}) error {
    88  	var resp serverResponse
    89  	resp.ID = m.Id
    90  	resp.Result = x
    91  	if m.Error == "" {
    92  		resp.Error = nil
    93  	} else {
    94  		resp.Error = m.Error
    95  	}
    96  	return c.enc.Encode(resp)
    97  }
    98  
    99  func (c *serverCodec) Close() error {
   100  	return c.c.Close()
   101  }