Merge pull request #92 from takewofly/feat/connect_session
Fix: fix connection was assigned a nil will lead to panic
diff --git a/session.go b/session.go
index be225c2..5bfa4da 100644
--- a/session.go
+++ b/session.go
@@ -73,7 +73,6 @@
IsClosed() bool
// EndPoint get endpoint type
EndPoint() EndPoint
-
SetMaxMsgLen(int)
SetName(string)
SetEventListener(EventListener)
@@ -81,9 +80,7 @@
SetReader(Reader)
SetWriter(Writer)
SetCronPeriod(int)
-
SetWaitTime(time.Duration)
-
GetAttribute(interface{}) interface{}
SetAttribute(interface{}, interface{})
RemoveAttribute(interface{})
@@ -565,11 +562,6 @@
func (s *session) addTask(pkg interface{}) {
f := func() {
- s.lock.RLock()
- defer s.lock.RUnlock()
- if s.Connection == nil {
- return
- }
s.listener.OnMessage(s, pkg)
s.incReadPkgNum()
}
@@ -878,3 +870,122 @@
s.stop()
log.Infof("%s closed now. its current gr num is %d", s.sessionToken(), s.grNum.Load())
}
+
+// GetActive return connection's time
+func (s *session) GetActive() time.Time {
+ if s == nil {
+ return launchTime
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ return s.Connection.GetActive()
+ }
+ return launchTime
+}
+
+// UpdateActive update connection's active time
+func (s *session) UpdateActive() {
+ if s == nil {
+ return
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+
+ if s.Connection != nil {
+ s.Connection.UpdateActive()
+ }
+}
+
+func (s *session) ID() uint32 {
+ if s == nil {
+ return 0
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ return s.Connection.ID()
+ }
+ return 0
+}
+
+func (s *session) LocalAddr() string {
+ if s == nil {
+ return ""
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ return s.Connection.LocalAddr()
+ }
+ return ""
+}
+
+func (s *session) RemoteAddr() string {
+ if s == nil {
+ return ""
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ return s.Connection.RemoteAddr()
+ }
+ return ""
+}
+
+func (s *session) incReadPkgNum() {
+ if s == nil {
+ return
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ s.Connection.incReadPkgNum()
+ }
+}
+
+func (s *session) incWritePkgNum() {
+ if s == nil {
+ return
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ s.Connection.incWritePkgNum()
+ }
+}
+
+func (s *session) send(pkg interface{}) (int, error) {
+ if s == nil {
+ return 0, nil
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ return s.Connection.send(pkg)
+ }
+ return 0, nil
+}
+
+func (s *session) readTimeout() time.Duration {
+ if s == nil {
+ return time.Duration(0)
+ }
+ s.lock.RLock()
+ defer s.lock.RUnlock()
+ if s.Connection != nil {
+ return s.Connection.readTimeout()
+ }
+ return time.Duration(0)
+}
+
+func (s *session) setSession(ss Session) {
+ if s == nil {
+ return
+ }
+ s.lock.RLock()
+ if s.Connection != nil {
+ s.Connection.setSession(ss)
+ }
+ s.lock.RUnlock()
+}