2017-10-26 11:14:00 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.loafle.net/commons_go/logging"
|
|
|
|
)
|
|
|
|
|
2017-11-01 06:24:05 +00:00
|
|
|
func New(sh ServerHandler) Server {
|
2017-10-26 12:13:48 +00:00
|
|
|
s := &server{
|
2017-10-31 10:46:32 +00:00
|
|
|
sh: sh,
|
2017-10-26 12:13:48 +00:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-11-15 04:13:47 +00:00
|
|
|
type Server interface {
|
|
|
|
Start() error
|
|
|
|
Stop()
|
2017-11-29 01:01:48 +00:00
|
|
|
Context() ServerContext
|
2017-11-15 04:13:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 11:14:00 +00:00
|
|
|
type server struct {
|
2017-11-29 01:01:48 +00:00
|
|
|
ctx ServerContext
|
|
|
|
|
2017-10-31 10:46:32 +00:00
|
|
|
sh ServerHandler
|
2017-10-26 11:14:00 +00:00
|
|
|
|
|
|
|
listener net.Listener
|
|
|
|
|
2017-11-01 05:22:52 +00:00
|
|
|
Stats ServerStats
|
2017-10-26 11:14:00 +00:00
|
|
|
|
|
|
|
stopChan chan struct{}
|
|
|
|
stopWg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Start() error {
|
2017-10-31 10:46:32 +00:00
|
|
|
if nil == s.sh {
|
2017-10-26 11:14:00 +00:00
|
|
|
panic("Server: server handler must be specified.")
|
|
|
|
}
|
2017-10-31 10:46:32 +00:00
|
|
|
s.sh.Validate()
|
2017-10-26 11:14:00 +00:00
|
|
|
|
|
|
|
if s.stopChan != nil {
|
|
|
|
panic("Server: server is already running. Stop it before starting it again")
|
|
|
|
}
|
|
|
|
var err error
|
2017-11-29 01:01:48 +00:00
|
|
|
s.ctx = s.sh.ServerContext()
|
2017-10-26 11:14:00 +00:00
|
|
|
|
2017-11-29 01:01:48 +00:00
|
|
|
if err = s.sh.Init(s.ctx); nil != err {
|
2017-11-15 04:13:47 +00:00
|
|
|
logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err))
|
|
|
|
}
|
|
|
|
|
2017-11-29 01:01:48 +00:00
|
|
|
if s.listener, err = s.sh.Listen(s.ctx); nil != err {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-01 05:22:52 +00:00
|
|
|
|
2017-11-29 01:01:48 +00:00
|
|
|
s.stopChan = make(chan struct{})
|
2017-10-27 08:53:26 +00:00
|
|
|
|
2017-10-26 11:14:00 +00:00
|
|
|
s.stopWg.Add(1)
|
2017-11-01 05:22:52 +00:00
|
|
|
go handleServer(s)
|
2017-10-27 08:53:26 +00:00
|
|
|
|
2017-10-26 11:14:00 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) Stop() {
|
|
|
|
if s.stopChan == nil {
|
|
|
|
panic("Server: server must be started before stopping it")
|
|
|
|
}
|
|
|
|
close(s.stopChan)
|
|
|
|
s.stopWg.Wait()
|
|
|
|
s.stopChan = nil
|
2017-11-15 04:13:47 +00:00
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
s.sh.OnStop(s.ctx)
|
2017-11-15 04:13:47 +00:00
|
|
|
|
|
|
|
logging.Logger().Info(fmt.Sprintf("Server[%s] is stopped", s.sh.GetName()))
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
func (s *server) Context() ServerContext {
|
|
|
|
return s.ctx
|
|
|
|
}
|
|
|
|
|
2017-11-01 05:22:52 +00:00
|
|
|
func handleServer(s *server) {
|
2017-10-26 11:14:00 +00:00
|
|
|
defer s.stopWg.Done()
|
|
|
|
|
2017-11-29 01:01:48 +00:00
|
|
|
logging.Logger().Info(fmt.Sprintf("Server[%s] is started", s.sh.GetName()))
|
|
|
|
s.sh.OnStart(s.ctx)
|
|
|
|
|
2017-11-01 05:22:52 +00:00
|
|
|
var conn net.Conn
|
2017-10-26 11:14:00 +00:00
|
|
|
var err error
|
|
|
|
var stopping atomic.Value
|
|
|
|
|
|
|
|
for {
|
|
|
|
acceptChan := make(chan struct{})
|
|
|
|
go func() {
|
2017-11-01 05:22:52 +00:00
|
|
|
if conn, err = s.listener.Accept(); err != nil {
|
2017-10-26 11:14:00 +00:00
|
|
|
if stopping.Load() == nil {
|
2017-11-09 06:15:00 +00:00
|
|
|
logging.Logger().Error(fmt.Sprintf("Server: Cannot accept new connection: [%s]", err))
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(acceptChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.stopChan:
|
|
|
|
stopping.Store(true)
|
|
|
|
s.listener.Close()
|
|
|
|
<-acceptChan
|
|
|
|
return
|
|
|
|
case <-acceptChan:
|
|
|
|
s.Stats.incAcceptCalls()
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
s.Stats.incAcceptErrors()
|
|
|
|
select {
|
|
|
|
case <-s.stopChan:
|
|
|
|
return
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
var socketID string
|
|
|
|
socketHandler := s.sh.GetSocketHandler()
|
|
|
|
|
|
|
|
if socketID = socketHandler.Handshake(s.ctx, conn); "" == socketID {
|
|
|
|
logging.Logger().Error(fmt.Sprintf("Server: Handshake err addr[%s] %v", conn.RemoteAddr(), conn))
|
|
|
|
conn.Close()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
socketCTX := socketHandler.SocketContext(s.ctx)
|
|
|
|
soc := newSocket(socketHandler, socketCTX, conn, socketID)
|
|
|
|
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", soc.RemoteAddr()))
|
|
|
|
|
|
|
|
socketHandler.addSocket(soc)
|
|
|
|
socketHandler.OnConnect(soc)
|
|
|
|
|
2017-10-26 11:14:00 +00:00
|
|
|
s.stopWg.Add(1)
|
2017-11-29 03:46:40 +00:00
|
|
|
go handleConnection(s, soc, socketHandler)
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
func handleConnection(s *server, soc Socket, socketHandler SocketHandler) {
|
2017-10-26 11:14:00 +00:00
|
|
|
defer s.stopWg.Done()
|
2017-10-27 07:14:20 +00:00
|
|
|
|
2017-10-27 05:33:35 +00:00
|
|
|
clientStopChan := make(chan struct{})
|
2017-11-29 03:46:40 +00:00
|
|
|
handleDoneChan := make(chan error, 1)
|
2017-11-15 04:13:47 +00:00
|
|
|
|
2017-11-29 03:46:40 +00:00
|
|
|
go socketHandler.Handle(soc, clientStopChan, handleDoneChan)
|
2017-10-26 11:14:00 +00:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.stopChan:
|
2017-10-27 05:33:35 +00:00
|
|
|
close(clientStopChan)
|
2017-11-07 02:57:26 +00:00
|
|
|
<-handleDoneChan
|
|
|
|
case <-handleDoneChan:
|
2017-11-01 05:22:52 +00:00
|
|
|
close(clientStopChan)
|
2017-11-29 03:46:40 +00:00
|
|
|
socketHandler.OnDisconnect(soc)
|
|
|
|
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", soc.RemoteAddr()))
|
|
|
|
socketHandler.removeSocket(soc)
|
|
|
|
soc.Close()
|
2017-10-26 11:14:00 +00:00
|
|
|
}
|
|
|
|
}
|