server/server_handlers.go
crusader a1cf7963b5 ing
2018-03-27 15:43:21 +09:00

71 lines
1.5 KiB
Go

package server
import (
"errors"
"net"
"git.loafle.net/commons_go/logging"
)
func NewServerHandler() ServerHandler {
sh := &ServerHandlers{}
return sh
}
type ServerHandlers struct {
// Server name for sending in response headers.
//
// Default server name is used if left blank.
Name string
SocketHandler SocketHandler
}
func (sh *ServerHandlers) ServerContext() ServerContext {
return newServerContext()
}
func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
if err := sh.SocketHandler.Init(serverCTX); nil != err {
return err
}
return nil
}
func (sh *ServerHandlers) Listen(serverCTX ServerContext) (net.Listener, error) {
return nil, errors.New("Server: Handler method[Listen] of Server is not implement")
}
func (sh *ServerHandlers) OnStart(serverCTX ServerContext) {
// no op
}
func (sh *ServerHandlers) OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) {
logging.Logger().Errorf("Server: error status: %d, reason: %v, [%v]", status, reason, conn)
}
func (sh *ServerHandlers) OnStop(serverCTX ServerContext) {
// no op
}
// Destroy invoked when server is stopped
// If you override ths method, must call
func (sh *ServerHandlers) Destroy(serverCTX ServerContext) {
sh.SocketHandler.Destroy()
}
func (sh *ServerHandlers) GetName() string {
return sh.Name
}
func (sh *ServerHandlers) GetSocketHandler() SocketHandler {
return sh.SocketHandler
}
func (sh *ServerHandlers) Validate() {
if nil == sh.SocketHandler {
logging.Logger().Panicf("Server: SocketHandler must be specified")
}
}