rpc/protocol/json/server.go
crusader 00cff36194 ing
2018-03-23 18:53:47 +09:00

47 lines
1.2 KiB
Go

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 {
}
// 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
}