Beispiel #1
0
        protected void OnHeartBeat()
        {
            if (!IsAlive)
            {
                return;
            }

            if (IsClientMode) //客户端无法访问全局缓存
            {
                lastTs = TimeUtil.GetTimeStampMS2();
            }
            else
            {
                this.RegisterGlobalManager(this);
                var actorRemoveList = new List <ulong>();
                foreach (var kv in this.actorDic)
                {
                    if (kv.Value.IsAlive)
                    {
                        this.RegisterGlobalManagerAsync(kv.Value);
                    }
                    else
                    {
                        actorRemoveList.Add(kv.Key);
                    }
                }
                foreach (var aId in actorRemoveList)
                {
                    actorDic.TryRemove(aId, out var _);
                }
            }
#if !CLIENT
            Global.IdManager.SyncWithCacheAsync();
#endif
        }
Beispiel #2
0
        protected void OnHeartBeat()
        {
            if (!IsAlive)
            {
                return;
            }

            Global.IdManager.PrintInfo();

            if (IsClientMode) //客户端无法访问全局缓存
            {
                lastTs = TimeUtil.GetTimeStampMS2();
            }
            else
            {
            }
        }
Beispiel #3
0
        protected void Heartbeat()
        {
            while (true)
            {
                try
                {
                    //Log.Info(string.Format("Heartbeat:{0}", IsAlive));
                    NetManager.Instance?.PrintPeerInfo();
                    if (!IsAlive)
                    {
                        return;
                    }

                    if (IsClientMode) //客户端无法访问全局缓存
                    {
                        lastTs = TimeUtil.GetTimeStampMS2();
                        NetManager.Instance.Ping(true);
                    }
                    else
                    {
                        NetManager.Instance.Ping(true);
                        this.RegisterGlobalManager(this);
                        foreach (var kv in this.actorDic)
                        {
                            this.RegisterGlobalManager(kv.Value);
                        }
                    }
#if !CLIENT
                    Global.IdManager.SyncWithCacheAsync();
#endif
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
                Thread.Sleep(5000);
            }
        }
Beispiel #4
0
        protected void OnReceive(NetPeer peer, IByteBuffer buffer)
        {
            if (!peer.IsActive)
            {
                return;
            }
            //Log.Debug(string.Format("RECV({0}) {1} {2} {3}", peer.netType, peer.ConnId, peer.RemoteAddress, StringUtil.ToHexString(buffer.ToArray())));
            if (buffer.ReadableBytes == 1)
            {
                byte opCode = buffer.ReadByte();
                //Log.Warn("RECV_PROTOCOL", opCode);

                if (opCode == (byte)OpCode.PING)
                {
                    Log.Info(string.Format("Ping({0}) {1} FROM {2}", peer.netType, peer.ConnId, peer.RemoteAddress));

                    peer.Pong();

                    if (peer != null && peer.RemoteAddress != null)
                    {
                        Global.IdManager.ReregisterHost(peer.ConnId, peer.RemoteAddress.ToIPv4String());
                    }

#if !CLIENT
                    //如果peer是客户端,则代表
                    var clientActorId = Global.IdManager.GetClientActorId(peer.ConnId);
                    if (clientActorId != 0 && this.actorDic.ContainsKey(clientActorId))
                    {
                        Global.IdManager.RegisterClientActor(clientActorId, GetActor(clientActorId).UniqueName, peer.ConnId, peer.RemoteAddress.ToIPv4String());
                    }
#endif

                    Global.NetManager.OnPong(peer);
                    return;
                }
                else if (opCode == (byte)OpCode.PONG)
                {
#if CLIENT
                    Log.Info("ping>>>" + (TimeUtil.GetTimeStampMS2() - lastTs).ToString());
#endif
                    peer.lastTickTime = TimeUtil.GetTimeStampMS2();
                    Global.NetManager.OnPong(peer);
                    return;
                }
                else if (opCode == (byte)OpCode.GOODBYE)
                {
                    //删除这个连接
                    Global.NetManager.Deregister(peer);
                    return;
                }
            }

            uint protoCode = buffer.ReadUnsignedIntLE();

#if !CLIENT
            if (protoCode == OpCode.REGISTER_REQ)
            {
                var hostId    = (ulong)buffer.ReadLongLE();
                var nameBytes = new byte[buffer.ReadableBytes];
                buffer.ReadBytes(nameBytes);
                var hostName = Encoding.UTF8.GetString(nameBytes);

                var context = new RpcContext(null, peer);

                this.Register(hostId, hostName, context);

                return;
            }
#endif

            if (protoCode == OpCode.PARTIAL)
            {
                var partialId    = (ulong)buffer.ReadLongLE();
                var partIndex    = buffer.ReadByte();
                var totPartCount = buffer.ReadByte();
                var payload      = new byte[buffer.ReadableBytes];
                buffer.ReadBytes(payload);

                var finalBytes = Global.NetManager.AddPartialRpc(partialId, partIndex, totPartCount, payload);
                if (finalBytes != null)
                {
                    var finalBuf   = Unpooled.WrappedBuffer(finalBytes);
                    var _protoCode = finalBuf.ReadUnsignedIntLE();
#if !CLIENT
                    if (_protoCode == OpCode.REGISTER_REQ)
                    {
                        ProcessRegisterProtocol(peer, _protoCode, finalBuf);
                    }
                    else
                    {
                        ProcessRpcProtocol(peer, _protoCode, finalBuf);
                    }
#else
                    ProcessRpcProtocol(peer, _protoCode, finalBuf);
#endif
                }
                return;
            }

            ProcessRpcProtocol(peer, protoCode, buffer);
        }
Beispiel #5
0
        protected void OnReceiveBuffer(NetPeer peer, IByteBuffer buffer)
        {
            if (!peer.IsActive)
            {
                return;
            }
            //Log.Debug(string.Format("RECV({0}) {1} {2} {3}", peer.netType, peer.ConnId, peer.RemoteAddress, StringUtil.ToHexString(buffer.ToArray())));
            if (buffer.ReadableBytes == 1)
            {
                byte protoCode = buffer.ReadByte();
                if (protoCode == (byte)OpCode.PING)
                {
                    Log.Debug(string.Format("Ping({0}) {1} FROM {2}", peer.netType, peer.ConnId, peer.RemoteAddress));

                    peer.Pong();

                    if (peer != null && peer.RemoteAddress != null)
                    {
                        Global.IdManager.ReregisterHost(peer.ConnId, peer.RemoteAddress.ToIPv4String());
                    }

#if !CLIENT
                    //如果peer是客户端,则代表
                    var clientActorId = Global.IdManager.GetClientActorId(peer.ConnId);
                    if (clientActorId != 0 && this.actorDic.ContainsKey(clientActorId))
                    {
                        Global.IdManager.RegisterClientActor(clientActorId, GetActor(clientActorId).UniqueName, peer.ConnId, peer.RemoteAddress.ToIPv4String());
                    }
#endif

                    NetManager.Instance.OnPong(peer);
                }
                else if (protoCode == (byte)OpCode.PONG)
                {
                    Log.Info("ping>>>" + (TimeUtil.GetTimeStampMS2() - lastTs).ToString());
                    peer.lastTickTime = TimeUtil.GetTimeStampMS2();
                    NetManager.Instance.OnPong(peer);
                }
                else if (protoCode == (byte)OpCode.GOODBYE)
                {
                    //删除这个连接
                    NetManager.Instance.Deregister(peer);
                }

                return;
            }
            else
            {
                uint protoCode = buffer.ReadUnsignedIntLE();
                if (protoCode == OpCode.REGISTER_REQ)
                {
                    var hostId    = buffer.ReadUnsignedIntLE();
                    var nameBytes = new byte[buffer.ReadableBytes];
                    buffer.ReadBytes(nameBytes);
                    var hostName = Encoding.UTF8.GetString(nameBytes);

                    var context = new RpcContext(null, peer);

                    this.Register(hostId, hostName, context);

                    return;
                }

                ulong msgId = (ulong)buffer.ReadLongLE();
                //uint fromHostId = buffer.ReadUnsignedIntLE();
                uint   fromActorId = buffer.ReadUnsignedIntLE();
                uint   toActorId   = buffer.ReadUnsignedIntLE();
                byte[] bytes       = new byte[buffer.ReadableBytes];
                buffer.ReadBytes(bytes);

                var packet = Packet.Create(msgId,
                                           protoCode,
                                           peer.ConnId,
                                           Global.Host.Id,
                                           fromActorId,
                                           toActorId,
                                           peer.netType,
                                           Global.TypeManager.GetMessageType(protoCode),
                                           bytes);

                Log.Debug(string.Format("RECV2({0}): {1} {2} => {3} {4} >= {5} {6} => {7}",
                                        peer.netType,
                                        protoCode,
                                        packet.FromHostId,
                                        packet.ToHostId,
                                        packet.FromActorId,
                                        packet.ToActorId,
                                        peer.RemoteAddress.ToIPv4String(),
                                        peer.LocalAddress.ToIPv4String()));

                if (protoCode >= OpCode.CALL_ACTOR_METHOD && toActorId != 0)
                {
                    this.CallActorMethod(packet);
                }
                else
                {
                    this.CallMethod(packet);
                }
            }
        }