Beispiel #1
0
        public virtual void Pack()
        {
            Dictionary <string, byte[]> packData = new Dictionary <string, byte[]>();

            packData["basic"] = RpcUtil.Serialize(this);
            foreach (var kv in this.mPersistentDic)
            {
                packData[kv.Key.Name] = RpcUtil.Serialize(kv.Value);
            }
        }
Beispiel #2
0
        public void RpcCallback(ulong protoId, int protoCode, ulong fromHostId, ulong toHostId, ulong fromActorId, ulong toActorId, NetworkType netType, IMessage cbMsg)
        {
            try
            {
                var packet = Packet.Create(protoId, -Math.Abs(protoCode), fromHostId, toHostId, fromActorId, toActorId, netType, cbMsg.GetType(), RpcUtil.Serialize(cbMsg));

                //如果是同进程,则本地调用
                if (fromHostId == toHostId)
                {
                    if (Math.Abs(protoCode) < OpCode.CALL_ACTOR_METHOD)
                    {
                        Global.Host.CallMethod(packet);
                        return;
                    }
                    else
                    {
                        var toActor = Global.Host.GetActor(toActorId);
                        toActor.CallMethod(packet);
                        return;
                    }
                }

                //如果是客户端,则直接用remote netpeer返回包
                //如果是服务端,则用local netpeer返回包
                bool isClient = Global.IdManager.IsClientHost(toHostId);

                //否则通过网络调用
                var peer = isClient ? Global.NetManager.GetRemotePeerById(toHostId, netType) : Global.NetManager.GetLocalPeerById(toHostId, netType);

                if (peer == null)
                {
                    Log.Warn(string.Format("RpcCallback:cannot_find_peer_and_create {0} => {1} ({2}", fromHostId, toHostId, netType));
                    peer = Global.NetManager.CreatePeer(toHostId, null, netType);
                }

                if (peer == null || !peer.IsActive)
                {
                    Log.Error(string.Format("RpcCallback:peer disconnected {0}", toHostId));
                    //这里可以尝试把global以及redis状态清空
                    if (peer == null)
                    {
                        Global.NetManager.RemovePeerId(toHostId);
                    }
                    else
                    {
                        Global.NetManager.Deregister(peer);
                    }
                    return;
                }

                //Log.Info(string.Format("{0} {1} {2} {3} {4}", Global.Host.Id, toHostId,
                //    Global.TypeManager.GetActorType(fromActorId).Name, Global.TypeManager.GetActorType(toActorId).Name,
                //    peer == null ? "NULL" : ""));

                Global.NetManager.Send(peer, packet);
            }
            catch (Exception ex)
            {
                Log.Error(ex);
            }
        }
Beispiel #3
0
        public void RpcCallback(ulong protoId, uint protoCode, ulong fromHostId, ulong toHostId, ulong fromActorId, ulong toActorId, NetworkType netType, IMessage cbMsg)
        {
            var packet = Packet.Create(protoId, protoCode, fromHostId, toHostId, fromActorId, toActorId, netType, cbMsg.GetType(), RpcUtil.Serialize(cbMsg));

            //如果是同进程,则本地调用
            if (fromHostId == toHostId)
            {
                var toActor = Global.Host.GetActor(toActorId);
                toActor.CallMethod(packet);
                return;
            }

            //否则通过网络调用
            var peer = Global.NetManager.GetPeerById(toHostId, netType);

            if (peer == null)
            {
                Log.Warn(string.Format("RpcCallback:cannot_find_peer_and_create {0} => {1} ({2}", fromHostId, toHostId, netType));
                peer = Global.NetManager.CreatePeer(toHostId, null, netType);
            }

            if (peer == null || !peer.IsActive)
            {
                Log.Error(string.Format("RpcCallback:peer disconnected {0}", toHostId));
                //这里可以尝试把global以及redis状态清空
                if (peer == null)
                {
                    Global.NetManager.RemovePeerId(toHostId);
                }
                else
                {
                    Global.NetManager.Deregister(peer);
                }
                return;
            }

            //Log.Info(string.Format("{0} {1} {2} {3} {4}", Global.Host.Id, toHostId,
            //    Global.TypeManager.GetActorType(fromActorId).Name, Global.TypeManager.GetActorType(toActorId).Name,
            //    peer == null ? "NULL" : ""));

            Global.NetManager.Send(peer, packet);
        }
Beispiel #4
0
        public void Rpc(uint protoCode, ulong fromHostId, ulong fromActorId, ulong toHostId, ulong toActorId,
                        IPEndPoint toPeerAddr, NetworkType netType, IMessage msg, Action <byte[]> cb)
        {
            var packet = Packet.Create(Basic.GenID64(), protoCode, fromHostId, toHostId, fromActorId, toActorId, netType, msg.GetType(), RpcUtil.Serialize(msg));

            /*创建一个等待回调的rpc_command*/
            var cmd = RpcCommand.Create(
                packet,
                (data) => { RemoveRpc(packet.Id); cb?.Invoke(data); },
                this);

            //如果是同进程,则本地调用
            if (fromHostId == toHostId)
            {
                var toActor = Global.Host.GetActor(toActorId);

                if (msg.HasCallback())
                {
                    AddCallbackRpc(cmd);
                }
                toActor.CallMethod(packet);
                return;
            }

            //否则通过网络调用
            var peer = Global.NetManager.GetPeerById(toHostId, netType);

            //Log.Info(string.Format("{0} {1} {2} {3} {4}", fromHostId, toHostId, fromActorId, toActorId, peer==null?"NULL":""));
            if (peer == null)
            {
                Log.Warn(string.Format("Rpc:cannot_find_peer_and_create {0} => {1} ({2})", fromHostId, toHostId, netType));
                peer = Global.NetManager.CreatePeer(toHostId, toPeerAddr, netType);
            }

            if (peer == null || !peer.IsActive)
            {
                Log.Error(string.Format("Rpc:peer disconnected {0}", toHostId), toPeerAddr, netType);
                //这里可以尝试把global以及redis状态清空
                if (peer == null)
                {
                    Global.NetManager.RemovePeerId(toHostId);
                }
                else
                {
                    Global.NetManager.Deregister(peer);
                }
                cb?.Invoke(null);
                return;
            }

            if (msg.HasCallback())
            {
                AddCallbackRpc(cmd);
            }

            Global.NetManager.Send(peer, packet);
        }