github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/util/codec/jsonrpc/jsonrpc.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/jsonrpc.go
    14  
    15  // Package jsonrpc provides a json-rpc 1.0 codec
    16  package jsonrpc
    17  
    18  import (
    19  	"bytes"
    20  	"encoding/json"
    21  	"fmt"
    22  	"io"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/util/codec"
    25  )
    26  
    27  type jsonCodec struct {
    28  	buf *bytes.Buffer
    29  	mt  codec.MessageType
    30  	rwc io.ReadWriteCloser
    31  	c   *clientCodec
    32  	s   *serverCodec
    33  }
    34  
    35  func (j *jsonCodec) Close() error {
    36  	j.buf.Reset()
    37  	return j.rwc.Close()
    38  }
    39  
    40  func (j *jsonCodec) String() string {
    41  	return "json-rpc"
    42  }
    43  
    44  func (j *jsonCodec) Write(m *codec.Message, b interface{}) error {
    45  	switch m.Type {
    46  	case codec.Request:
    47  		return j.c.Write(m, b)
    48  	case codec.Response, codec.Error:
    49  		return j.s.Write(m, b)
    50  	case codec.Event:
    51  		data, err := json.Marshal(b)
    52  		if err != nil {
    53  			return err
    54  		}
    55  		_, err = j.rwc.Write(data)
    56  		return err
    57  	default:
    58  		return fmt.Errorf("Unrecognised message type: %v", m.Type)
    59  	}
    60  }
    61  
    62  func (j *jsonCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
    63  	j.buf.Reset()
    64  	j.mt = mt
    65  
    66  	switch mt {
    67  	case codec.Request:
    68  		return j.s.ReadHeader(m)
    69  	case codec.Response:
    70  		return j.c.ReadHeader(m)
    71  	case codec.Event:
    72  		_, err := io.Copy(j.buf, j.rwc)
    73  		return err
    74  	default:
    75  		return fmt.Errorf("Unrecognised message type: %v", mt)
    76  	}
    77  }
    78  
    79  func (j *jsonCodec) ReadBody(b interface{}) error {
    80  	switch j.mt {
    81  	case codec.Request:
    82  		return j.s.ReadBody(b)
    83  	case codec.Response:
    84  		return j.c.ReadBody(b)
    85  	case codec.Event:
    86  		if b != nil {
    87  			return json.Unmarshal(j.buf.Bytes(), b)
    88  		}
    89  	default:
    90  		return fmt.Errorf("Unrecognised message type: %v", j.mt)
    91  	}
    92  	return nil
    93  }
    94  
    95  func NewCodec(rwc io.ReadWriteCloser) codec.Codec {
    96  	return &jsonCodec{
    97  		buf: bytes.NewBuffer(nil),
    98  		rwc: rwc,
    99  		c:   newClientCodec(rwc),
   100  		s:   newServerCodec(rwc),
   101  	}
   102  }