2018-04-05 15:15:29 +00:00
|
|
|
package socket
|
2018-04-04 13:28:35 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-04-13 10:13:37 +00:00
|
|
|
|
|
|
|
logging "git.loafle.net/commons/logging-go"
|
2018-04-04 13:28:35 +00:00
|
|
|
)
|
|
|
|
|
2018-04-04 16:51:34 +00:00
|
|
|
func connReadHandler(readWriteHandler ReadWriteHandler, conn Conn, stopChan <-chan struct{}, doneChan chan<- error, readChan chan<- []byte) {
|
2018-04-04 13:28:35 +00:00
|
|
|
var (
|
2018-04-13 10:43:14 +00:00
|
|
|
err error
|
2018-04-04 13:28:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
doneChan <- err
|
|
|
|
}()
|
|
|
|
|
2018-04-13 10:07:44 +00:00
|
|
|
if 0 < readWriteHandler.GetMaxMessageSize() {
|
|
|
|
conn.SetReadLimit(readWriteHandler.GetMaxMessageSize())
|
|
|
|
}
|
2018-04-13 09:45:24 +00:00
|
|
|
|
2018-04-13 10:07:44 +00:00
|
|
|
if 0 < readWriteHandler.GetReadTimeout() {
|
|
|
|
conn.SetReadDeadline(time.Now().Add(readWriteHandler.GetReadTimeout()))
|
|
|
|
} else {
|
|
|
|
conn.SetReadDeadline(time.Time{})
|
|
|
|
}
|
2018-04-04 13:28:35 +00:00
|
|
|
conn.SetPongHandler(func(string) error {
|
2018-04-13 10:07:44 +00:00
|
|
|
if 0 < readWriteHandler.GetPongTimeout() {
|
|
|
|
conn.SetReadDeadline(time.Now().Add(readWriteHandler.GetPongTimeout()))
|
|
|
|
} else {
|
|
|
|
conn.SetReadDeadline(time.Time{})
|
|
|
|
}
|
2018-04-04 13:28:35 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
for {
|
2018-04-13 10:43:14 +00:00
|
|
|
var message []byte
|
2018-04-04 13:28:35 +00:00
|
|
|
readMessageChan := make(chan struct{})
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, message, err = conn.ReadMessage()
|
|
|
|
close(readMessageChan)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
case <-readMessageChan:
|
|
|
|
}
|
|
|
|
|
|
|
|
if nil != err {
|
|
|
|
if IsUnexpectedCloseError(err, CloseGoingAway, CloseAbnormalClosure) {
|
|
|
|
err = fmt.Errorf("Read error %v", err)
|
|
|
|
}
|
2018-04-13 10:13:37 +00:00
|
|
|
logging.Logger().Debug(err)
|
2018-04-04 13:28:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
readChan <- message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 16:51:34 +00:00
|
|
|
func connWriteHandler(readWriteHandler ReadWriteHandler, conn Conn, stopChan <-chan struct{}, doneChan chan<- error, writeChan <-chan []byte) {
|
2018-04-04 13:28:35 +00:00
|
|
|
var (
|
|
|
|
message []byte
|
|
|
|
ok bool
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
doneChan <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
ticker := time.NewTicker(readWriteHandler.GetPingPeriod())
|
|
|
|
defer func() {
|
|
|
|
ticker.Stop()
|
|
|
|
}()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case message, ok = <-writeChan:
|
2018-04-13 10:07:44 +00:00
|
|
|
if 0 < readWriteHandler.GetWriteTimeout() {
|
|
|
|
conn.SetWriteDeadline(time.Now().Add(readWriteHandler.GetWriteTimeout()))
|
|
|
|
} else {
|
|
|
|
conn.SetWriteDeadline(time.Time{})
|
|
|
|
}
|
2018-04-04 13:28:35 +00:00
|
|
|
if !ok {
|
|
|
|
conn.WriteMessage(CloseMessage, []byte{})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-27 12:31:50 +00:00
|
|
|
err = conn.WriteCompress(TextMessage, message)
|
2018-04-04 13:28:35 +00:00
|
|
|
if err != nil {
|
2018-04-13 10:13:37 +00:00
|
|
|
logging.Logger().Debug(err)
|
2018-04-04 13:28:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-ticker.C:
|
2018-04-13 10:07:44 +00:00
|
|
|
if 0 < readWriteHandler.GetPingTimeout() {
|
|
|
|
conn.SetWriteDeadline(time.Now().Add(readWriteHandler.GetPingTimeout()))
|
|
|
|
} else {
|
|
|
|
conn.SetWriteDeadline(time.Time{})
|
|
|
|
}
|
2018-04-04 13:28:35 +00:00
|
|
|
if err = conn.WriteMessage(PingMessage, nil); nil != err {
|
2018-04-13 10:13:37 +00:00
|
|
|
logging.Logger().Debug(err)
|
2018-04-04 13:28:35 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case <-stopChan:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|