protected void Ping(NetPeer clientPeer) { clientPeer?.Send(new byte[] { (byte)OpCode.PING }); }
protected void OnReceiveBuffer(NetPeer peer, IByteBuffer buffer) { if (!peer.IsActive) { return; } Log.Info(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.Info(string.Format("Ping({0}) {1} FROM {2}", peer.netType, peer.ConnId, peer.RemoteAddress)); if (peer != null && peer.RemoteAddress != null) { Global.IdManager.ReregisterHost(peer.ConnId, peer.RemoteAddress.ToIPv4String()); } peer.Send(new byte[] { (byte)OpCode.PONG }); #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) { 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.Info(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); } } }