server-go/socket/net/server-handler.go

95 lines
1.8 KiB
Go
Raw Normal View History

2018-04-04 04:01:26 +00:00
package net
import (
"net"
2018-04-13 07:11:43 +00:00
"sync/atomic"
2018-04-04 04:01:26 +00:00
"git.loafle.net/commons/server-go"
2018-04-05 15:15:29 +00:00
"git.loafle.net/commons/server-go/socket"
2018-04-04 04:01:26 +00:00
)
type ServerHandler interface {
2018-04-05 15:15:29 +00:00
socket.ServerHandler
2018-04-04 04:01:26 +00:00
OnError(serverCtx server.ServerCtx, conn net.Conn, status int, reason error)
RegisterServlet(servlet Servlet)
2018-04-04 16:51:34 +00:00
Servlet(serverCtx server.ServerCtx, conn net.Conn) Servlet
2018-04-04 04:01:26 +00:00
}
type ServerHandlers struct {
2018-04-06 00:28:42 +00:00
socket.ServerHandlers
2018-04-04 04:01:26 +00:00
servlet Servlet
2018-04-13 07:11:43 +00:00
validated atomic.Value
2018-04-04 04:01:26 +00:00
}
func (sh *ServerHandlers) Init(serverCtx server.ServerCtx) error {
if err := sh.ServerHandlers.Init(serverCtx); nil != err {
return err
}
if nil != sh.servlet {
if err := sh.servlet.Init(serverCtx); nil != err {
return err
}
}
return nil
}
2018-04-09 12:48:14 +00:00
func (sh *ServerHandlers) OnStart(serverCtx server.ServerCtx) error {
if err := sh.ServerHandlers.OnStart(serverCtx); nil != err {
return err
}
if nil != sh.servlet {
if err := sh.servlet.OnStart(serverCtx); nil != err {
return err
}
}
return nil
}
func (sh *ServerHandlers) OnStop(serverCtx server.ServerCtx) {
if nil != sh.servlet {
sh.servlet.OnStop(serverCtx)
}
sh.ServerHandlers.OnStop(serverCtx)
}
2018-04-04 04:01:26 +00:00
func (sh *ServerHandlers) Destroy(serverCtx server.ServerCtx) {
if nil != sh.servlet {
sh.servlet.Destroy(serverCtx)
}
sh.ServerHandlers.Destroy(serverCtx)
}
func (sh *ServerHandlers) OnError(serverCtx server.ServerCtx, conn net.Conn, status int, reason error) {
}
func (sh *ServerHandlers) RegisterServlet(servlet Servlet) {
sh.servlet = servlet
}
2018-04-04 16:51:34 +00:00
func (sh *ServerHandlers) Servlet(serverCtx server.ServerCtx, conn net.Conn) Servlet {
2018-04-04 04:01:26 +00:00
return sh.servlet
}
func (sh *ServerHandlers) Validate() error {
2018-04-13 07:11:43 +00:00
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
2018-04-04 04:01:26 +00:00
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}
return nil
}