ing
This commit is contained in:
parent
83feb109dc
commit
4522a80579
|
@ -19,11 +19,10 @@ type Server interface {
|
||||||
Context() ServerContext
|
Context() ServerContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(serverCTX ServerContext, sh ServerHandler) Server {
|
func New(sh ServerHandler) Server {
|
||||||
s := &server{
|
s := &server{
|
||||||
sh: sh,
|
sh: sh,
|
||||||
}
|
}
|
||||||
s.ctx = serverCTX
|
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
@ -74,6 +73,7 @@ func (s *server) Start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
s.ctx = s.sh.ServerContext()
|
||||||
|
|
||||||
if err = s.sh.Init(s.ctx); nil != err {
|
if err = s.sh.Init(s.ctx); nil != err {
|
||||||
logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err))
|
logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err))
|
||||||
|
@ -153,7 +153,8 @@ func (s *server) handleRequest(ctx *fasthttp.RequestCtx) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
soc := newSocket(s.ctx, socketID, conn, socketHandler)
|
socketCTX := socketHandler.SocketContext(s.ctx)
|
||||||
|
soc := newSocket(socketHandler, socketCTX, conn, socketID)
|
||||||
|
|
||||||
s.stopWg.Add(1)
|
s.stopWg.Add(1)
|
||||||
handleConnection(s, soc, socketHandler)
|
handleConnection(s, soc, socketHandler)
|
||||||
|
|
|
@ -12,9 +12,9 @@ type serverContext struct {
|
||||||
cuc.Context
|
cuc.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServerContext(parent cuc.Context) ServerContext {
|
func newServerContext() ServerContext {
|
||||||
sCTX := &serverContext{}
|
sCTX := &serverContext{}
|
||||||
sCTX.Context = cuc.NewContext(parent)
|
sCTX.Context = cuc.NewContext(nil)
|
||||||
|
|
||||||
return sCTX
|
return sCTX
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ServerHandler interface {
|
type ServerHandler interface {
|
||||||
|
ServerContext() ServerContext
|
||||||
// Init invoked before the server is started
|
// Init invoked before the server is started
|
||||||
// If you override ths method, must call
|
// If you override ths method, must call
|
||||||
//
|
//
|
||||||
|
|
|
@ -55,6 +55,10 @@ type ServerHandlers struct {
|
||||||
socketHandlers map[string]SocketHandler
|
socketHandlers map[string]SocketHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sh *ServerHandlers) ServerContext() ServerContext {
|
||||||
|
return newServerContext()
|
||||||
|
}
|
||||||
|
|
||||||
func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
|
func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
|
||||||
if nil != sh.socketHandlers {
|
if nil != sh.socketHandlers {
|
||||||
for _, socketHandler := range sh.socketHandlers {
|
for _, socketHandler := range sh.socketHandlers {
|
||||||
|
|
12
socket.go
12
socket.go
|
@ -147,16 +147,16 @@ type Socket interface {
|
||||||
Context() SocketContext
|
Context() SocketContext
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSocket(serverCTX ServerContext, id string, conn *websocket.Conn, sh SocketHandler) Socket {
|
func newSocket(socketHandler SocketHandler, socketCTX SocketContext, conn *websocket.Conn, id string) Socket {
|
||||||
s := retainSocket()
|
s := retainSocket()
|
||||||
s.Conn = conn
|
s.Conn = conn
|
||||||
s.sh = sh
|
s.sh = socketHandler
|
||||||
s.id = id
|
s.id = id
|
||||||
s.SetReadLimit(sh.GetMaxMessageSize())
|
s.SetReadLimit(socketHandler.GetMaxMessageSize())
|
||||||
if 0 < sh.GetReadTimeout() {
|
if 0 < socketHandler.GetReadTimeout() {
|
||||||
s.SetReadDeadline(time.Now().Add(sh.GetReadTimeout() * time.Second))
|
s.SetReadDeadline(time.Now().Add(socketHandler.GetReadTimeout() * time.Second))
|
||||||
}
|
}
|
||||||
s.ctx = newSocketContext(serverCTX)
|
s.ctx = socketCTX
|
||||||
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,7 @@ type SocketHandler interface {
|
||||||
// Handshake do handshake client and server
|
// Handshake do handshake client and server
|
||||||
// id is identity of client socket. if id is "", disallow connection
|
// id is identity of client socket. if id is "", disallow connection
|
||||||
Handshake(serverCTX ServerContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader)
|
Handshake(serverCTX ServerContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader)
|
||||||
|
SocketContext(serverCTX ServerContext) SocketContext
|
||||||
// OnConnect invoked when client is connected
|
// OnConnect invoked when client is connected
|
||||||
// If you override ths method, must call
|
// If you override ths method, must call
|
||||||
//
|
//
|
||||||
|
|
|
@ -35,10 +35,14 @@ func (sh *SocketHandlers) Init(serverCTX ServerContext) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sh *SocketHandlers) Handshake(ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) {
|
func (sh *SocketHandlers) Handshake(serverCTX ServerContext, ctx *fasthttp.RequestCtx) (id string, extensionsHeader *fasthttp.ResponseHeader) {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (sh *SocketHandlers) SocketContext(serverCTX ServerContext) SocketContext {
|
||||||
|
return newSocketContext(serverCTX)
|
||||||
|
}
|
||||||
|
|
||||||
func (sh *SocketHandlers) OnConnect(soc Socket) Socket {
|
func (sh *SocketHandlers) OnConnect(soc Socket) Socket {
|
||||||
|
|
||||||
return soc
|
return soc
|
||||||
|
|
Loading…
Reference in New Issue
Block a user