ing
This commit is contained in:
parent
08773bb09b
commit
c9a94de195
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
func (sh *ServerHandlers) Init() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
sh.codecs[strings.ToLower(contentType)] = codec
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user