Files
open-im-server/internal/msg_gateway/gate/rpc_server.go
T

244 lines
8.0 KiB
Go
Raw Normal View History

2021-05-26 19:24:25 +08:00
package gate
import (
"Open_IM/pkg/common/config"
"Open_IM/pkg/common/constant"
"Open_IM/pkg/common/log"
2021-12-28 20:44:19 +08:00
"Open_IM/pkg/common/token_verify"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
2021-10-11 22:00:38 +08:00
pbRelay "Open_IM/pkg/proto/relay"
"Open_IM/pkg/utils"
"bytes"
2021-05-26 19:24:25 +08:00
"context"
"encoding/gob"
2021-12-17 15:55:43 +08:00
"github.com/golang/protobuf/proto"
2021-05-26 19:24:25 +08:00
"net"
2022-05-07 17:05:05 +08:00
"strconv"
2021-05-26 19:24:25 +08:00
"strings"
"github.com/gorilla/websocket"
"google.golang.org/grpc"
2021-05-26 19:24:25 +08:00
)
type RPCServer struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
2022-06-02 16:44:55 +08:00
platformList []int
pushTerminal []int
2021-05-26 19:24:25 +08:00
}
func (r *RPCServer) onInit(rpcPort int) {
r.rpcPort = rpcPort
r.rpcRegisterName = config.Config.RpcRegisterName.OpenImOnlineMessageRelayName
r.etcdSchema = config.Config.Etcd.EtcdSchema
r.etcdAddr = config.Config.Etcd.EtcdAddr
2022-06-02 16:44:55 +08:00
r.platformList = genPlatformArray()
r.pushTerminal = []int{constant.IOSPlatformID, constant.AndroidPlatformID}
2021-05-26 19:24:25 +08:00
}
func (r *RPCServer) run() {
2022-05-07 17:05:05 +08:00
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(r.rpcPort)
listener, err := net.Listen("tcp", address)
2021-05-26 19:24:25 +08:00
if err != nil {
2022-05-10 09:09:37 +08:00
panic("listening err:" + err.Error() + r.rpcRegisterName)
2021-05-26 19:24:25 +08:00
}
defer listener.Close()
srv := grpc.NewServer()
defer srv.GracefulStop()
pbRelay.RegisterOnlineMessageRelayServiceServer(srv, r)
2022-05-07 17:05:05 +08:00
rpcRegisterIP := ""
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
err = getcdv3.RegisterEtcd4Unique(r.etcdSchema, strings.Join(r.etcdAddr, ","), rpcRegisterIP, r.rpcPort, r.rpcRegisterName, 10)
2021-05-26 19:24:25 +08:00
if err != nil {
2022-05-07 17:05:05 +08:00
log.Error("", "register push message rpc to etcd err", "", "err", err.Error(), r.etcdSchema, strings.Join(r.etcdAddr, ","), rpcRegisterIP, r.rpcPort, r.rpcRegisterName)
2021-05-26 19:24:25 +08:00
}
err = srv.Serve(listener)
if err != nil {
2022-05-07 17:05:05 +08:00
log.Error("", "push message rpc listening err", "", "err", err.Error())
2021-05-26 19:24:25 +08:00
return
}
}
2021-12-23 17:34:32 +08:00
func (r *RPCServer) OnlinePushMsg(_ context.Context, in *pbRelay.OnlinePushMsgReq) (*pbRelay.OnlinePushMsgResp, error) {
2022-05-05 16:13:45 +08:00
log.NewInfo(in.OperationID, "PushMsgToUser is arriving", in.String())
2022-06-02 16:44:55 +08:00
var resp []*pbRelay.SingleMsgToUserPlatform
2021-12-23 17:34:32 +08:00
msgBytes, _ := proto.Marshal(in.MsgData)
mReply := Resp{
ReqIdentifier: constant.WSPushMsg,
OperationID: in.OperationID,
Data: msgBytes,
}
var replyBytes bytes.Buffer
enc := gob.NewEncoder(&replyBytes)
err := enc.Encode(mReply)
if err != nil {
log.NewError(in.OperationID, "data encode err", err.Error())
}
var tag bool
2022-02-08 17:12:02 +08:00
recvID := in.PushToUserID
2022-06-02 16:44:55 +08:00
for _, v := range r.platformList {
2021-12-23 17:34:32 +08:00
if conn := ws.getUserConn(recvID, v); conn != nil {
tag = true
2021-12-23 17:34:32 +08:00
resultCode := sendMsgToUser(conn, replyBytes.Bytes(), in, v, recvID)
2022-06-02 16:44:55 +08:00
temp := &pbRelay.SingleMsgToUserPlatform{
2021-06-28 15:34:08 +08:00
ResultCode: resultCode,
2021-12-23 17:34:32 +08:00
RecvID: recvID,
2022-06-02 16:44:55 +08:00
RecvPlatFormID: int32(v),
2021-05-26 19:24:25 +08:00
}
2021-06-28 15:34:08 +08:00
resp = append(resp, temp)
} else {
2022-06-02 16:44:55 +08:00
temp := &pbRelay.SingleMsgToUserPlatform{
ResultCode: -1,
2021-12-23 17:34:32 +08:00
RecvID: recvID,
2022-06-02 16:44:55 +08:00
RecvPlatFormID: int32(v),
}
resp = append(resp, temp)
2021-05-26 19:24:25 +08:00
}
}
if !tag {
2022-05-05 16:13:45 +08:00
log.NewDebug(in.OperationID, "push err ,no matched ws conn not in map", in.String())
}
2021-12-23 17:34:32 +08:00
return &pbRelay.OnlinePushMsgResp{
2021-05-26 19:24:25 +08:00
Resp: resp,
}, nil
}
2021-11-29 16:26:57 +08:00
func (r *RPCServer) GetUsersOnlineStatus(_ context.Context, req *pbRelay.GetUsersOnlineStatusReq) (*pbRelay.GetUsersOnlineStatusResp, error) {
2021-12-28 20:44:19 +08:00
log.NewInfo(req.OperationID, "rpc GetUsersOnlineStatus arrived server", req.String())
2022-04-16 20:10:10 +08:00
if !token_verify.IsManagerUserID(req.OpUserID) {
2021-12-28 20:44:19 +08:00
log.NewError(req.OperationID, "no permission GetUsersOnlineStatus ", req.OpUserID)
return &pbRelay.GetUsersOnlineStatusResp{ErrCode: constant.ErrAccess.ErrCode, ErrMsg: constant.ErrAccess.ErrMsg}, nil
}
2021-11-29 16:26:57 +08:00
var resp pbRelay.GetUsersOnlineStatusResp
2021-12-23 17:34:32 +08:00
for _, userID := range req.UserIDList {
2021-11-29 16:26:57 +08:00
temp := new(pbRelay.GetUsersOnlineStatusResp_SuccessResult)
2021-12-23 17:34:32 +08:00
temp.UserID = userID
2022-06-02 16:44:55 +08:00
userConnMap := ws.getUserAllCons(userID)
for platform, userConn := range userConnMap {
if userConn != nil {
2021-11-29 16:26:57 +08:00
ps := new(pbRelay.GetUsersOnlineStatusResp_SuccessDetail)
2022-06-02 16:44:55 +08:00
ps.Platform = constant.PlatformIDToName(platform)
2021-11-29 16:26:57 +08:00
ps.Status = constant.OnlineStatus
temp.Status = constant.OnlineStatus
temp.DetailPlatformStatus = append(temp.DetailPlatformStatus, ps)
}
}
2022-06-02 16:44:55 +08:00
2021-11-29 16:26:57 +08:00
if temp.Status == constant.OnlineStatus {
resp.SuccessResult = append(resp.SuccessResult, temp)
}
}
2021-12-28 20:44:19 +08:00
log.NewInfo(req.OperationID, "GetUsersOnlineStatus rpc return ", resp.String())
2021-11-29 16:26:57 +08:00
return &resp, nil
}
2022-06-02 16:44:55 +08:00
func (r *RPCServer) OnlineBatchPushOneMsg(_ context.Context, req *pbRelay.OnlineBatchPushOneMsgReq) (*pbRelay.OnlineBatchPushOneMsgResp, error) {
log.NewInfo(req.OperationID, "BatchPushMsgToUser is arriving", req.String())
var singleUserResult []*pbRelay.SingelMsgToUserResultList
msgBytes, _ := proto.Marshal(req.MsgData)
mReply := Resp{
ReqIdentifier: constant.WSPushMsg,
OperationID: req.OperationID,
Data: msgBytes,
}
var replyBytes bytes.Buffer
enc := gob.NewEncoder(&replyBytes)
err := enc.Encode(mReply)
if err != nil {
log.NewError(req.OperationID, "data encode err", err.Error())
}
for _, v := range req.PushToUserIDList {
var resp []*pbRelay.SingleMsgToUserPlatform
userConnMap := ws.getUserAllCons(v)
for platform, userConn := range userConnMap {
if userConn != nil {
resultCode := sendMsgBatchToUser(userConn, replyBytes.Bytes(), req, platform, v)
if resultCode == 0 && utils.IsContainInt(platform, r.pushTerminal) { //仅仅记录推送成功的平台端
temp := &pbRelay.SingleMsgToUserPlatform{
ResultCode: resultCode,
RecvID: v,
RecvPlatFormID: int32(platform),
}
resp = append(resp, temp)
}
}
}
//for _, x := range r.platformList {
// if conn := ws.getUserConn(v, x); conn != nil {
// resultCode := sendMsgBatchToUser(conn, replyBytes.Bytes(), req, x, v)
// temp := &pbRelay.SingleMsgToUserPlatform{
// ResultCode: resultCode,
// RecvID: v,
// RecvPlatFormID: constant.PlatformNameToID(x),
// }
// resp = append(resp, temp)
// } else {
// if utils.IsContain(x,r.pushTerminal) {
// temp := &pbRelay.SingleMsgToUserPlatform{
// ResultCode: -1,
// RecvID: v,
// RecvPlatFormID: constant.PlatformNameToID(x),
// }
// resp = append(resp, temp)
// }
//
// }
//}
tempT := &pbRelay.SingelMsgToUserResultList{
UserID: v,
Resp: resp,
}
singleUserResult = append(singleUserResult, tempT)
}
return &pbRelay.OnlineBatchPushOneMsgResp{
SinglePushResult: singleUserResult,
}, nil
}
func sendMsgToUser(conn *UserConn, bMsg []byte, in *pbRelay.OnlinePushMsgReq, RecvPlatForm int, RecvID string) (ResultCode int64) {
err := ws.writeMsg(conn, websocket.BinaryMessage, bMsg)
if err != nil {
log.NewError(in.OperationID, "PushMsgToUser is failed By Ws", "Addr", conn.RemoteAddr().String(),
"error", err, "senderPlatform", constant.PlatformIDToName(int(in.MsgData.SenderPlatformID)), "recvPlatform", RecvPlatForm, "args", in.String(), "recvID", RecvID)
ResultCode = -2
return ResultCode
} else {
log.NewDebug(in.OperationID, "PushMsgToUser is success By Ws", "args", in.String(), "recvPlatForm", RecvPlatForm, "recvID", RecvID)
ResultCode = 0
return ResultCode
}
}
func sendMsgBatchToUser(conn *UserConn, bMsg []byte, in *pbRelay.OnlineBatchPushOneMsgReq, RecvPlatForm int, RecvID string) (ResultCode int64) {
err := ws.writeMsg(conn, websocket.BinaryMessage, bMsg)
2021-05-26 19:24:25 +08:00
if err != nil {
2022-05-05 16:13:45 +08:00
log.NewError(in.OperationID, "PushMsgToUser is failed By Ws", "Addr", conn.RemoteAddr().String(),
2022-06-02 16:44:55 +08:00
"error", err, "senderPlatform", constant.PlatformIDToName(int(in.MsgData.SenderPlatformID)), "recvPlatform", RecvPlatForm, "args", in.String(), "recvID", RecvID)
2021-05-26 19:24:25 +08:00
ResultCode = -2
return ResultCode
} else {
2022-05-05 16:13:45 +08:00
log.NewDebug(in.OperationID, "PushMsgToUser is success By Ws", "args", in.String(), "recvPlatForm", RecvPlatForm, "recvID", RecvID)
2021-05-26 19:24:25 +08:00
ResultCode = 0
return ResultCode
}
}
2022-06-02 16:44:55 +08:00
func genPlatformArray() (array []int) {
2021-11-25 14:12:52 +08:00
for i := 1; i <= constant.LinuxPlatformID; i++ {
2022-06-02 16:44:55 +08:00
array = append(array, i)
}
return array
}