Skip to content

Commit

Permalink
把AddCloseCallback和RemoveCloseCallback改为公开方法,修复Add和Remove行为不一致的设计缺陷
Browse files Browse the repository at this point in the history
  • Loading branch information
bg5sbk committed Oct 25, 2016
1 parent 61bb850 commit 1aa2d1d
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 16 deletions.
Empty file modified README.md
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ func (channel *Channel) Put(key KEY, session *Session) {
if session, exists := channel.sessions[key]; exists {
channel.remove(key, session)
}
session.addCloseCallback(channel, func() {
session.AddCloseCallback(channel, key, func() {
channel.Remove(key)
})
channel.sessions[key] = session
}

func (channel *Channel) remove(key KEY, session *Session) {
session.removeCloseCallback(channel)
session.RemoveCloseCallback(channel, key)
delete(channel.sessions, key)
}

Expand Down
4 changes: 2 additions & 2 deletions channel_int32.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (channel *Int32Channel) Put(key int32, session *Session) {
if session, exists := channel.sessions[key]; exists {
channel.remove(key, session)
}
session.addCloseCallback(channel, func() {
session.AddCloseCallback(channel, key, func() {
channel.Remove(key)
})
channel.sessions[key] = session
}

func (channel *Int32Channel) remove(key int32, session *Session) {
session.removeCloseCallback(channel)
session.RemoveCloseCallback(channel, key)
delete(channel.sessions, key)
}

Expand Down
4 changes: 2 additions & 2 deletions channel_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (channel *Int64Channel) Put(key int64, session *Session) {
if session, exists := channel.sessions[key]; exists {
channel.remove(key, session)
}
session.addCloseCallback(channel, func() {
session.AddCloseCallback(channel, key, func() {
channel.Remove(key)
})
channel.sessions[key] = session
}

func (channel *Int64Channel) remove(key int64, session *Session) {
session.removeCloseCallback(channel)
session.RemoveCloseCallback(channel, key)
delete(channel.sessions, key)
}

Expand Down
4 changes: 2 additions & 2 deletions channel_string.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (channel *StringChannel) Put(key string, session *Session) {
if session, exists := channel.sessions[key]; exists {
channel.remove(key, session)
}
session.addCloseCallback(channel, func() {
session.AddCloseCallback(channel, key, func() {
channel.Remove(key)
})
channel.sessions[key] = session
}

func (channel *StringChannel) remove(key string, session *Session) {
session.removeCloseCallback(channel)
session.RemoveCloseCallback(channel, key)
delete(channel.sessions, key)
}

Expand Down
4 changes: 2 additions & 2 deletions channel_uint32.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (channel *Uint32Channel) Put(key uint32, session *Session) {
if session, exists := channel.sessions[key]; exists {
channel.remove(key, session)
}
session.addCloseCallback(channel, func() {
session.AddCloseCallback(channel, key, func() {
channel.Remove(key)
})
channel.sessions[key] = session
}

func (channel *Uint32Channel) remove(key uint32, session *Session) {
session.removeCloseCallback(channel)
session.RemoveCloseCallback(channel, key)
delete(channel.sessions, key)
}

Expand Down
4 changes: 2 additions & 2 deletions channel_uint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,14 @@ func (channel *Uint64Channel) Put(key uint64, session *Session) {
if session, exists := channel.sessions[key]; exists {
channel.remove(key, session)
}
session.addCloseCallback(channel, func() {
session.AddCloseCallback(channel, key, func() {
channel.Remove(key)
})
channel.sessions[key] = session
}

func (channel *Uint64Channel) remove(key uint64, session *Session) {
session.removeCloseCallback(channel)
session.RemoveCloseCallback(channel, key)
delete(channel.sessions, key)
}

Expand Down
10 changes: 6 additions & 4 deletions session.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,11 @@ func (session *Session) Send(msg interface{}) error {

type closeCallback struct {
Handler interface{}
Key interface{}
Func func()
}

func (session *Session) addCloseCallback(handler interface{}, callback func()) {
func (session *Session) AddCloseCallback(handler, key interface{}, callback func()) {
if session.IsClosed() {
return
}
Expand All @@ -147,10 +148,10 @@ func (session *Session) addCloseCallback(handler interface{}, callback func()) {
session.closeCallbacks = list.New()
}

session.closeCallbacks.PushBack(closeCallback{handler, callback})
session.closeCallbacks.PushBack(&closeCallback{handler, key, callback})
}

func (session *Session) removeCloseCallback(handler interface{}) {
func (session *Session) RemoveCloseCallback(handler, key interface{}) {
if session.IsClosed() {
return
}
Expand All @@ -159,7 +160,8 @@ func (session *Session) removeCloseCallback(handler interface{}) {
defer session.closeMutex.Unlock()

for i := session.closeCallbacks.Front(); i != nil; i = i.Next() {
if i.Value.(closeCallback).Handler == handler {
item := i.Value.(*closeCallback)
if item.Handler == handler && item.Key == key {
session.closeCallbacks.Remove(i)
return
}
Expand Down

0 comments on commit 1aa2d1d

Please sign in to comment.