package json import ( "encoding/json" "io" "git.loafle.net/commons_go/rpc/protocol" ) var null = json.RawMessage([]byte("null")) // ---------------------------------------------------------------------------- // Codec // ---------------------------------------------------------------------------- // NewServerCodec returns a new JSON Codec. func NewServerCodec() protocol.ServerCodec { return &ServerCodec{} } // ServerCodec creates a ServerRequestCodec to process each request. type ServerCodec struct { } func (sc *ServerCodec) NewRequestB(buf []byte) (protocol.ServerRequestCodec, error) { return newServerRequestCodecB(buf) } func (sc *ServerCodec) NewNotificationB(method string, args []interface{}) ([]byte, error) { params, err := convertParamsToStringArray(args) if nil != err { return nil, err } noti := &serverNotification{Method: method, Params: params} res := &serverResponse{Version: Version, Result: noti} return json.Marshal(res) } // NewRequest returns a ServerRequestCodec. func (sc *ServerCodec) NewRequest(r io.Reader) (protocol.ServerRequestCodec, error) { return newServerRequestCodec(r) } // WriteNotification send a notification from server to client. func (sc *ServerCodec) WriteNotification(w io.Writer, method string, args []interface{}) error { params, err := convertParamsToStringArray(args) if nil != err { return err } noti := &serverNotification{Method: method, Params: params} res := &serverResponse{Version: Version, Result: noti} encoder := json.NewEncoder(w) // Not sure in which case will this happen. But seems harmless. if err := encoder.Encode(res); nil != err { return err } return nil }