Skip to content

Commit

Permalink
Merge pull request #92 from takewofly/feat/connect_session
Browse files Browse the repository at this point in the history
Fix: fix connection was assigned a nil will lead to panic
  • Loading branch information
AlexStocks authored Jan 15, 2022
2 parents 292995d + 431c47e commit 0b14a0d
Showing 1 changed file with 119 additions and 8 deletions.
127 changes: 119 additions & 8 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,14 @@ type Session interface {
IsClosed() bool
// EndPoint get endpoint type
EndPoint() EndPoint

SetMaxMsgLen(int)
SetName(string)
SetEventListener(EventListener)
SetPkgHandler(ReadWriter)
SetReader(Reader)
SetWriter(Writer)
SetCronPeriod(int)

SetWaitTime(time.Duration)

GetAttribute(interface{}) interface{}
SetAttribute(interface{}, interface{})
RemoveAttribute(interface{})
Expand Down Expand Up @@ -565,11 +562,6 @@ func (s *session) run() {

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()
}
Expand Down Expand Up @@ -878,3 +870,122 @@ func (s *session) Close() {
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()
}

0 comments on commit 0b14a0d

Please sign in to comment.