Commit 79dd7fa9 authored by AlexStocks's avatar AlexStocks

session var -> ss

parent 5d4283aa
......@@ -14,9 +14,11 @@
- 2017/02/08
> improvement
>
> 1 server shutdown in server.go:(Server) stop()
> 1 server shutdown in server.go:(Server) stop()
>
> 2 version: 0.7.01
> 2 session variable -> ss
>
> 3 version: 0.7.01
- 2017/02/03
> 1 Session struct -> session struct and add Session interface
......
......@@ -43,7 +43,7 @@ type Client struct {
interval time.Duration
addr string
newSession NewSessionCallback
sessionMap map[Session]gxsync.Empty
ssMap map[Session]gxsync.Empty
sync.Once
done chan gxsync.Empty
......@@ -66,11 +66,11 @@ func NewClient(connNum int, connInterval time.Duration, serverAddr string) *Clie
}
return &Client{
number: connNum,
interval: connInterval,
addr: serverAddr,
sessionMap: make(map[Session]gxsync.Empty, connNum),
done: make(chan gxsync.Empty),
number: connNum,
interval: connInterval,
addr: serverAddr,
ssMap: make(map[Session]gxsync.Empty, connNum),
done: make(chan gxsync.Empty),
}
}
......@@ -88,12 +88,12 @@ func NewWSSClient(connNum int, connInterval time.Duration, serverAddr string, ce
}
return &Client{
number: connNum,
interval: connInterval,
addr: serverAddr,
sessionMap: make(map[Session]gxsync.Empty, connNum),
done: make(chan gxsync.Empty),
certFile: cert,
number: connNum,
interval: connInterval,
addr: serverAddr,
ssMap: make(map[Session]gxsync.Empty, connNum),
done: make(chan gxsync.Empty),
certFile: cert,
}
}
......@@ -123,10 +123,10 @@ func (c *Client) dialTCP() Session {
func (c *Client) dialWS() Session {
var (
err error
dialer websocket.Dialer
conn *websocket.Conn
session Session
err error
dialer websocket.Dialer
conn *websocket.Conn
ss Session
)
dialer.EnableCompression = true
......@@ -139,12 +139,12 @@ func (c *Client) dialWS() Session {
err = errSelfConnect
}
if err == nil {
session = NewWSSession(conn)
if session.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(session.(*session).maxMsgLen))
ss = NewWSSession(conn)
if ss.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(ss.(*session).maxMsgLen))
}
return session
return ss
}
log.Info("websocket.dialer.Dial(addr:%s) = error{%v}", c.addr, err)
......@@ -160,7 +160,7 @@ func (c *Client) dialWSS() Session {
certPool *x509.CertPool
dialer websocket.Dialer
conn *websocket.Conn
session Session
ss Session
)
dialer.EnableCompression = true
......@@ -184,12 +184,12 @@ func (c *Client) dialWSS() Session {
err = errSelfConnect
}
if err == nil {
session = NewWSSession(conn)
if session.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(session.(*session).maxMsgLen))
ss = NewWSSession(conn)
if ss.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(ss.(*session).maxMsgLen))
}
return session
return ss
}
log.Info("websocket.dialer.Dial(addr:%s) = error{%v}", c.addr, err)
......@@ -208,16 +208,16 @@ func (c *Client) dial() Session {
return c.dialTCP()
}
func (c *Client) sessionNum() int {
func (c *Client) ssNum() int {
var num int
c.Lock()
for s := range c.sessionMap {
for s := range c.ssMap {
if s.IsClosed() {
delete(c.sessionMap, s)
delete(c.ssMap, s)
}
}
num = len(c.sessionMap)
num = len(c.ssMap)
c.Unlock()
return num
......@@ -225,28 +225,28 @@ func (c *Client) sessionNum() int {
func (c *Client) connect() {
var (
err error
session Session
err error
ss Session
)
for {
session = c.dial()
if session == nil {
ss = c.dial()
if ss == nil {
// client has been closed
break
}
err = c.newSession(session)
err = c.newSession(ss)
if err == nil {
// session.RunEventLoop()
session.(*session).run()
// ss.RunEventLoop()
ss.(*session).run()
c.Lock()
c.sessionMap[session] = gxsync.Empty{}
c.ssMap[ss] = gxsync.Empty{}
c.Unlock()
break
}
// don't distinguish between tcp connection and websocket connection. Because
// gorilla/websocket/conn.go:(Conn)Close also invoke net.Conn.Close()
session.Conn().Close()
ss.Conn().Close()
}
}
......@@ -270,7 +270,7 @@ func (c *Client) RunEventLoop(newSession NewSessionCallback) {
break
}
num = c.sessionNum()
num = c.ssNum()
// log.Info("current client connction number:%d", num)
if max <= num {
times++
......@@ -295,10 +295,10 @@ func (c *Client) stop() {
c.Once.Do(func() {
close(c.done)
c.Lock()
for s := range c.sessionMap {
for s := range c.ssMap {
s.Close()
}
c.sessionMap = nil
c.ssMap = nil
c.Unlock()
})
}
......
......@@ -191,18 +191,18 @@ func (s *wsHandler) serveWSRequest(w http.ResponseWriter, r *http.Request) {
return
}
// conn.SetReadLimit(int64(handler.maxMsgLen))
session := NewWSSession(conn)
err = s.newSession(session)
ss := NewWSSession(conn)
err = s.newSession(ss)
if err != nil {
conn.Close()
log.Warn("Server{%s}.newSession(session{%#v}) = err {%#v}", s.server.addr, session, err)
log.Warn("Server{%s}.newSession(ss{%#v}) = err {%#v}", s.server.addr, ss, err)
return
}
if session.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(session.(*session).maxMsgLen))
if ss.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(ss.(*session).maxMsgLen))
}
// session.RunEventLoop()
session.(*session).run()
// ss.RunEventLoop()
ss.(*session).run()
}
// RunWSEventLoop serve websocket client request
......@@ -291,14 +291,14 @@ func (s *Server) accept(newSession NewSessionCallback) (Session, error) {
return nil, errSelfConnect
}
session := NewTCPSession(conn)
err = newSession(session)
ss := NewTCPSession(conn)
err = newSession(ss)
if err != nil {
conn.Close()
return nil, err
}
return session, nil
return ss, nil
}
func (s *Server) Close() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment