You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Open-IM-Server/internal/rpc/msg/rpcChat.go

100 lines
2.6 KiB

package msg
4 years ago
import (
"Open_IM/pkg/common/config"
3 years ago
"Open_IM/pkg/common/constant"
3 years ago
"Open_IM/pkg/common/db"
"Open_IM/pkg/common/kafka"
"Open_IM/pkg/common/log"
"Open_IM/pkg/grpc-etcdv3/getcdv3"
pbChat "Open_IM/pkg/proto/chat"
3 years ago
"Open_IM/pkg/utils"
"google.golang.org/grpc"
4 years ago
"net"
"strconv"
"strings"
)
type rpcChat struct {
rpcPort int
rpcRegisterName string
etcdSchema string
etcdAddr []string
onlineProducer *kafka.Producer
offlineProducer *kafka.Producer
3 years ago
delMsgCh chan deleteMsg
}
type deleteMsg struct {
UserID string
OpUserID string
SeqList []uint32
OperationID string
4 years ago
}
func NewRpcChatServer(port int) *rpcChat {
3 years ago
log.NewPrivateLog(constant.LogFileName)
4 years ago
rc := rpcChat{
rpcPort: port,
rpcRegisterName: config.Config.RpcRegisterName.OpenImOfflineMessageName,
etcdSchema: config.Config.Etcd.EtcdSchema,
etcdAddr: config.Config.Etcd.EtcdAddr,
}
rc.onlineProducer = kafka.NewKafkaProducer(config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.Ws2mschat.Topic)
rc.offlineProducer = kafka.NewKafkaProducer(config.Config.Kafka.Ws2mschatOffline.Addr, config.Config.Kafka.Ws2mschatOffline.Topic)
3 years ago
rc.delMsgCh = make(chan deleteMsg, 1000)
4 years ago
return &rc
}
func (rpc *rpcChat) Run() {
3 years ago
log.Info("", "rpcChat init...")
listenIP := ""
if config.Config.ListenIP == "" {
listenIP = "0.0.0.0"
} else {
listenIP = config.Config.ListenIP
}
address := listenIP + ":" + strconv.Itoa(rpc.rpcPort)
4 years ago
listener, err := net.Listen("tcp", address)
if err != nil {
3 years ago
panic("listening err:" + err.Error() + rpc.rpcRegisterName)
4 years ago
}
3 years ago
log.Info("", "listen network success, address ", address)
4 years ago
srv := grpc.NewServer()
defer srv.GracefulStop()
3 years ago
rpcRegisterIP := ""
4 years ago
pbChat.RegisterChatServer(srv, rpc)
3 years ago
if config.Config.RpcRegisterIP == "" {
rpcRegisterIP, err = utils.GetLocalIP()
if err != nil {
log.Error("", "GetLocalIP failed ", err.Error())
}
}
err = getcdv3.RegisterEtcd(rpc.etcdSchema, strings.Join(rpc.etcdAddr, ","), rpcRegisterIP, rpc.rpcPort, rpc.rpcRegisterName, 10)
4 years ago
if err != nil {
3 years ago
log.Error("", "register rpcChat to etcd failed ", err.Error())
4 years ago
return
}
3 years ago
go rpc.runCh()
4 years ago
err = srv.Serve(listener)
if err != nil {
3 years ago
log.Error("", "rpc rpcChat failed ", err.Error())
4 years ago
return
}
3 years ago
log.Info("", "rpc rpcChat init success")
4 years ago
}
3 years ago
func (rpc *rpcChat) runCh() {
log.NewInfo("", "start del msg chan ")
for {
select {
case msg := <-rpc.delMsgCh:
if err := db.DB.DelMsgBySeqList(msg.UserID, msg.SeqList, msg.OperationID); err != nil {
log.NewError(msg.OperationID, utils.GetSelfFuncName(), "DelMsgBySeqList qrgs: ", msg.UserID, msg.SeqList, msg.OperationID, err.Error())
}
}
}
}