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 {
RegisterCodec(codec protocol.ServerCodec, contentType string)
GetCodec(contentType string) (protocol.ServerCodec, error)
Init() error
OnStart()
OnPreRead(r io.Reader)
OnPostRead(r io.Reader)
@ -18,4 +18,11 @@ type ServerHandler interface {
OnPreWriteError(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"
)
func NewServerHandler() ServerHandler {
sh := &ServerHandlers{}
return sh
}
type ServerHandlers struct {
codecs map[string]protocol.ServerCodec
}
// 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) Init() error {
return nil
}
func (sh *ServerHandlers) OnStart() {
// no op
}
func (sh *ServerHandlers) OnPreRead(r io.Reader) {
@ -48,9 +51,25 @@ func (sh *ServerHandlers) OnPostWriteError(w io.Writer, err error) {
// no op
}
func (sh *ServerHandlers) OnStop() {
// no op
}
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) {
var codec protocol.ServerCodec
if contentType == "" && len(sh.codecs) == 1 {