server/server.go

147 lines
2.6 KiB
Go
Raw Normal View History

2017-10-26 11:14:00 +00:00
package server
import (
"fmt"
"io"
"net"
"sync"
"sync/atomic"
"time"
"git.loafle.net/commons_go/logging"
)
type Server interface {
Start() error
Stop()
Serve() error
}
2017-10-26 12:13:48 +00:00
func NewServer(sh ServerHandler) Server {
s := &server{
2017-10-31 10:46:32 +00:00
sh: sh,
2017-10-26 12:13:48 +00:00
}
return s
}
2017-10-26 11:14:00 +00:00
type server struct {
2017-10-31 10:46:32 +00:00
sh ServerHandler
2017-10-26 11:14:00 +00:00
listener net.Listener
2017-10-26 12:08:50 +00:00
Stats ConnStats
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")
}
s.stopChan = make(chan struct{})
var err error
2017-10-31 10:46:32 +00:00
if s.listener, err = s.sh.Listen(); nil != err {
2017-10-26 11:14:00 +00:00
return err
}
2017-10-31 10:46:32 +00:00
s.sh.OnStart()
2017-10-27 08:53:26 +00:00
2017-10-26 11:14:00 +00:00
s.stopWg.Add(1)
go runServer(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-10-31 10:46:32 +00:00
s.sh.OnStop()
2017-10-26 11:14:00 +00:00
}
func (s *server) Serve() error {
if err := s.Start(); err != nil {
return err
}
s.stopWg.Wait()
return nil
}
func runServer(s *server) {
defer s.stopWg.Done()
var conn io.ReadWriteCloser
var clientAddr string
var err error
var stopping atomic.Value
for {
acceptChan := make(chan struct{})
go func() {
2017-10-31 10:46:32 +00:00
if conn, clientAddr, err = s.sh.accept(s.listener); err != nil {
2017-10-26 11:14:00 +00:00
if stopping.Load() == nil {
2017-10-31 10:46:32 +00:00
logging.Logger.Error(fmt.Sprintf("Server: [%s]. Cannot accept new connection: [%s]", s.sh.GetAddr(), 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
}
s.stopWg.Add(1)
2017-10-27 07:07:14 +00:00
go handleServerConnection(s, conn, clientAddr)
2017-10-26 11:14:00 +00:00
}
}
2017-10-27 07:07:14 +00:00
func handleServerConnection(s *server, conn io.ReadWriteCloser, clientAddr string) {
2017-10-26 11:14:00 +00:00
defer s.stopWg.Done()
2017-10-27 07:14:20 +00:00
2017-10-31 10:46:32 +00:00
if err := s.sh.OnHandshake(clientAddr, conn); nil != err {
2017-10-27 07:14:20 +00:00
logging.Logger.Error(fmt.Sprintf("Server: [%s]. handshake error: [%s]", clientAddr, err))
conn.Close()
return
}
2017-10-27 05:33:35 +00:00
logging.Logger.Debug(fmt.Sprintf("Server: Client[%s] is connected.", clientAddr))
clientStopChan := make(chan struct{})
2017-10-31 10:46:32 +00:00
go s.sh.Handle(clientAddr, conn, clientStopChan)
2017-10-26 11:14:00 +00:00
select {
case <-s.stopChan:
2017-10-27 05:33:35 +00:00
close(clientStopChan)
2017-10-26 11:14:00 +00:00
conn.Close()
return
2017-10-27 05:33:35 +00:00
case <-clientStopChan:
2017-10-27 03:47:10 +00:00
conn.Close()
2017-10-27 05:33:35 +00:00
logging.Logger.Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", clientAddr))
2017-10-27 03:47:10 +00:00
return
2017-10-26 11:14:00 +00:00
}
}