This commit is contained in:
crusader 2018-04-04 22:50:34 +09:00
parent 1ca5974e12
commit acbc1b22be
6 changed files with 49 additions and 49 deletions

View File

@ -4,28 +4,28 @@ import (
"time"
)
type ClientConnectionHandler interface {
type ClientConnHandler interface {
ConnectionHandler
GetReconnectInterval() time.Duration
GetReconnectTryTime() int
}
type ClientConnectionHandlers struct {
type ClientConnHandlers struct {
ConnectionHandlers
ReconnectInterval time.Duration
ReconnectTryTime int
}
func (cch *ClientConnectionHandlers) GetReconnectInterval() time.Duration {
func (cch *ClientConnHandlers) GetReconnectInterval() time.Duration {
return cch.ReconnectInterval
}
func (cch *ClientConnectionHandlers) GetReconnectTryTime() int {
func (cch *ClientConnHandlers) GetReconnectTryTime() int {
return cch.ReconnectTryTime
}
func (cch *ClientConnectionHandlers) Validate() error {
func (cch *ClientConnHandlers) Validate() error {
if err := cch.ConnectionHandlers.Validate(); nil != err {
return err
}

View File

@ -7,7 +7,7 @@ import (
logging "git.loafle.net/commons/logging-go"
)
type ClientRWCHandler struct {
type ClientReadWriter struct {
ReadwriteHandler ReadWriteHandler
ReadChan chan<- []byte
WriteChan <-chan []byte
@ -17,14 +17,14 @@ type ClientRWCHandler struct {
ClientStopWg *sync.WaitGroup
}
func (crwch *ClientRWCHandler) HandleConnection(conn *Conn) {
func (crw *ClientReadWriter) HandleConnection(conn *Conn) {
defer func() {
if nil != conn {
conn.Close()
}
logging.Logger().Infof("disconnected")
crwch.ClientStopWg.Done()
crw.ClientStopWg.Done()
}()
logging.Logger().Infof("connected")
@ -39,8 +39,8 @@ func (crwch *ClientRWCHandler) HandleConnection(conn *Conn) {
for {
if nil != err {
if io.EOF == err || io.ErrUnexpectedEOF == err {
crwch.DisconnectedChan <- struct{}{}
newConn := <-crwch.ReconnectedChan
crw.DisconnectedChan <- struct{}{}
newConn := <-crw.ReconnectedChan
if nil == newConn {
return
}
@ -50,8 +50,8 @@ func (crwch *ClientRWCHandler) HandleConnection(conn *Conn) {
}
}
go connReadHandler(crwch.ReadwriteHandler, conn, stopChan, readerDoneChan, crwch.ReadChan)
go connWriteHandler(crwch.ReadwriteHandler, conn, stopChan, writerDoneChan, crwch.WriteChan)
go connReadHandler(crw.ReadwriteHandler, conn, stopChan, readerDoneChan, crw.ReadChan)
go connWriteHandler(crw.ReadwriteHandler, conn, stopChan, writerDoneChan, crw.WriteChan)
select {
case err = <-readerDoneChan:
@ -60,7 +60,7 @@ func (crwch *ClientRWCHandler) HandleConnection(conn *Conn) {
case err = <-writerDoneChan:
close(stopChan)
<-readerDoneChan
case <-crwch.ClientStopChan:
case <-crw.ClientStopChan:
close(stopChan)
<-readerDoneChan
<-writerDoneChan

View File

@ -23,7 +23,7 @@ import (
var errMalformedURL = errors.New("malformed ws or wss URL")
type Client struct {
server.ClientConnectionHandlers
server.ClientConnHandlers
server.ReadWriteHandlers
Name string
@ -58,7 +58,7 @@ type Client struct {
disconnectedChan chan struct{}
reconnectedChan chan *server.Conn
crwch server.ClientRWCHandler
crw server.ClientReadWriter
}
func (c *Client) Connect() (readChan <-chan []byte, writeChan chan<- []byte, res *http.Response, err error) {
@ -86,16 +86,16 @@ func (c *Client) Connect() (readChan <-chan []byte, writeChan chan<- []byte, res
c.reconnectedChan = make(chan *server.Conn)
c.stopChan = make(chan struct{})
c.crwch.ReadwriteHandler = c
c.crwch.ReadChan = c.readChan
c.crwch.WriteChan = c.writeChan
c.crwch.ClientStopChan = c.stopChan
c.crwch.ClientStopWg = &c.stopWg
c.crwch.DisconnectedChan = c.disconnectedChan
c.crwch.ReconnectedChan = c.reconnectedChan
c.crw.ReadwriteHandler = c
c.crw.ReadChan = c.readChan
c.crw.WriteChan = c.writeChan
c.crw.ClientStopChan = c.stopChan
c.crw.ClientStopWg = &c.stopWg
c.crw.DisconnectedChan = c.disconnectedChan
c.crw.ReconnectedChan = c.reconnectedChan
c.stopWg.Add(1)
go c.crwch.HandleConnection(conn)
go c.crw.HandleConnection(conn)
return c.readChan, c.writeChan, res, nil
}

View File

@ -12,7 +12,7 @@ import (
)
type Client struct {
server.ClientConnectionHandlers
server.ClientConnHandlers
server.ReadWriteHandlers
Name string
@ -30,7 +30,7 @@ type Client struct {
disconnectedChan chan struct{}
reconnectedChan chan *server.Conn
crwch server.ClientRWCHandler
crw server.ClientReadWriter
}
func (c *Client) Connect() (readChan <-chan []byte, writeChan chan<- []byte, err error) {
@ -58,16 +58,16 @@ func (c *Client) Connect() (readChan <-chan []byte, writeChan chan<- []byte, err
c.reconnectedChan = make(chan *server.Conn)
c.stopChan = make(chan struct{})
c.crwch.ReadwriteHandler = c
c.crwch.ReadChan = c.readChan
c.crwch.WriteChan = c.writeChan
c.crwch.ClientStopChan = c.stopChan
c.crwch.ClientStopWg = &c.stopWg
c.crwch.DisconnectedChan = c.disconnectedChan
c.crwch.ReconnectedChan = c.reconnectedChan
c.crw.ReadwriteHandler = c
c.crw.ReadChan = c.readChan
c.crw.WriteChan = c.writeChan
c.crw.ClientStopChan = c.stopChan
c.crw.ClientStopWg = &c.stopWg
c.crw.DisconnectedChan = c.disconnectedChan
c.crw.ReconnectedChan = c.reconnectedChan
c.stopWg.Add(1)
go c.crwch.HandleConnection(conn)
go c.crw.HandleConnection(conn)
return c.readChan, c.writeChan, nil
}

View File

@ -19,7 +19,7 @@ type Server struct {
stopChan chan struct{}
stopWg sync.WaitGroup
srwch server.ServerRWCHandler
srw server.ServerReadWriter
}
func (s *Server) ListenAndServe() error {
@ -53,9 +53,9 @@ func (s *Server) ListenAndServe() error {
s.stopChan = make(chan struct{})
s.srwch.ReadwriteHandler = s.ServerHandler
s.srwch.ServerStopChan = s.stopChan
s.srwch.ServerStopWg = &s.stopWg
s.srw.ReadwriteHandler = s.ServerHandler
s.srw.ServerStopChan = s.stopChan
s.srw.ServerStopWg = &s.stopWg
s.stopWg.Add(1)
return s.handleServer(listener)
@ -136,7 +136,7 @@ func (s *Server) handleServer(listener net.Listener) error {
}
if 0 < s.ServerHandler.GetConcurrency() {
sz := s.srwch.ConnectionSize()
sz := s.srw.ConnectionSize()
if sz >= s.ServerHandler.GetConcurrency() {
logging.Logger().Warnf(s.serverMessage(fmt.Sprintf("max connections size %d, refuse", sz)))
netConn.Close()
@ -164,6 +164,6 @@ func (s *Server) handleServer(listener net.Listener) error {
conn := server.NewConn(netConn, true, s.ServerHandler.GetReadBufferSize(), s.ServerHandler.GetWriteBufferSize())
s.stopWg.Add(1)
go s.srwch.HandleConnection(servlet, servletCtx, conn)
go s.srw.HandleConnection(servlet, servletCtx, conn)
}
}

View File

@ -6,7 +6,7 @@ import (
logging "git.loafle.net/commons/logging-go"
)
type ServerRWCHandler struct {
type ServerReadWriter struct {
connections sync.Map
ReadwriteHandler ReadWriteHandler
@ -14,16 +14,16 @@ type ServerRWCHandler struct {
ServerStopWg *sync.WaitGroup
}
func (srwch *ServerRWCHandler) ConnectionSize() int {
func (srw *ServerReadWriter) ConnectionSize() int {
var sz int
srwch.connections.Range(func(k, v interface{}) bool {
srw.connections.Range(func(k, v interface{}) bool {
sz++
return true
})
return sz
}
func (srwch *ServerRWCHandler) HandleConnection(servlet Servlet, servletCtx ServletCtx, conn *Conn) {
func (srw *ServerReadWriter) HandleConnection(servlet Servlet, servletCtx ServletCtx, conn *Conn) {
addr := conn.RemoteAddr()
defer func() {
@ -32,13 +32,13 @@ func (srwch *ServerRWCHandler) HandleConnection(servlet Servlet, servletCtx Serv
}
servlet.OnDisconnect(servletCtx)
logging.Logger().Infof("Client[%s] has been disconnected", addr)
srwch.ServerStopWg.Done()
srw.ServerStopWg.Done()
}()
logging.Logger().Infof("Client[%s] has been connected", addr)
srwch.connections.Store(conn, true)
defer srwch.connections.Delete(conn)
srw.connections.Store(conn, true)
defer srw.connections.Delete(conn)
servlet.OnConnect(servletCtx, conn)
conn.SetCloseHandler(func(code int, text string) error {
@ -56,8 +56,8 @@ func (srwch *ServerRWCHandler) HandleConnection(servlet Servlet, servletCtx Serv
writerDoneChan := make(chan error)
go servlet.Handle(servletCtx, stopChan, servletDoneChan, readChan, writeChan)
go connReadHandler(srwch.ReadwriteHandler, conn, stopChan, readerDoneChan, readChan)
go connWriteHandler(srwch.ReadwriteHandler, conn, stopChan, writerDoneChan, writeChan)
go connReadHandler(srw.ReadwriteHandler, conn, stopChan, readerDoneChan, readChan)
go connWriteHandler(srw.ReadwriteHandler, conn, stopChan, writerDoneChan, writeChan)
select {
case <-readerDoneChan:
@ -72,7 +72,7 @@ func (srwch *ServerRWCHandler) HandleConnection(servlet Servlet, servletCtx Serv
close(stopChan)
<-readerDoneChan
<-writerDoneChan
case <-srwch.ServerStopChan:
case <-srw.ServerStopChan:
close(stopChan)
<-readerDoneChan
<-writerDoneChan