Files
open-im-server/internal/msggateway/new/client.go
T

187 lines
4.8 KiB
Go
Raw Normal View History

2023-02-14 21:08:36 +08:00
package new
import (
"Open_IM/pkg/common/constant"
2023-02-15 19:57:16 +08:00
"Open_IM/pkg/utils"
"bytes"
2023-02-14 21:08:36 +08:00
"context"
"errors"
"fmt"
"github.com/go-playground/validator/v10"
"runtime/debug"
"sync"
2023-02-15 19:57:16 +08:00
"time"
2023-02-14 21:08:36 +08:00
)
const (
// MessageText is for UTF-8 encoded text messages like JSON.
2023-02-15 19:57:16 +08:00
MessageText = iota + 1
2023-02-14 21:08:36 +08:00
// MessageBinary is for binary messages like protobufs.
MessageBinary
// CloseMessage denotes a close control message. The optional message
// payload contains a numeric code and text. Use the FormatCloseMessage
// function to format a close message payload.
CloseMessage = 8
// PingMessage denotes a ping control message. The optional message payload
// is UTF-8 encoded text.
PingMessage = 9
// PongMessage denotes a pong control message. The optional message payload
// is UTF-8 encoded text.
PongMessage = 10
)
type Client struct {
2023-02-15 19:57:16 +08:00
w *sync.Mutex
conn LongConn
PlatformID int32
PushedMaxSeq uint32
IsCompress bool
userID string
IsBackground bool
token string
connID string
onlineAt int64 // 上线时间戳(毫秒)
handler MessageHandler
unregisterChan chan *Client
2023-02-14 21:08:36 +08:00
compressor Compressor
encoder Encoder
2023-02-15 19:57:16 +08:00
userContext UserConnContext
validate *validator.Validate
closed bool
2023-02-14 21:08:36 +08:00
}
2023-02-15 19:57:16 +08:00
func newClient(conn LongConn, isCompress bool, userID string, isBackground bool, token string,
connID string, onlineAt int64, handler MessageHandler, unregisterChan chan *Client) *Client {
2023-02-14 21:08:36 +08:00
return &Client{
2023-02-15 19:57:16 +08:00
conn: conn,
2023-02-14 21:08:36 +08:00
IsCompress: isCompress,
2023-02-15 19:57:16 +08:00
userID: userID, IsBackground: isBackground, token: token,
connID: connID,
onlineAt: onlineAt,
handler: handler,
unregisterChan: unregisterChan,
2023-02-14 21:08:36 +08:00
}
}
2023-02-15 19:57:16 +08:00
func (c *Client) readMessage() {
2023-02-14 21:08:36 +08:00
defer func() {
2023-02-15 19:57:16 +08:00
if r := recover(); r != nil {
2023-02-14 21:08:36 +08:00
fmt.Println("socket have panic err:", r, string(debug.Stack()))
}
//c.close()
}()
2023-02-15 19:57:16 +08:00
var returnErr error
for {
messageType, message, returnErr := c.conn.ReadMessage()
if returnErr != nil {
break
}
if c.closed == true {
2023-02-14 21:08:36 +08:00
break
}
switch messageType {
case PingMessage:
case PongMessage:
case CloseMessage:
return
case MessageText:
case MessageBinary:
if len(message) == 0 {
continue
}
returnErr = c.handleMessage(message)
2023-02-15 19:57:16 +08:00
if returnErr != nil {
2023-02-14 21:08:36 +08:00
break
}
}
}
}
2023-02-15 19:57:16 +08:00
func (c *Client) handleMessage(message []byte) error {
if c.IsCompress {
2023-02-14 21:08:36 +08:00
var decompressErr error
2023-02-15 19:57:16 +08:00
message, decompressErr = c.compressor.DeCompress(message)
2023-02-14 21:08:36 +08:00
if decompressErr != nil {
2023-02-15 19:57:16 +08:00
return utils.Wrap(decompressErr, "")
2023-02-14 21:08:36 +08:00
}
}
2023-02-15 19:57:16 +08:00
var binaryReq Req
2023-02-14 21:08:36 +08:00
err := c.encoder.Decode(message, &binaryReq)
if err != nil {
2023-02-15 19:57:16 +08:00
return utils.Wrap(err, "")
2023-02-14 21:08:36 +08:00
}
if err := c.validate.Struct(binaryReq); err != nil {
2023-02-15 19:57:16 +08:00
return utils.Wrap(err, "")
2023-02-14 21:08:36 +08:00
}
if binaryReq.SendID != c.userID {
return errors.New("exception conn userID not same to req userID")
}
2023-02-15 19:57:16 +08:00
ctx := context.Background()
ctx = context.WithValue(ctx, "operationID", binaryReq.OperationID)
ctx = context.WithValue(ctx, "userID", binaryReq.SendID)
2023-02-14 21:08:36 +08:00
var messageErr error
2023-02-15 19:57:16 +08:00
var resp []byte
2023-02-14 21:08:36 +08:00
switch binaryReq.ReqIdentifier {
case constant.WSGetNewestSeq:
2023-02-15 19:57:16 +08:00
resp, messageErr = c.handler.GetSeq(ctx, binaryReq)
2023-02-14 21:08:36 +08:00
case constant.WSSendMsg:
2023-02-15 19:57:16 +08:00
resp, messageErr = c.handler.SendMessage(ctx, binaryReq)
2023-02-14 21:08:36 +08:00
case constant.WSSendSignalMsg:
2023-02-15 19:57:16 +08:00
resp, messageErr = c.handler.SendSignalMessage(ctx, binaryReq)
2023-02-14 21:08:36 +08:00
case constant.WSPullMsgBySeqList:
2023-02-15 19:57:16 +08:00
resp, messageErr = c.handler.PullMessageBySeqList(ctx, binaryReq)
2023-02-14 21:08:36 +08:00
case constant.WsLogoutMsg:
2023-02-15 19:57:16 +08:00
resp, messageErr = c.handler.UserLogout(ctx, binaryReq)
2023-02-14 21:08:36 +08:00
case constant.WsSetBackgroundStatus:
2023-02-15 19:57:16 +08:00
resp, messageErr = c.handler.SetUserDeviceBackground(ctx, binaryReq)
2023-02-14 21:08:36 +08:00
default:
2023-02-15 19:57:16 +08:00
return errors.New(fmt.Sprintf("ReqIdentifier failed,sendID:%d,msgIncr:%s,reqIdentifier:%s", binaryReq.SendID, binaryReq.MsgIncr, binaryReq.ReqIdentifier))
2023-02-14 21:08:36 +08:00
}
2023-02-15 19:57:16 +08:00
c.replyMessage(binaryReq, messageErr, resp)
return nil
2023-02-14 21:08:36 +08:00
}
2023-02-15 19:57:16 +08:00
func (c *Client) close() {
c.w.Lock()
defer c.w.Unlock()
c.conn.Close()
c.unregisterChan <- c
2023-02-14 21:08:36 +08:00
}
2023-02-15 19:57:16 +08:00
func (c *Client) replyMessage(binaryReq Req, err error, resp []byte) {
mReply := Resp{
ReqIdentifier: binaryReq.ReqIdentifier,
MsgIncr: binaryReq.MsgIncr,
OperationID: binaryReq.OperationID,
Data: resp,
}
_ = c.writeMsg(mReply)
}
func (c *Client) writeMsg(resp Resp) error {
c.w.Lock()
defer c.w.Unlock()
if c.closed == true {
return nil
}
encodedBuf := bufferPool.Get().([]byte)
resultBuf := bufferPool.Get().([]byte)
encodeBuf, err := c.encoder.Encode(resp)
if err != nil {
return utils.Wrap(err, "")
}
_ = c.conn.SetWriteTimeout(60)
if c.IsCompress {
var compressErr error
resultBuf, compressErr = c.compressor.Compress(encodeBuf)
if compressErr != nil {
return utils.Wrap(compressErr, "")
}
return c.conn.WriteMessage(MessageBinary, resultBuf)
} else {
return c.conn.WriteMessage(MessageBinary, encodedBuf)
}
2023-02-14 21:08:36 +08:00
}