rpc/protocol/json/server.go

88 lines
2.3 KiB
Go
Raw Normal View History

2017-10-25 14:52:47 +00:00
package json
import (
"encoding/json"
2017-10-26 07:21:35 +00:00
"io"
2017-10-31 09:25:44 +00:00
"sync"
2017-10-26 07:21:35 +00:00
2017-11-26 10:15:51 +00:00
"git.loafle.net/commons_go/rpc/codec"
2017-10-26 07:21:35 +00:00
"git.loafle.net/commons_go/rpc/protocol"
2017-10-25 14:52:47 +00:00
)
var null = json.RawMessage([]byte("null"))
2017-11-26 10:15:51 +00:00
type serverMessage struct {
2017-10-25 14:52:47 +00:00
// JSON-RPC protocol.
2017-11-26 10:15:51 +00:00
Version string `json:"jsonrpc"`
MessageType protocol.MessageType `json:"messageType"`
Message interface{} `json:"message"`
2017-10-25 14:52:47 +00:00
}
// ----------------------------------------------------------------------------
// Codec
// ----------------------------------------------------------------------------
2017-10-31 09:25:44 +00:00
// NewCustomServerCodec returns a new JSON Codec based on passed encoder selector.
2017-11-26 10:15:51 +00:00
func NewCustomServerCodec(codecSel codec.CodecSelector) protocol.ServerCodec {
return &ServerCodec{codecSel: codecSel}
2017-10-25 14:52:47 +00:00
}
2017-10-31 09:25:44 +00:00
// NewServerCodec returns a new JSON Codec.
2017-11-26 10:15:51 +00:00
func NewServerCodec() protocol.ServerCodec {
return NewCustomServerCodec(codec.DefaultCodecSelector)
2017-10-25 14:52:47 +00:00
}
2017-11-26 10:15:51 +00:00
// ServerCodec creates a ServerRequestCodec to process each request.
2017-10-31 09:25:44 +00:00
type ServerCodec struct {
2017-11-26 10:15:51 +00:00
codecSel codec.CodecSelector
2017-10-25 14:52:47 +00:00
}
2017-11-26 10:15:51 +00:00
// NewRequest returns a ServerRequestCodec.
func (sc *ServerCodec) NewRequest(r io.Reader) (protocol.ServerRequestCodec, error) {
return newServerRequestCodec(r, sc.codecSel.SelectByReader(r))
2017-10-25 14:52:47 +00:00
}
2017-11-26 10:15:51 +00:00
// WriteNotification send a notification from server to client.
func (sc *ServerCodec) WriteNotification(w io.Writer, method string, args interface{}) error {
noti := retainServerNotification(method, args)
2017-10-31 09:25:44 +00:00
defer func() {
2017-11-26 10:15:51 +00:00
releaseServerNotification(noti)
2017-10-31 09:25:44 +00:00
}()
2017-11-26 10:15:51 +00:00
msg := retainServerMessage(protocol.MessageTypeNotification, noti)
defer func() {
releaseServerMessage(msg)
}()
encoder := json.NewEncoder(sc.codecSel.SelectByWriter(w).Encode(w))
// Not sure in which case will this happen. But seems harmless.
if err := encoder.Encode(msg); nil != err {
return err
2017-10-25 14:52:47 +00:00
}
2017-10-26 07:21:35 +00:00
return nil
2017-10-25 14:52:47 +00:00
}
2017-11-26 10:15:51 +00:00
var serverMessagePool sync.Pool
2017-10-31 09:25:44 +00:00
2017-11-26 10:15:51 +00:00
func retainServerMessage(msgType protocol.MessageType, msg interface{}) *serverMessage {
var sm *serverMessage
v := serverMessagePool.Get()
2017-10-31 09:25:44 +00:00
if v == nil {
2017-11-26 10:15:51 +00:00
sm = &serverMessage{}
2017-11-01 07:55:51 +00:00
} else {
2017-11-26 10:15:51 +00:00
sm = v.(*serverMessage)
2017-10-31 09:25:44 +00:00
}
2017-11-01 07:55:51 +00:00
2017-11-26 10:15:51 +00:00
sm.Version = Version
sm.MessageType = msgType
sm.Message = msg
2017-10-31 09:25:44 +00:00
2017-11-26 10:15:51 +00:00
return sm
2017-10-31 09:25:44 +00:00
}
2017-11-26 10:15:51 +00:00
func releaseServerMessage(sm *serverMessage) {
sm.Version = ""
sm.MessageType = protocol.MessageTypeUnknown
sm.Message = nil
2017-10-31 09:25:44 +00:00
2017-11-26 10:15:51 +00:00
serverMessagePool.Put(sm)
2017-10-31 09:25:44 +00:00
}