This commit is contained in:
crusader 2017-11-29 09:57:03 +09:00
commit 1cdf24971c
4 changed files with 51 additions and 38 deletions

View File

@ -10,12 +10,6 @@ import (
"git.loafle.net/commons_go/logging" "git.loafle.net/commons_go/logging"
) )
type Server interface {
Start() error
Stop()
Serve() error
}
func New(sh ServerHandler) Server { func New(sh ServerHandler) Server {
s := &server{ s := &server{
sh: sh, sh: sh,
@ -23,6 +17,12 @@ func New(sh ServerHandler) Server {
return s return s
} }
type Server interface {
Start() error
Stop()
Serve() error
}
type server struct { type server struct {
sh ServerHandler sh ServerHandler
@ -48,6 +48,10 @@ func (s *server) Start() error {
return err return err
} }
if err = s.sh.Init(); nil != err {
logging.Logger().Panic(fmt.Sprintf("Server: Initialization of server has been failed %v", err))
}
s.stopChan = make(chan struct{}) s.stopChan = make(chan struct{})
s.sh.OnStart() s.sh.OnStart()
@ -65,7 +69,10 @@ func (s *server) Stop() {
close(s.stopChan) close(s.stopChan)
s.stopWg.Wait() s.stopWg.Wait()
s.stopChan = nil s.stopChan = nil
s.sh.OnStop() s.sh.OnStop()
logging.Logger().Info(fmt.Sprintf("Server[%s] is stopped", s.sh.GetName()))
} }
func (s *server) Serve() error { func (s *server) Serve() error {
@ -88,10 +95,8 @@ func handleServer(s *server) {
go func() { go func() {
if conn, err = s.listener.Accept(); err != nil { if conn, err = s.listener.Accept(); err != nil {
if stopping.Load() == nil { if stopping.Load() == nil {
logging.Logger.Error(fmt.Sprintf("Server: Cannot accept new connection: [%s]", err)) logging.Logger().Error(fmt.Sprintf("Server: Cannot accept new connection: [%s]", err))
} }
} else {
conn, err = s.sh.OnAccept(conn)
} }
close(acceptChan) close(acceptChan)
}() }()
@ -124,19 +129,27 @@ func handleServer(s *server) {
func handleConnection(s *server, conn net.Conn) { func handleConnection(s *server, conn net.Conn) {
defer s.stopWg.Done() defer s.stopWg.Done()
logging.Logger.Debug(fmt.Sprintf("Server: Client[%s] is connected.", conn.RemoteAddr())) var err error
if conn, err = s.sh.OnConnect(conn); nil != err {
logging.Logger().Error(fmt.Sprintf("Server: connecting[%s] failed %v", conn.RemoteAddr(), err))
return
}
logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is connected.", conn.RemoteAddr()))
clientStopChan := make(chan struct{}) clientStopChan := make(chan struct{})
handleDoneCnah := make(chan struct{}) handleDoneChan := make(chan struct{})
go s.sh.Handle(conn, clientStopChan, handleDoneCnah)
go s.sh.Handle(conn, clientStopChan, handleDoneChan)
select { select {
case <-s.stopChan: case <-s.stopChan:
close(clientStopChan) close(clientStopChan)
conn.Close() conn.Close()
<-handleDoneCnah <-handleDoneChan
case <-handleDoneCnah: case <-handleDoneChan:
close(clientStopChan) close(clientStopChan)
conn.Close() conn.Close()
logging.Logger.Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", conn.RemoteAddr())) logging.Logger().Debug(fmt.Sprintf("Server: Client[%s] is disconnected.", conn.RemoteAddr()))
} }
} }

View File

@ -12,9 +12,16 @@ type ServerHandler interface {
OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) OnError(serverCTX ServerContext, conn net.Conn, status int, reason error)
// OnStop invoked when server is stopped
// If you override ths method, must call
//
// func (sh *ServerHandler) OnStop() {
// ...
// sh.ServerHandler.OnStop()
// }
OnStop(serverCTX ServerContext) OnStop(serverCTX ServerContext)
IsClientDisconnect(err error) bool GetName() string
Validate() Validate()
} }

View File

@ -2,16 +2,16 @@ package server
import ( import (
"errors" "errors"
"io"
"log"
"net" "net"
) )
type ServerHandlers struct { func NewServerHandler() ServerHandler {
sh := &ServerHandlers{}
return sh
} }
func (sh *ServerHandlers) ServerContext() ServerContext { func (sh *ServerHandlers) ServerContext() ServerContext {
return newServerContext()
} }
func (sh *ServerHandlers) Init(serverCTX ServerContext) error { func (sh *ServerHandlers) Init(serverCTX ServerContext) error {
@ -26,32 +26,18 @@ func (sh *ServerHandlers) OnStart(serverCTX ServerContext) {
// no op // no op
} }
func (sh *ServerHandlers) OnAccept(conn net.Conn) (net.Conn, error) {
}
func (sh *ServerHandlers) Handle(serverCTX ServerContext, conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) {
}
func (sh *ServerHandlers) OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) { func (sh *ServerHandlers) OnError(serverCTX ServerContext, conn net.Conn, status int, reason error) {
} }
// OnStop invoked when server is stopped
// If you override ths method, must call
func (sh *ServerHandlers) OnStop(serverCTX ServerContext) { func (sh *ServerHandlers) OnStop(serverCTX ServerContext) {
// no op // no op
} }
func (sh *ServerHandlers) OnAccept(conn net.Conn) (net.Conn, error) { func (sh *ServerHandlers) GetName() string {
return conn, nil return sh.Name
}
func (sh *ServerHandlers) Handle(conn net.Conn, stopChan <-chan struct{}, doneChan chan<- struct{}) {
log.Printf("Server.Handle")
}
func (sh *ServerHandlers) IsClientDisconnect(err error) bool {
return err == io.ErrUnexpectedEOF || err == io.EOF
} }
func (sh *ServerHandlers) Validate() { func (sh *ServerHandlers) Validate() {

7
util.go Normal file
View File

@ -0,0 +1,7 @@
package server
import "io"
func IsClientDisconnect(err error) bool {
return err == io.ErrUnexpectedEOF || err == io.EOF
}