server-go/net/server.go

150 lines
3.0 KiB
Go
Raw Normal View History

2018-04-04 04:01:26 +00:00
package net
import (
"fmt"
"net"
"sync/atomic"
"time"
"git.loafle.net/commons/logging-go"
"git.loafle.net/commons/server-go"
)
2018-04-04 09:22:11 +00:00
type Server interface {
server.Server
}
func NewServer(serverHandler ServerHandler) Server {
s := &netServer{}
s.ServerHandler = serverHandler
2018-04-04 04:01:26 +00:00
2018-04-04 09:22:11 +00:00
return s
2018-04-04 04:01:26 +00:00
}
2018-04-04 09:22:11 +00:00
type netServer struct {
server.Servers
}
func (s *netServer) 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 {
return fmt.Errorf("Server: server handler must be specified")
}
if err = s.ServerHandler.Validate(); nil != err {
return err
}
2018-04-04 09:22:11 +00:00
s.ServerCtx = s.ServerHandler.ServerCtx()
if nil == s.ServerCtx {
return fmt.Errorf(s.ServerMessage("ServerCtx is nil"))
2018-04-04 04:01:26 +00:00
}
2018-04-04 09:22:11 +00:00
if err = s.ServerHandler.Init(s.ServerCtx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 09:22:11 +00:00
if listener, err = s.ServerHandler.Listener(s.ServerCtx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 09:22:11 +00:00
s.StopChan = make(chan struct{})
s.StopWg.Add(1)
2018-04-04 04:01:26 +00:00
return s.handleServer(listener)
}
2018-04-04 09:22:11 +00:00
func (s *netServer) 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 09:22:11 +00:00
s.ServerHandler.OnStop(s.ServerCtx)
2018-04-04 04:01:26 +00:00
2018-04-04 09:22:11 +00:00
logging.Logger().Infof(s.ServerMessage("Stopped"))
2018-04-04 04:01:26 +00:00
2018-04-04 09:22:11 +00:00
s.StopWg.Done()
2018-04-04 04:01:26 +00:00
}()
2018-04-04 09:22:11 +00:00
if err = s.ServerHandler.OnStart(s.ServerCtx); nil != err {
2018-04-04 04:01:26 +00:00
return err
}
2018-04-04 09:22:11 +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 09:22:11 +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 09:22:11 +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 09:22:11 +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() {
sz := s.ConnectionSize()
if sz >= s.ServerHandler.GetConcurrency() {
2018-04-04 09:22:11 +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 09:22:11 +00:00
servlet := s.ServerHandler.(ServerHandler).Servlet()
2018-04-04 04:01:26 +00:00
if nil == servlet {
2018-04-04 09:22:11 +00:00
logging.Logger().Errorf(s.ServerMessage("Servlet is nil"))
2018-04-04 04:01:26 +00:00
continue
}
2018-04-04 09:22:11 +00:00
servletCtx := servlet.ServletCtx(s.ServerCtx)
2018-04-04 04:01:26 +00:00
if nil == servletCtx {
2018-04-04 09:22:11 +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 09:22:11 +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-04 05:47:10 +00:00
conn := server.NewConn(netConn, true, s.ServerHandler.GetReadBufferSize(), s.ServerHandler.GetWriteBufferSize())
2018-04-04 04:01:26 +00:00
2018-04-04 09:22:11 +00:00
s.StopWg.Add(1)
go s.HandleConnection(servlet, servletCtx, conn)
2018-04-04 04:01:26 +00:00
}
}