|
|
@ -2,16 +2,15 @@ package new
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"Open_IM/pkg/common/constant"
|
|
|
|
"Open_IM/pkg/common/constant"
|
|
|
|
promePkg "Open_IM/pkg/common/prometheus"
|
|
|
|
"Open_IM/pkg/utils"
|
|
|
|
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"github.com/envoyproxy/protoc-gen-validate/validate"
|
|
|
|
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"github.com/go-playground/validator/v10"
|
|
|
|
"open_im_sdk/pkg/log"
|
|
|
|
|
|
|
|
"open_im_sdk/pkg/utils"
|
|
|
|
|
|
|
|
"runtime/debug"
|
|
|
|
"runtime/debug"
|
|
|
|
"sync"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
const (
|
|
|
@ -50,6 +49,7 @@ type Client struct {
|
|
|
|
encoder Encoder
|
|
|
|
encoder Encoder
|
|
|
|
userContext UserConnContext
|
|
|
|
userContext UserConnContext
|
|
|
|
validate *validator.Validate
|
|
|
|
validate *validator.Validate
|
|
|
|
|
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func newClient(conn LongConn, isCompress bool, userID string, isBackground bool, token string,
|
|
|
|
func newClient(conn LongConn, isCompress bool, userID string, isBackground bool, token string,
|
|
|
@ -57,8 +57,7 @@ func newClient( conn LongConn,isCompress bool, userID string, isBackground boo
|
|
|
|
return &Client{
|
|
|
|
return &Client{
|
|
|
|
conn: conn,
|
|
|
|
conn: conn,
|
|
|
|
IsCompress: isCompress,
|
|
|
|
IsCompress: isCompress,
|
|
|
|
userID: userID, IsBackground:
|
|
|
|
userID: userID, IsBackground: isBackground, token: token,
|
|
|
|
isBackground, token: token,
|
|
|
|
|
|
|
|
connID: connID,
|
|
|
|
connID: connID,
|
|
|
|
onlineAt: onlineAt,
|
|
|
|
onlineAt: onlineAt,
|
|
|
|
handler: handler,
|
|
|
|
handler: handler,
|
|
|
@ -78,6 +77,9 @@ func(c *Client) readMessage(){
|
|
|
|
if returnErr != nil {
|
|
|
|
if returnErr != nil {
|
|
|
|
break
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.closed == true {
|
|
|
|
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
switch messageType {
|
|
|
|
switch messageType {
|
|
|
|
case PingMessage:
|
|
|
|
case PingMessage:
|
|
|
|
case PongMessage:
|
|
|
|
case PongMessage:
|
|
|
@ -137,12 +139,48 @@ func (c *Client) handleMessage(message []byte)error {
|
|
|
|
default:
|
|
|
|
default:
|
|
|
|
return errors.New(fmt.Sprintf("ReqIdentifier failed,sendID:%d,msgIncr:%s,reqIdentifier:%s", binaryReq.SendID, binaryReq.MsgIncr, binaryReq.ReqIdentifier))
|
|
|
|
return errors.New(fmt.Sprintf("ReqIdentifier failed,sendID:%d,msgIncr:%s,reqIdentifier:%s", binaryReq.SendID, binaryReq.MsgIncr, binaryReq.ReqIdentifier))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.replyMessage(binaryReq, messageErr, resp)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *Client) close() {
|
|
|
|
func (c *Client) close() {
|
|
|
|
|
|
|
|
c.w.Lock()
|
|
|
|
|
|
|
|
defer c.w.Unlock()
|
|
|
|
|
|
|
|
c.conn.Close()
|
|
|
|
|
|
|
|
c.unregisterChan <- c
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func () {
|
|
|
|
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)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|