github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/rpc.go (about)

     1  package kit
     2  
     3  // IncomingRPCContainer defines the behavior of RPC message envelopes.
     4  // Basically in RPC communication the actual message should be contained in some kind of container.
     5  // This interface defines a set of guidelines for the implementation of those containers. The user
     6  // of the RonyKIT does not need to use this, and it is basically useful for Bundle developers.
     7  // Although even Bundle developers are not forced to use this interface in their implementation, but
     8  // they are encouraged to.
     9  //
    10  // Example implementations: common.SimpleIncomingJSONRPC
    11  type IncomingRPCContainer interface {
    12  	GetID() string
    13  	// Unmarshal deserialize the received payload.
    14  	Unmarshal(data []byte) error
    15  	// ExtractMessage extracts the payload of the container to the input message.
    16  	ExtractMessage(m Message) error
    17  	// GetHdr to read header. This method is used by RonyKIT to fill Envelope's header fields.
    18  	GetHdr(key string) string
    19  	// GetHdrMap returns all the header key-values.
    20  	GetHdrMap() map[string]string
    21  	// Release will be called when we finished our work with it.
    22  	Release()
    23  }
    24  
    25  type IncomingRPCFactory func() IncomingRPCContainer
    26  
    27  // OutgoingRPCContainer define the behavior of RPC message envelope. Similar to IncomingRPCContainer but
    28  // in another direction.
    29  //
    30  // Example implementations: common.SimpleOutgoingJSONRPC
    31  type OutgoingRPCContainer interface {
    32  	SetID(id string)
    33  	// Marshal serializes the contained message
    34  	Marshal() ([]byte, error)
    35  	// SetHdr set the header.
    36  	SetHdr(k, v string)
    37  	// InjectMessage set the body/payload of the container with the actual Message.
    38  	InjectMessage(m Message)
    39  	// Release will be called when we finished our work with it.
    40  	Release()
    41  }
    42  
    43  type OutgoingRPCFactory func() OutgoingRPCContainer
    44  
    45  // Encoding defines the encoding of the messages which will be sent/received. Gateway implementor needs
    46  // to call the correct method based on the encoding value.
    47  type Encoding struct {
    48  	tag string
    49  }
    50  
    51  func (enc Encoding) Tag() string {
    52  	return enc.tag
    53  }
    54  
    55  var (
    56  	Undefined = Encoding{}
    57  	JSON      = Encoding{tag: "json"}
    58  	Proto     = Encoding{tag: "proto"}
    59  	MSG       = Encoding{tag: "msg"}
    60  )
    61  
    62  func CustomEncoding(tag string) Encoding {
    63  	return Encoding{tag: tag}
    64  }