This commit is contained in:
crusader 2017-11-15 14:45:08 +09:00
parent 08773bb09b
commit c9a94de195
2 changed files with 38 additions and 12 deletions

View File

@ -7,8 +7,8 @@ import (
) )
type ServerHandler interface { type ServerHandler interface {
RegisterCodec(codec protocol.ServerCodec, contentType string) Init() error
GetCodec(contentType string) (protocol.ServerCodec, error) OnStart()
OnPreRead(r io.Reader) OnPreRead(r io.Reader)
OnPostRead(r io.Reader) OnPostRead(r io.Reader)
@ -18,4 +18,11 @@ type ServerHandler interface {
OnPreWriteError(w io.Writer, err error) OnPreWriteError(w io.Writer, err error)
OnPostWriteError(w io.Writer, err error) OnPostWriteError(w io.Writer, err error)
OnStop()
Validate()
RegisterCodec(codec protocol.ServerCodec, contentType string)
GetCodec(contentType string) (protocol.ServerCodec, error)
} }

View File

@ -8,20 +8,23 @@ import (
"git.loafle.net/commons_go/rpc/protocol" "git.loafle.net/commons_go/rpc/protocol"
) )
func NewServerHandler() ServerHandler {
sh := &ServerHandlers{}
return sh
}
type ServerHandlers struct { type ServerHandlers struct {
codecs map[string]protocol.ServerCodec codecs map[string]protocol.ServerCodec
} }
// RegisterCodec adds a new codec to the server. func (sh *ServerHandlers) Init() error {
//
// Codecs are defined to process a given serialization scheme, e.g., JSON or return nil
// XML. A codec is chosen based on the "Content-Type" header from the request, }
// excluding the charset definition.
func (sh *ServerHandlers) RegisterCodec(codec protocol.ServerCodec, contentType string) { func (sh *ServerHandlers) OnStart() {
if nil == sh.codecs { // no op
sh.codecs = make(map[string]protocol.ServerCodec)
}
sh.codecs[strings.ToLower(contentType)] = codec
} }
func (sh *ServerHandlers) OnPreRead(r io.Reader) { func (sh *ServerHandlers) OnPreRead(r io.Reader) {
@ -48,9 +51,25 @@ func (sh *ServerHandlers) OnPostWriteError(w io.Writer, err error) {
// no op // no op
} }
func (sh *ServerHandlers) OnStop() {
// no op
}
func (sh *ServerHandlers) Validate() { func (sh *ServerHandlers) Validate() {
} }
// RegisterCodec adds a new codec to the server.
//
// Codecs are defined to process a given serialization scheme, e.g., JSON or
// XML. A codec is chosen based on the "Content-Type" header from the request,
// excluding the charset definition.
func (sh *ServerHandlers) RegisterCodec(codec protocol.ServerCodec, contentType string) {
if nil == sh.codecs {
sh.codecs = make(map[string]protocol.ServerCodec)
}
sh.codecs[strings.ToLower(contentType)] = codec
}
func (sh *ServerHandlers) GetCodec(contentType string) (protocol.ServerCodec, error) { func (sh *ServerHandlers) GetCodec(contentType string) (protocol.ServerCodec, error) {
var codec protocol.ServerCodec var codec protocol.ServerCodec
if contentType == "" && len(sh.codecs) == 1 { if contentType == "" && len(sh.codecs) == 1 {