1 Star 1 Fork 0

凡卡/libp2parea

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
handers.go 2.73 KB
一键复制 编辑 原始数据 按行查看 历史
package multicast
import (
"gitee.com/prestonTao/libp2parea/engine"
mc "gitee.com/prestonTao/libp2parea/message_center"
"gitee.com/prestonTao/libp2parea/message_center/flood"
"gitee.com/prestonTao/libp2parea/nodeStore"
"gitee.com/prestonTao/libp2parea/protobuf/go_protobuf"
"gitee.com/prestonTao/utils"
"github.com/gogo/protobuf/proto"
)
// 注册消息
func (gm *MulticastMsg) registerMsg() {
if gm == nil || gm.Area == nil || !gm.initialized {
return
}
gm.Area.Register_p2p(MSGID_P2P_SEND_MULTICAST_MSG, gm.sendMulticastMsg)
gm.Area.Register_p2p(MSGID_P2P_SEND_MULTICAST_MSG_RECV, gm.sendMulticastMsg_recv)
}
// 发送组播消息
func (gm *MulticastMsg) sendMulticastMsg(c engine.Controller, msg engine.Packet, message *mc.Message) {
// 1. 判断消息内容
if message == nil || message.Body == nil || message.Body.Content == nil {
return
}
// 2. 解析组播消息
multicastMsgInfo := new(go_protobuf.MulticastMsg)
err := proto.Unmarshal(*message.Body.Content, multicastMsgInfo)
if err != nil {
engine.Log.Error("解析组播消息失败 err:%s", err)
return
}
// 2.1 验证解析的组播消息参数信息
if multicastMsgInfo.MsgID == 0 || len(multicastMsgInfo.Nodes) == 0 {
engine.Log.Error("组播消息参数有误 msgID:%d nodeLen:%d", multicastMsgInfo.MsgID, len(multicastMsgInfo.Nodes))
return
}
// 3. 依次发送消息
var content []byte
if multicastMsgInfo.Content != "" {
content = []byte(multicastMsgInfo.Content)
}
for i := range multicastMsgInfo.Nodes {
nodeId := nodeStore.AddressNet(multicastMsgInfo.Nodes[i].Id)
exist, proxyInfoes := gm.Area.ProxyData.GetNodeIdProxy2(&nodeId, multicastMsgInfo.Nodes[i].MachineID)
if !exist || len(proxyInfoes) == 0 {
// 没有代理, 直接发送消息
engine.Log.Error("tId:%s tmid:%s 没有代理", nodeId.B58String(), multicastMsgInfo.Nodes[i].MachineID)
gm.Area.SendP2pMsgProxyMachineID(multicastMsgInfo.MsgID, &nodeId, nil, gm.Area.GodID, multicastMsgInfo.Nodes[i].MachineID, &content)
continue
}
// 根据代理信息依次发送消息
for ii := range proxyInfoes {
engine.Log.Error("tId:%s tmid:%s 代理: %s", nodeId.B58String(), proxyInfoes[ii].MachineId, proxyInfoes[ii].ProxyId.B58String())
gm.Area.SendP2pMsgProxyMachineID(multicastMsgInfo.MsgID, &nodeId, proxyInfoes[ii].ProxyId, gm.Area.GodID, proxyInfoes[ii].MachineId, &content)
}
}
// 4. 回复消息
gm.Area.SendP2pReplyMsg(message, MSGID_P2P_SEND_MULTICAST_MSG_RECV, nil)
}
// 发送组播消息的返回
func (gm *MulticastMsg) sendMulticastMsg_recv(c engine.Controller, msg engine.Packet, message *mc.Message) {
// engine.Log.Info("收到P2P消息返回 from:%s", message.Head.Sender.B58String())
flood.ResponseBytes(utils.Bytes2string(message.Body.Hash), message.Body.Content)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/prestonTao/libp2parea.git
git@gitee.com:prestonTao/libp2parea.git
prestonTao
libp2parea
libp2parea
3aaa451ef873

搜索帮助