Skip to content

Commit

Permalink
让收发保持线程安全,简化Codec开发复杂度
Browse files Browse the repository at this point in the history
  • Loading branch information
bg5sbk committed Nov 14, 2016
1 parent 878817c commit c79f38f
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Session struct {
codec Codec
manager *Manager
sendChan chan interface{}
recvMutex sync.Mutex
sendMutex sync.RWMutex

closeFlag int32
Expand Down Expand Up @@ -81,6 +82,9 @@ func (session *Session) Codec() Codec {
}

func (session *Session) Receive() (interface{}, error) {
session.recvMutex.Lock()
defer session.recvMutex.Unlock()

msg, err := session.codec.Receive()
if err != nil {
session.Close()
Expand Down Expand Up @@ -108,6 +112,9 @@ func (session *Session) Send(msg interface{}) error {
return SessionClosedError
}

session.sendMutex.Lock()
defer session.sendMutex.Unlock()

err := session.codec.Send(msg)
if err != nil {
session.Close()
Expand Down

0 comments on commit c79f38f

Please sign in to comment.