2017-10-26 11:14:00 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2017-11-29 03:46:40 +00:00
|
|
|
"fmt"
|
2017-10-26 11:14:00 +00:00
|
|
|
"net"
|
2017-11-29 03:46:40 +00:00
|
|
|
|
|
|
|
"git.loafle.net/commons_go/logging"
|
2017-10-26 11:14:00 +00:00
|
|
|
)
|
|
|
|
|
2017-11-15 04:16:31 +00:00
|
|
|
func NewServerHandler() ServerHandler {
|
|
|
|
sh := &ServerHandlers{}
|
|
|
|
return sh
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
type ServerHandlers struct {
|
|
|
|
// Server name for sending in response headers.
|
|
|
|
//
|
|
|
|
// Default server name is used if left blank.
|
|
|
|
Name string
|
|
|
|
|
|
|
|
SocketHandler SocketHandler
|
|
|
|
}
|
|
|
|
|
2017-11-29 00:51:41 +00:00
|
|
|
func (sh *ServerHandlers) ServerContext() ServerContext {
|
2017-11-29 00:57:03 +00:00
|
|
|
return newServerContext()
|
2017-10-27 08:53:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 00:51:41 +00:00
|
|
|
func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
|
2017-11-29 03:46:40 +00:00
|
|
|
if err := sh.SocketHandler.Init(serverCTX); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-10-27 08:53:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 00:51:41 +00:00
|
|
|
func (sh *ServerHandlers) Listen(serverCTX ServerContext) (net.Listener, error) {
|
2017-10-26 11:14:00 +00:00
|
|
|
return nil, errors.New("Server: Handler method[Listen] of Server is not implement")
|
|
|
|
}
|
|
|
|
|
2017-11-29 00:51:41 +00:00
|
|
|
func (sh *ServerHandlers) OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) {
|
2017-11-29 03:46:40 +00:00
|
|
|
logging.Logger().Error(fmt.Sprintf("Server: error status: %d, reason: %v, [%v]", status, reason, conn))
|
2017-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 04:13:47 +00:00
|
|
|
// OnStop invoked when server is stopped
|
|
|
|
// If you override ths method, must call
|
2017-11-29 00:51:41 +00:00
|
|
|
func (sh *ServerHandlers) OnStop(serverCTX ServerContext) {
|
2017-11-29 03:46:40 +00:00
|
|
|
sh.SocketHandler.Destroy()
|
2017-11-29 00:51:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-15 04:13:47 +00:00
|
|
|
func (sh *ServerHandlers) GetName() string {
|
|
|
|
return sh.Name
|
2017-10-27 05:27:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
func (sh *ServerHandlers) GetSocketHandler() SocketHandler {
|
|
|
|
return sh.SocketHandler
|
|
|
|
}
|
2017-10-26 11:14:00 +00:00
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
func (sh *ServerHandlers) Validate() {
|
|
|
|
if nil == sh.SocketHandler {
|
|
|
|
panic("Server: SocketHandler must be specified")
|
|
|
|
}
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|