Commit 96c2170a authored by AlexStocks's avatar AlexStocks

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

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