Commit 96c2170a authored by AlexStocks's avatar AlexStocks

Session::EndPointType() -> Session::EndPoint()

parent 3e82e215
...@@ -19,8 +19,9 @@ ...@@ -19,8 +19,9 @@
* nerr -> netError * nerr -> netError
* check udp connection alive after connect() * check udp connection alive after connect()
* use ReadFromUDP as the uniform UDP read interface * use ReadFromUDP as the uniform UDP read interface
* close net.UDPConn when connected failed * close net.UDPConn when connected failed
* close net.Conn when connected failed * close net.Conn when connected failed
* Session::EndPointType() -> Session::EndPoint()
- 2018/03/17 - 2018/03/17
> improvement > improvement
......
...@@ -68,7 +68,7 @@ func newClient(t EndPointType, opts ...ClientOption) *client { ...@@ -68,7 +68,7 @@ func newClient(t EndPointType, opts ...ClientOption) *client {
c.init(opts...) c.init(opts...)
if c.number <= 0 || c.addr == "" { if c.number <= 0 || c.addr == "" {
panic(fmt.Sprintf("@connNum:%d, @serverAddr:%s", c.number, c.addr)) panic(fmt.Sprintf("client type:%s, @connNum:%d, @serverAddr:%s", t, c.number, c.addr))
} }
c.ssMap = make(map[Session]gxsync.Empty, c.number) c.ssMap = make(map[Session]gxsync.Empty, c.number)
...@@ -131,7 +131,7 @@ func (c *client) dialTCP() Session { ...@@ -131,7 +131,7 @@ func (c *client) dialTCP() Session {
err = errSelfConnect err = errSelfConnect
} }
if err == nil { if err == nil {
return newTCPSession(conn, c.endPointType) return newTCPSession(conn, c)
} }
log.Info("net.DialTimeout(addr:%s, timeout:%v) = error{%s}", c.addr, err) log.Info("net.DialTimeout(addr:%s, timeout:%v) = error{%s}", c.addr, err)
...@@ -158,6 +158,7 @@ func (c *client) dialUDP() Session { ...@@ -158,6 +158,7 @@ func (c *client) dialUDP() Session {
} }
conn, err = net.DialUDP("udp", localAddr, peerAddr) conn, err = net.DialUDP("udp", localAddr, peerAddr)
if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() { if err == nil && conn.LocalAddr().String() == conn.RemoteAddr().String() {
conn.Close()
err = errSelfConnect err = errSelfConnect
} }
if err != nil { if err != nil {
...@@ -187,7 +188,7 @@ func (c *client) dialUDP() Session { ...@@ -187,7 +188,7 @@ func (c *client) dialUDP() Session {
continue continue
} }
//if err == nil { //if err == nil {
return newUDPSession(conn, c.endPointType) return newUDPSession(conn, c)
//} //}
} }
} }
...@@ -212,7 +213,7 @@ func (c *client) dialWS() Session { ...@@ -212,7 +213,7 @@ func (c *client) dialWS() Session {
err = errSelfConnect err = errSelfConnect
} }
if err == nil { if err == nil {
ss = newWSSession(conn, c.endPointType) ss = newWSSession(conn, c)
if ss.(*session).maxMsgLen > 0 { if ss.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(ss.(*session).maxMsgLen)) conn.SetReadLimit(int64(ss.(*session).maxMsgLen))
} }
...@@ -289,7 +290,7 @@ func (c *client) dialWSS() Session { ...@@ -289,7 +290,7 @@ func (c *client) dialWSS() Session {
err = errSelfConnect err = errSelfConnect
} }
if err == nil { if err == nil {
ss = newWSSession(conn, c.endPointType) ss = newWSSession(conn, c)
if ss.(*session).maxMsgLen > 0 { if ss.(*session).maxMsgLen > 0 {
conn.SetReadLimit(int64(ss.(*session).maxMsgLen)) conn.SetReadLimit(int64(ss.(*session).maxMsgLen))
} }
......
...@@ -414,7 +414,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) { ...@@ -414,7 +414,7 @@ func (u *gettyUDPConn) Write(udpCtx interface{}) (int, error) {
if buf, ok = ctx.Pkg.([]byte); !ok { if buf, ok = ctx.Pkg.([]byte); !ok {
return 0, fmt.Errorf("illegal @udpCtx.Pkg{%#v} type", udpCtx) return 0, fmt.Errorf("illegal @udpCtx.Pkg{%#v} type", udpCtx)
} }
if u.ss.EndPointType() == UDP_ENDPOINT { if u.ss.EndPoint().EndPointType() == UDP_ENDPOINT {
peerAddr = ctx.PeerAddr peerAddr = ctx.PeerAddr
if peerAddr == nil { if peerAddr == nil {
return 0, ErrNullPeerAddr return 0, ErrNullPeerAddr
......
...@@ -126,7 +126,7 @@ type Session interface { ...@@ -126,7 +126,7 @@ type Session interface {
Stat() string Stat() string
IsClosed() bool IsClosed() bool
// get endpoint type // get endpoint type
EndPointType() EndPointType EndPoint() EndPoint
SetMaxMsgLen(int) SetMaxMsgLen(int)
SetName(string) SetName(string)
......
...@@ -212,7 +212,7 @@ func (s *server) accept(newSession NewSessionCallback) (Session, error) { ...@@ -212,7 +212,7 @@ func (s *server) accept(newSession NewSessionCallback) (Session, error) {
return nil, errSelfConnect return nil, errSelfConnect
} }
ss := newTCPSession(conn, s.endPointType) ss := newTCPSession(conn, s)
err = newSession(ss) err = newSession(ss)
if err != nil { if err != nil {
conn.Close() conn.Close()
...@@ -267,7 +267,7 @@ func (s *server) runUDPEventLoop(newSession NewSessionCallback) { ...@@ -267,7 +267,7 @@ func (s *server) runUDPEventLoop(newSession NewSessionCallback) {
ss Session ss Session
) )
ss = newUDPSession(s.pktListener.(*net.UDPConn), s.endPointType) ss = newUDPSession(s.pktListener.(*net.UDPConn), s)
if err := newSession(ss); err != nil { if err := newSession(ss); err != nil {
panic(err.Error()) panic(err.Error())
} }
...@@ -317,7 +317,7 @@ func (s *wsHandler) serveWSRequest(w http.ResponseWriter, r *http.Request) { ...@@ -317,7 +317,7 @@ func (s *wsHandler) serveWSRequest(w http.ResponseWriter, r *http.Request) {
return return
} }
// conn.SetReadLimit(int64(handler.maxMsgLen)) // conn.SetReadLimit(int64(handler.maxMsgLen))
ss := newWSSession(conn, s.server.endPointType) ss := newWSSession(conn, s.server)
err = s.newSession(ss) err = s.newSession(ss)
if err != nil { if err != nil {
conn.Close() conn.Close()
......
...@@ -50,9 +50,9 @@ var ( ...@@ -50,9 +50,9 @@ var (
// getty base session // getty base session
type session struct { type session struct {
name string name string
endPointType EndPointType endPoint EndPoint
maxMsgLen int32 maxMsgLen int32
// net read Write // net read Write
Connection Connection
// pkgHandler ReadWriter // pkgHandler ReadWriter
...@@ -75,16 +75,16 @@ type session struct { ...@@ -75,16 +75,16 @@ type session struct {
lock sync.RWMutex lock sync.RWMutex
} }
func newSession(endPointType EndPointType, conn Connection) *session { func newSession(endPoint EndPoint, conn Connection) *session {
ss := &session{ ss := &session{
name: defaultSessionName, name: defaultSessionName,
endPointType: endPointType, endPoint: endPoint,
maxMsgLen: maxReadBufLen, maxMsgLen: maxReadBufLen,
Connection: conn, Connection: conn,
done: make(chan gxsync.Empty), done: make(chan gxsync.Empty),
period: period, period: period,
wait: pendingDuration, wait: pendingDuration,
attrs: gxcontext.NewValuesContext(nil), attrs: gxcontext.NewValuesContext(nil),
} }
ss.Connection.setSession(ss) ss.Connection.setSession(ss)
...@@ -94,25 +94,25 @@ func newSession(endPointType EndPointType, conn Connection) *session { ...@@ -94,25 +94,25 @@ func newSession(endPointType EndPointType, conn Connection) *session {
return ss return ss
} }
func newTCPSession(conn net.Conn, endPointType EndPointType) Session { func newTCPSession(conn net.Conn, endPoint EndPoint) Session {
c := newGettyTCPConn(conn) c := newGettyTCPConn(conn)
session := newSession(endPointType, c) session := newSession(endPoint, c)
session.name = defaultTCPSessionName session.name = defaultTCPSessionName
return session return session
} }
func newUDPSession(conn *net.UDPConn, endPointType EndPointType) Session { func newUDPSession(conn *net.UDPConn, endPoint EndPoint) Session {
c := newGettyUDPConn(conn) c := newGettyUDPConn(conn)
session := newSession(endPointType, c) session := newSession(endPoint, c)
session.name = defaultUDPSessionName session.name = defaultUDPSessionName
return session return session
} }
func newWSSession(conn *websocket.Conn, endPointType EndPointType) Session { func newWSSession(conn *websocket.Conn, endPoint EndPoint) Session {
c := newGettyWSConn(conn) c := newGettyWSConn(conn)
session := newSession(endPointType, c) session := newSession(endPoint, c)
session.name = defaultWSSessionName session.name = defaultWSSessionName
return session return session
...@@ -149,8 +149,8 @@ func (s *session) Conn() net.Conn { ...@@ -149,8 +149,8 @@ func (s *session) Conn() net.Conn {
return nil return nil
} }
func (s *session) EndPointType() EndPointType { func (s *session) EndPoint() EndPoint {
return s.endPointType return s.endPoint
} }
func (s *session) gettyConn() *gettyConn { func (s *session) gettyConn() *gettyConn {
...@@ -294,7 +294,7 @@ func (s *session) RemoveAttribute(key interface{}) { ...@@ -294,7 +294,7 @@ func (s *session) RemoveAttribute(key interface{}) {
} }
func (s *session) sessionToken() string { func (s *session) sessionToken() string {
return fmt.Sprintf("{%s:%d:%s<->%s}", s.name, s.ID(), s.LocalAddr(), s.RemoteAddr()) return fmt.Sprintf("{%s:%s:%d:%s<->%s}", s.name, s.EndPoint().EndPointType(), s.ID(), s.LocalAddr(), s.RemoteAddr())
} }
// Queued Write, for handler. // Queued Write, for handler.
......
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