diff --git a/connection/websocket/fasthttp/servlet_handlers.go b/connection/websocket/fasthttp/servlet_handlers.go index d14b518..9e74b86 100644 --- a/connection/websocket/fasthttp/servlet_handlers.go +++ b/connection/websocket/fasthttp/servlet_handlers.go @@ -3,6 +3,7 @@ package fasthttp import ( "fmt" + "git.loafle.net/commons_go/rpc" "git.loafle.net/commons_go/rpc/protocol" "git.loafle.net/commons_go/websocket_fasthttp/websocket" ) @@ -10,7 +11,7 @@ import ( type ServletHandlers struct { } -func (sh *ServletHandlers) GetRequest(codec protocol.ServerCodec, reader interface{}) (protocol.ServerRequestCodec, error) { +func (sh *ServletHandlers) GetRequest(servletCTX rpc.ServletContext, codec protocol.ServerCodec, reader interface{}) (protocol.ServerRequestCodec, error) { conn := reader.(*websocket.Conn) _, r, err := conn.NextReader() @@ -19,7 +20,7 @@ func (sh *ServletHandlers) GetRequest(codec protocol.ServerCodec, reader interfa return requestCodec, err } -func (sh *ServletHandlers) SendResponse(requestCodec protocol.ServerRequestCodec, writer interface{}, result interface{}, err error) error { +func (sh *ServletHandlers) SendResponse(servletCTX rpc.ServletContext, requestCodec protocol.ServerRequestCodec, writer interface{}, result interface{}, err error) error { conn := writer.(*websocket.Conn) wc, lerr := conn.NextWriter(websocket.TextMessage) @@ -40,7 +41,7 @@ func (sh *ServletHandlers) SendResponse(requestCodec protocol.ServerRequestCodec return fmt.Errorf("Servlet Handler: SendResponse is not implemented") } -func (sh *ServletHandlers) SendNotification(codec protocol.ServerCodec, writer interface{}, method string, args ...interface{}) error { +func (sh *ServletHandlers) SendNotification(servletCTX rpc.ServletContext, codec protocol.ServerCodec, writer interface{}, method string, args ...interface{}) error { conn := writer.(*websocket.Conn) wc, lerr := conn.NextWriter(websocket.TextMessage) diff --git a/constants.go b/constants.go index d38302b..4cd6c6c 100644 --- a/constants.go +++ b/constants.go @@ -1,5 +1,11 @@ package rpc +import cuc "git.loafle.net/commons_go/util/context" + const ( DefaultPendingMessages = 32 * 1024 ) + +var ( + ContentTypeKey = cuc.ContextKey("ContentType") +) diff --git a/servlet.go b/servlet.go index 8679885..db24038 100644 --- a/servlet.go +++ b/servlet.go @@ -6,16 +6,17 @@ import ( "git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/rpc/protocol" + cuc "git.loafle.net/commons_go/util/context" ) -func NewServlet(servletCTX ServletContext, sh ServletHandler) Servlet { +func NewServlet(sh ServletHandler) Servlet { return &servlet{ sh: sh, } } type Servlet interface { - Start(contentType string, reader interface{}, writer interface{}) error + Start(parentCTX cuc.Context, reader interface{}, writer interface{}) error Stop() Send(method string, args ...interface{}) (err error) @@ -28,7 +29,6 @@ type servlet struct { sh ServletHandler messageQueueChan chan *messageState - contentType string reader interface{} writer interface{} serverCodec protocol.ServerCodec @@ -37,7 +37,7 @@ type servlet struct { stopWg sync.WaitGroup } -func (s *servlet) Start(contentType string, reader interface{}, writer interface{}) error { +func (s *servlet) Start(parentCTX cuc.Context, reader interface{}, writer interface{}) error { if nil == s.sh { panic("Servlet: servlet handler must be specified.") } @@ -46,13 +46,13 @@ func (s *servlet) Start(contentType string, reader interface{}, writer interface if s.stopChan != nil { return fmt.Errorf("Servlet: servlet is already running. Stop it before starting it again") } + servletCTX := s.sh.ServletContext(parentCTX) - sc, err := s.sh.getCodec(contentType) + sc, err := s.sh.getCodec(servletCTX.GetAttribute(ContentTypeKey).(string)) if nil != err { return err } - s.contentType = contentType s.reader = reader s.writer = writer s.serverCodec = sc @@ -81,7 +81,6 @@ func (s *servlet) Stop() { s.messageQueueChan = nil - s.contentType = "" s.reader = nil s.writer = nil s.serverCodec = nil diff --git a/servlet_context.go b/servlet_context.go index 606eb51..391f340 100644 --- a/servlet_context.go +++ b/servlet_context.go @@ -12,7 +12,7 @@ type servletContext struct { cuc.Context } -func NewServletContext(parent cuc.Context) ServletContext { +func newServletContext(parent cuc.Context) ServletContext { sCTX := &servletContext{} sCTX.Context = cuc.NewContext(parent) diff --git a/servlet_handler.go b/servlet_handler.go index 6ad7776..d8f1d4a 100644 --- a/servlet_handler.go +++ b/servlet_handler.go @@ -1,8 +1,13 @@ package rpc -import "git.loafle.net/commons_go/rpc/protocol" +import ( + "git.loafle.net/commons_go/rpc/protocol" + cuc "git.loafle.net/commons_go/util/context" +) type ServletHandler interface { + ServletContext(parent cuc.Context) ServletContext + Init(servletCTX ServletContext) error GetRequest(servletCTX ServletContext, codec protocol.ServerCodec, reader interface{}) (protocol.ServerRequestCodec, error) diff --git a/servlet_handlers.go b/servlet_handlers.go index 6bb4250..57a8063 100644 --- a/servlet_handlers.go +++ b/servlet_handlers.go @@ -5,6 +5,7 @@ import ( "strings" "git.loafle.net/commons_go/rpc/protocol" + cuc "git.loafle.net/commons_go/util/context" ) type ServletHandlers struct { @@ -20,6 +21,10 @@ type ServletHandlers struct { codecs map[string]protocol.ServerCodec } +func (sh *ServletHandlers) ServletContext(parent cuc.Context) ServletContext { + return newServletContext(parent) +} + func (sh *ServletHandlers) Init(servletCTX ServletContext) error { return nil }