server-go/socket/net/server-handler.go
2018-08-22 17:37:12 +09:00

95 lines
1.8 KiB
Go

package net
import (
"net"
"sync/atomic"
"git.loafle.net/overflow/server-go"
"git.loafle.net/overflow/server-go/socket"
)
type ServerHandler interface {
socket.ServerHandler
OnError(serverCtx server.ServerCtx, conn net.Conn, status int, reason error)
RegisterServlet(servlet Servlet)
Servlet(serverCtx server.ServerCtx, conn net.Conn) Servlet
}
type ServerHandlers struct {
socket.ServerHandlers
servlet Servlet
validated atomic.Value
}
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
}
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)
}
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
}
func (sh *ServerHandlers) Servlet(serverCtx server.ServerCtx, conn net.Conn) Servlet {
return sh.servlet
}
func (sh *ServerHandlers) Validate() error {
if nil != sh.validated.Load() {
return nil
}
sh.validated.Store(true)
if err := sh.ServerHandlers.Validate(); nil != err {
return err
}
return nil
}