Example #1
0
        public static ReconnPacket Read(NBufferReader reader)
        {
            var ret = new ReconnPacket();

            ret.Name        = reader.ReadUTF();
            ret.Host        = reader.ReadUTF();
            ret.Port        = reader.ReadUInt32();
            ret.GameId      = reader.ReadUInt32();
            ret.KeyTime     = reader.ReadUInt32();
            ret.IsFromArena = reader.ReadBoolean();
            ret.Key         = reader.ReadBytes(reader.ReadUInt16());
            return(ret);
        }
Example #2
0
        private bool OnServerPacketReceived(ref byte id, ref byte[] content)
        {
            if (id == PacketTable.RECONNECT)
            {
                ReconnPacket reconn = ReconnPacket.Read(new NBufferReader(content));

                var endPoint = (IPEndPoint)server.Client.RemoteEndPoint;

                int reconnId = rand.Next();
                while (!reconnKeys.TryAdd(reconnId, Tuple.Create(
                                              string.IsNullOrEmpty(reconn.Host) ? endPoint.Address.ToString() : reconn.Host,
                                              reconn.Port == 0xffffffff ? endPoint.Port : (int)reconn.Port,
                                              reconn.Key)))
                {
                    reconnId = rand.Next();
                }

                if (reconn.Name.StartsWith("NexusPortal."))
                {
                    var entry = Dns.GetHostEntry(reconn.Host);
                    serverNames[entry.AddressList[0].ToString()] = reconn.Name;
                }
                AppendLog("Reconnect to '{0}'.", reconnKeys[reconnId].Item1);

                reconn.Host = local;
                reconn.Port = 2050;
                reconn.Key  = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(reconnId));

                content = NWriter.Write(writer => reconn.Write(writer));
            }
            else if (id == PacketTable.MAPINFO)
            {
                MapInfoPacket p = MapInfoPacket.Read(new NBufferReader(content));
                if (ConnectionName == null)
                {
                    ConnectionName = p.Name;
                }

                World = GameWorld.CreateWorld(p, this);
                if (MapCreated != null)
                {
                    MapCreated(this, EventArgs.Empty);
                }
            }

            bool ret = true;
            var  e   = new PacketEventArgs(id, content);

            if (ServerPacketReceived != null)
            {
                ServerPacketReceived(this, e);
                id      = e.ID;
                content = e.Content;
                ret    &= e.Send;
            }
            if (ret && World != null)
            {
                World.OnPacketReceived(this, e);
            }
            if (ret && PostServerPacketReceived != null)
            {
                PostServerPacketReceived(this, e);
                id      = e.ID;
                content = e.Content;
                ret    &= e.Send;
            }
            return(ret);
        }