Example #1
0
        protected override void Initialize(params object[] args)
        {
            using (Packet outPacket = new Packet(InteroperabilityOperationCode.RegistrationRequest))
            {
                outPacket.WriteString((string)args[0]);
                outPacket.WriteByte((byte)args[1]);
                outPacket.WriteBytes(ChannelServer.RemoteEndPoint.Address.GetAddressBytes());
                outPacket.WriteShort((short)ChannelServer.RemoteEndPoint.Port);

                this.Send(outPacket);
            }

            ByteBuffer buffer = new ByteBuffer();

            this.Socket.BeginReceive(buffer.Array, buffer.Position, buffer.Capacity, SocketFlags.None, new AsyncCallback(this.OnReceive), buffer);

            this.ResponseReceived.WaitOne();

            using (Packet inPacket = this.ResponsePacket)
            {
                ChannelRegistrationResponse outPacket = (ChannelRegistrationResponse)inPacket.ReadByte();

                switch (outPacket)
                {
                case ChannelRegistrationResponse.Valid:
                    ChannelServer.ExperienceRate           = inPacket.ReadInt();
                    ChannelServer.QuestExperienceRate      = inPacket.ReadInt();
                    ChannelServer.PartyQuestExperienceRate = inPacket.ReadInt();
                    ChannelServer.MesoRate  = inPacket.ReadInt();
                    ChannelServer.DropRate  = inPacket.ReadInt();
                    ChannelServer.WorldID   = inPacket.ReadByte();
                    ChannelServer.ChannelID = inPacket.ReadByte();

                    Log.Success("Registered channel as {0}-{1} at {2}.", WorldNameResolver.GetName(ChannelServer.WorldID), ChannelServer.ChannelID, ChannelServer.RemoteEndPoint);
                    Log.Inform("Rates: {0}x / {1}x / {2}x / {3}x / {4}x.",
                               ChannelServer.ExperienceRate,
                               ChannelServer.QuestExperienceRate,
                               ChannelServer.PartyQuestExperienceRate,
                               ChannelServer.MesoRate,
                               ChannelServer.DropRate);
                    break;

                default:
                    throw new NetworkException(RegistrationResponseResolver.Explain(outPacket));
                }
            }
        }
		private void OnRegistrationResponse(InPacket inPacket)
		{
			ServerRegistrationResponse response = (ServerRegistrationResponse)inPacket.ReadByte();

			switch (response)
			{
				case ServerRegistrationResponse.Valid:
					{
						GameServer.WorldID = inPacket.ReadByte();
						GameServer.ChannelID = inPacket.ReadByte();

						GameServer.ScrollingHeader = inPacket.ReadString();
						GameServer.Rates = new Rates()
						{
							Experience = inPacket.ReadInt(),
							QuestExperience = inPacket.ReadInt(),
							PartyQuestExperience = inPacket.ReadInt(),

							Meso = inPacket.ReadInt(),
							Loot = inPacket.ReadInt(),
						};

						Log.Success("Registered Game as {0}-{1} at {2}.", WorldNameResolver.GetName(GameServer.WorldID),
							GameServer.ChannelID, GameServer.RemoteEndPoint);
						Log.Inform("Rates: {0}x / {1}x / {2}x / {3}x / {4}x.",
							GameServer.Rates.Experience,
							GameServer.Rates.QuestExperience,
							GameServer.Rates.PartyQuestExperience,
							GameServer.Rates.Meso,
							GameServer.Rates.Loot);
					}
					break;

				default:
					throw new NetworkException(RegistrationResponseResolver.Explain(response));
			}
		}
        public void HandleRegistrationRequest(Packet inPacket)
        {
            string     securityCode = inPacket.ReadString();
            byte       WorldID      = inPacket.ReadByte();
            IPEndPoint endPoint     = new IPEndPoint(inPacket.ReadIPAddress(), inPacket.ReadShort());

            bool worked = false;

            using (Packet outPacket = new Packet(InteroperabilityOperationCode.RegistrationResponse))
            {
                if (securityCode != ChannelServerHandler.SecurityCode)
                {
                    outPacket.WriteByte((byte)ChannelRegistrationResponse.InvalidCode);
                    Log.Error(RegistrationResponseResolver.Explain(ChannelRegistrationResponse.InvalidCode));
                }
                else if (!WorldNameResolver.IsValid(WorldID))
                {
                    outPacket.WriteByte((byte)ChannelRegistrationResponse.InvalidWorld);
                    Log.Error(RegistrationResponseResolver.Explain(ChannelRegistrationResponse.InvalidWorld));
                }
                else if (!LoginServer.Worlds.Contains(WorldID) && LoginServer.Worlds.Count == 15)
                {
                    outPacket.WriteByte((byte)ChannelRegistrationResponse.WorldsFull);
                    Log.Error(RegistrationResponseResolver.Explain(ChannelRegistrationResponse.WorldsFull));
                }
                else
                {
                    if (!LoginServer.Worlds.Contains(WorldID))
                    {
                        LoginServer.Worlds.Add(new World(WorldID));
                    }

                    if (LoginServer.Worlds[WorldID].Count == 20)
                    {
                        outPacket.WriteByte((byte)ChannelRegistrationResponse.ChannelsFull);
                        Log.Error(RegistrationResponseResolver.Explain(ChannelRegistrationResponse.ChannelsFull));
                    }
                    else if (LoginServer.Worlds[WorldID].HostIP.ToString() != endPoint.Address.ToString())
                    {
                        outPacket.WriteByte((byte)ChannelRegistrationResponse.InvalidIP);
                        Log.Error(RegistrationResponseResolver.Explain(ChannelRegistrationResponse.InvalidIP));
                    }
                    else
                    {
                        this.RemoteEndPoint = endPoint;
                        this.WorldID        = WorldID;

                        this.World.Add(this);
                        this.ID = (byte)this.World.Count;

                        outPacket.WriteByte((byte)ChannelRegistrationResponse.Valid);
                        outPacket.WriteInt(this.World.ExperienceRate);
                        outPacket.WriteInt(this.World.QuestExperienceRate);
                        outPacket.WriteInt(this.World.PartyQuestExperienceRate);
                        outPacket.WriteInt(this.World.MesoRate);
                        outPacket.WriteInt(this.World.DropRate);
                        outPacket.WriteByte(this.WorldID);
                        outPacket.WriteByte(this.ExternalID);

                        worked = true;
                    }
                }

                this.Send(outPacket);
            }

            if (worked)
            {
                Log.Success("Registered channel {0}-{1} at {2}.", LoginServer.Worlds[this.WorldID].Name, this.ExternalID, this.RemoteEndPoint);
            }
            else
            {
                Log.Warn("Channel server registration failed.");
                this.Stop();
            }
        }