server-go/socket/readwrite.go

124 lines
2.7 KiB
Go
Raw Normal View History

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-06-29 10:02:40 +00:00
type SocketMessage func() (int, []byte)
func MakeSocketMessage(messageType int, message []byte) SocketMessage {
return func() (int, []byte) { return messageType, message }
}
func connReadHandler(readWriteHandler ReadWriteHandler, conn Conn, stopChan <-chan struct{}, doneChan chan<- error, readChan chan<- SocketMessage) {
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-06-29 10:02:40 +00:00
var messageType int
2018-04-04 13:28:35 +00:00
readMessageChan := make(chan struct{})
go func() {
2018-06-29 10:02:40 +00:00
messageType, message, err = conn.ReadMessage()
2018-04-04 13:28:35 +00:00
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
}
2018-06-29 10:02:40 +00:00
readChan <- MakeSocketMessage(messageType, message)
2018-04-04 13:28:35 +00:00
}
}
2018-06-29 10:02:40 +00:00
func connWriteHandler(readWriteHandler ReadWriteHandler, conn Conn, stopChan <-chan struct{}, doneChan chan<- error, writeChan <-chan SocketMessage) {
2018-04-04 13:28:35 +00:00
var (
2018-06-29 10:02:40 +00:00
socketMessage SocketMessage
message []byte
messageType int
ok bool
err error
2018-04-04 13:28:35 +00:00
)
defer func() {
doneChan <- err
}()
ticker := time.NewTicker(readWriteHandler.GetPingPeriod())
defer func() {
ticker.Stop()
}()
for {
select {
2018-06-29 10:02:40 +00:00
case socketMessage, 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-29 10:02:40 +00:00
messageType, message = socketMessage()
err = conn.WriteMessage(messageType, 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
}
}
}