server-go/socket/net/server.go

168 lines
3.4 KiB
Go
Raw Normal View History

2018-04-04 04:01:26 +00:00
package net
import (
2018-04-04 13:28:35 +00:00
"context"
2018-04-04 04:01:26 +00:00
"fmt"
"net"
2018-04-04 13:28:35 +00:00
"sync"
2018-04-04 04:01:26 +00:00
"sync/atomic"
"time"
"git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go"
2018-04-05 15:15:29 +00:00
"git.loafle.net/commons/server-go/socket"
2018-04-04 04:01:26 +00:00
)
2018-04-04 13:28:35 +00:00
type Server struct {
ServerHandler ServerHandler
2018-04-04 04:01:26 +00:00
2018-04-04 13:28:35 +00:00
ctx server.ServerCtx
stopChan chan struct{}
stopWg sync.WaitGroup
2018-04-04 04:01:26 +00:00
2018-04-05 15:15:29 +00:00
srw socket.ServerReadWriter
2018-04-04 09:22:11 +00:00
}
2018-04-04 13:28:35 +00:00
func (s *Server) ListenAndServe() error {
if s.stopChan != nil {
return fmt.Errorf(s.serverMessage("already running. Stop it before starting it again"))
2018-04-04 04:01:26 +00:00
}
var (
err error
listener net.Listener
)
if nil == s.ServerHandler {
2018-04-04 16:51:34 +00:00
return fmt.Errorf(s.serverMessage("server handler must be specified"))
2018-04-04 04:01:26 +00:00
}
if err = s.ServerHandler.Validate(); nil != err {
return err
}
2018-04-04 13:28:35 +00:00
s.ctx = s.ServerHandler.ServerCtx()
if nil == s.ctx {
return fmt.Errorf(s.serverMessage("ServerCtx is nil"))
2018-04-04 04:01:26 +00:00
}
2018-04-04 13:28:35 +00:00
if err = s.ServerHandler.Init(s.ctx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 13:28:35 +00:00
if listener, err = s.ServerHandler.Listener(s.ctx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 13:28:35 +00:00
s.stopChan = make(chan struct{})
2018-04-04 13:50:34 +00:00
s.srw.ReadwriteHandler = s.ServerHandler
s.srw.ServerStopChan = s.stopChan
s.srw.ServerStopWg = &s.stopWg
2018-04-04 13:28:35 +00:00
s.stopWg.Add(1)
2018-04-04 04:01:26 +00:00
return s.handleServer(listener)
}
2018-04-04 13:28:35 +00:00
func (s *Server) Shutdown(ctx context.Context) error {
if s.stopChan == nil {
return fmt.Errorf("server must be started before stopping it")
}
close(s.stopChan)
s.stopWg.Wait()
s.ServerHandler.Destroy(s.ctx)
s.stopChan = nil
return nil
}
func (s *Server) serverMessage(msg string) string {
return fmt.Sprintf("Server[%s]: %s", s.ServerHandler.GetName(), msg)
}
func (s *Server) handleServer(listener net.Listener) error {
2018-04-04 04:01:26 +00:00
var (
stopping atomic.Value
netConn net.Conn
err error
)
defer func() {
if nil != listener {
listener.Close()
}
2018-04-04 13:28:35 +00:00
s.ServerHandler.OnStop(s.ctx)
logging.Logger().Infof(s.serverMessage("Stopped"))
s.stopWg.Done()
2018-04-04 04:01:26 +00:00
}()
2018-04-04 13:28:35 +00:00
if err = s.ServerHandler.OnStart(s.ctx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 13:28:35 +00:00
logging.Logger().Infof(s.serverMessage("Started"))
2018-04-04 04:01:26 +00:00
for {
acceptChan := make(chan struct{})
go func() {
if netConn, err = listener.Accept(); err != nil {
if nil == stopping.Load() {
2018-04-04 13:28:35 +00:00
logging.Logger().Errorf(s.serverMessage(fmt.Sprintf("%v", err)))
2018-04-04 04:01:26 +00:00
}
}
close(acceptChan)
}()
select {
2018-04-04 13:28:35 +00:00
case <-s.stopChan:
2018-04-04 04:01:26 +00:00
stopping.Store(true)
listener.Close()
<-acceptChan
listener = nil
return nil
case <-acceptChan:
}
if nil != err {
select {
2018-04-04 13:28:35 +00:00
case <-s.stopChan:
2018-04-04 04:01:26 +00:00
return nil
case <-time.After(time.Second):
}
continue
}
if 0 < s.ServerHandler.GetConcurrency() {
2018-04-04 13:50:34 +00:00
sz := s.srw.ConnectionSize()
2018-04-04 04:01:26 +00:00
if sz >= s.ServerHandler.GetConcurrency() {
2018-04-04 13:28:35 +00:00
logging.Logger().Warnf(s.serverMessage(fmt.Sprintf("max connections size %d, refuse", sz)))
2018-04-04 04:01:26 +00:00
netConn.Close()
continue
}
}
2018-04-04 16:51:34 +00:00
servlet := s.ServerHandler.(ServerHandler).Servlet(s.ctx, netConn)
2018-04-04 04:01:26 +00:00
if nil == servlet {
2018-04-04 13:28:35 +00:00
logging.Logger().Errorf(s.serverMessage("Servlet is nil"))
2018-04-04 04:01:26 +00:00
continue
}
2018-04-04 13:28:35 +00:00
servletCtx := servlet.ServletCtx(s.ctx)
2018-04-04 04:01:26 +00:00
if nil == servletCtx {
2018-04-04 13:28:35 +00:00
logging.Logger().Errorf(s.serverMessage("ServletCtx is nil"))
2018-04-04 04:01:26 +00:00
continue
}
if err := servlet.Handshake(servletCtx, netConn); nil != err {
2018-04-04 13:28:35 +00:00
logging.Logger().Infof(s.serverMessage(fmt.Sprintf("Handshaking of Client[%s] has been failed %v", netConn.RemoteAddr(), err)))
2018-04-04 04:01:26 +00:00
continue
}
2018-04-05 15:15:29 +00:00
conn := socket.NewConn(netConn, true, s.ServerHandler.GetReadBufferSize(), s.ServerHandler.GetWriteBufferSize())
2018-04-04 04:01:26 +00:00
2018-04-04 13:28:35 +00:00
s.stopWg.Add(1)
2018-04-04 13:50:34 +00:00
go s.srw.HandleConnection(servlet, servletCtx, conn)
2018-04-04 04:01:26 +00:00
}
}