server/server_handlers.go
crusader 089d12de4f ing
2017-11-29 14:11:11 +09:00

68 lines
1.5 KiB
Go

package server
import (
"errors"
"fmt"
"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().Error(fmt.Sprintf("Server: error status: %d, reason: %v, [%v]", status, reason, conn))
}
// OnStop invoked when server is stopped
// If you override ths method, must call
func (sh *ServerHandlers) OnStop(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 {
panic("Server: SocketHandler must be specified")
}
}