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 {
2018-04-12 01:43:43 +00:00
return fmt.Errorf("%s already running. Stop it before starting it again", s.logHeader())
2018-04-04 04:01:26 +00:00
}
var (
err error
listener net.Listener
)
if nil == s.ServerHandler {
2018-04-12 01:43:43 +00:00
return fmt.Errorf("%s server handler must be specified", s.logHeader())
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 {
2018-04-12 01:43:43 +00:00
return fmt.Errorf("%s ServerCtx is nil", s.logHeader())
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 {
2018-04-12 01:43:43 +00:00
return fmt.Errorf("%s must be started before stopping it", s.logHeader())
2018-04-04 13:28:35 +00:00
}
close(s.stopChan)
s.stopWg.Wait()
s.ServerHandler.Destroy(s.ctx)
s.stopChan = nil
return nil
}
2018-04-12 01:43:43 +00:00
func (s *Server) logHeader() string {
return fmt.Sprintf("Server[%s]:", s.ServerHandler.GetName())
2018-04-04 13:28:35 +00:00
}
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)
2018-04-12 01:43:43 +00:00
logging.Logger().Infof("%s Stopped", s.logHeader())
2018-04-04 13:28:35 +00:00
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-12 01:43:43 +00:00
logging.Logger().Infof("%s Started", s.logHeader())
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-12 01:43:43 +00:00
logging.Logger().Errorf("%s %v", s.logHeader(), 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-12 01:43:43 +00:00
logging.Logger().Warnf("%s max connections size %d, refuse", s.logHeader(), 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-12 01:43:43 +00:00
logging.Logger().Errorf("%s Servlet is nil", s.logHeader())
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-12 01:43:43 +00:00
logging.Logger().Errorf("%s ServletCtx is nil", s.logHeader())
2018-04-04 04:01:26 +00:00
continue
}
if err := servlet.Handshake(servletCtx, netConn); nil != err {
2018-04-12 01:43:43 +00:00
logging.Logger().Infof("%s Handshaking of Client[%s] has been failed %v", s.logHeader(), 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
}
}