2017-11-29 00:51:41 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SocketHandler interface {
|
|
|
|
SocketContext(serverCTX ServerContext) SocketContext
|
|
|
|
// Init invoked when server is stated
|
|
|
|
// If you override ths method, must call
|
|
|
|
//
|
|
|
|
// func (sh *SocketHandler) Init() error {
|
|
|
|
// if err := sh.SocketHandlers.Init(); nil != err {
|
|
|
|
// return err
|
|
|
|
// }
|
|
|
|
// ...
|
|
|
|
// ...
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
Init(serverCTX ServerContext) error
|
|
|
|
// Handshake do handshake client and server
|
|
|
|
// id is identity of client socket. if id is "", disallow connection
|
2017-12-01 08:26:02 +00:00
|
|
|
Handshake(socketCTX SocketContext, conn net.Conn) (id string)
|
2017-11-29 00:51:41 +00:00
|
|
|
// OnConnect invoked when client is connected
|
|
|
|
// If you override ths method, must call
|
|
|
|
//
|
|
|
|
// func (sh *SocketHandler) OnConnect(soc cwf.Socket) cwf.Socket {
|
|
|
|
// soc = sh.SocketHandlers.OnConnect(newSoc)
|
|
|
|
// newSoc := ...
|
|
|
|
// return newSoc
|
|
|
|
// }
|
|
|
|
OnConnect(soc Socket)
|
|
|
|
Handle(soc Socket, stopChan <-chan struct{}, doneChan chan<- error)
|
|
|
|
// OnDisconnect invoked when client is disconnected
|
|
|
|
// If you override ths method, must call
|
|
|
|
//
|
|
|
|
// func (sh *SocketHandler) OnDisconnect(soc cwf.Socket) {
|
|
|
|
// ...
|
|
|
|
// sh.SocketHandlers.OnDisconnect(soc)
|
|
|
|
// }
|
|
|
|
OnDisconnect(soc Socket)
|
|
|
|
// Destroy invoked when server is stopped
|
|
|
|
// If you override ths method, must call
|
|
|
|
//
|
|
|
|
// func (sh *SocketHandler) Destroy() {
|
|
|
|
// ...
|
|
|
|
// sh.SocketHandlers.Destroy()
|
|
|
|
// }
|
|
|
|
Destroy()
|
|
|
|
|
|
|
|
GetSocket(id string) Socket
|
|
|
|
GetSockets() map[string]Socket
|
|
|
|
|
|
|
|
GetMaxMessageSize() int64
|
|
|
|
GetWriteTimeout() time.Duration
|
|
|
|
GetReadTimeout() time.Duration
|
|
|
|
|
|
|
|
// Validate is check handler value
|
|
|
|
// If you override ths method, must call
|
|
|
|
//
|
|
|
|
// func (sh *SocketHandlers) Validate() {
|
|
|
|
// sh.SocketHandlers.Validate()
|
|
|
|
// ...
|
|
|
|
// }
|
|
|
|
Validate()
|
|
|
|
|
|
|
|
addSocket(soc Socket)
|
|
|
|
removeSocket(soc Socket)
|
|
|
|
}
|