close channel on server side

This commit is contained in:
Gennadii Kovalev 2017-05-10 18:27:25 +02:00
parent 9ad754ba04
commit 2c44953b9b
3 changed files with 16 additions and 7 deletions

View File

@ -62,5 +62,5 @@ func Dial(url string, tr transport.Transport) (*Client, error) {
Close client connection Close client connection
*/ */
func (c *Client) Close() { func (c *Client) Close() {
CloseChannel(&c.Channel, &c.methods) closeChannel(&c.Channel, &c.methods)
} }

12
loop.go
View File

@ -83,7 +83,7 @@ func (c *Channel) IsAlive() bool {
/** /**
Close channel Close channel
*/ */
func CloseChannel(c *Channel, m *methods, args ...interface{}) error { func closeChannel(c *Channel, m *methods, args ...interface{}) error {
c.aliveLock.Lock() c.aliveLock.Lock()
defer c.aliveLock.Unlock() defer c.aliveLock.Unlock()
@ -115,18 +115,18 @@ func inLoop(c *Channel, m *methods) error {
for { for {
pkg, err := c.conn.GetMessage() pkg, err := c.conn.GetMessage()
if err != nil { if err != nil {
return CloseChannel(c, m, err) return closeChannel(c, m, err)
} }
msg, err := protocol.Decode(pkg) msg, err := protocol.Decode(pkg)
if err != nil { if err != nil {
CloseChannel(c, m, protocol.ErrorWrongPacket) closeChannel(c, m, protocol.ErrorWrongPacket)
return err return err
} }
switch msg.Type { switch msg.Type {
case protocol.MessageTypeOpen: case protocol.MessageTypeOpen:
if err := json.Unmarshal([]byte(msg.Source[1:]), &c.header); err != nil { if err := json.Unmarshal([]byte(msg.Source[1:]), &c.header); err != nil {
CloseChannel(c, m, ErrorWrongHeader) closeChannel(c, m, ErrorWrongHeader)
} }
m.callLoopEvent(c, OnConnection) m.callLoopEvent(c, OnConnection)
case protocol.MessageTypePing: case protocol.MessageTypePing:
@ -156,7 +156,7 @@ func outLoop(c *Channel, m *methods) error {
for { for {
outBufferLen := len(c.out) outBufferLen := len(c.out)
if outBufferLen >= queueBufferSize-1 { if outBufferLen >= queueBufferSize-1 {
return CloseChannel(c, m, ErrorSocketOverflood) return closeChannel(c, m, ErrorSocketOverflood)
} else if outBufferLen > int(queueBufferSize/2) { } else if outBufferLen > int(queueBufferSize/2) {
overfloodedLock.Lock() overfloodedLock.Lock()
overflooded[c] = struct{}{} overflooded[c] = struct{}{}
@ -174,7 +174,7 @@ func outLoop(c *Channel, m *methods) error {
err := c.conn.WriteMessage(msg) err := c.conn.WriteMessage(msg)
if err != nil { if err != nil {
return CloseChannel(c, m, err) return closeChannel(c, m, err)
} }
} }
return nil return nil

View File

@ -41,6 +41,15 @@ type Server struct {
tr transport.Transport tr transport.Transport
} }
/**
Close current channel
*/
func (c *Channel) Close() {
if c.server != nil {
closeChannel(c, &c.server.methods)
}
}
/** /**
Get ip of socket client Get ip of socket client
*/ */