private void ProcessGenericPackets(List <SubPacket> subPackets) { foreach (SubPacket subPacket in subPackets) { subPacket.debugPrintSubPacket(); Console.WriteLine(subPacket.gameMessage.opcode); switch (subPacket.gameMessage.opcode) { //contact login server and ensure that ipaddress of player is currently connected to case ((ushort)GamePacketOpCode.Handshake): Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); try { IPAddress[] ip = Dns.GetHostAddresses(LOGIN_SERVER_IP); int characterId = BitConverter.ToInt32(subPacket.data, 0); client.CharacterId = characterId; client.WorldServerToClient = true; IPEndPoint remoteEP = new IPEndPoint(ip[0], LOGIN_SERVER_PORT); socket.Connect(remoteEP); HandshakePacket packet = new HandshakePacket(client.GetIp(), client.GetPort(), characterId); //Console.WriteLine("PORT FROM CLIENT:" + client.GetPort()); //Console.WriteLine("IP FROM CLIENT:" + client.GetIp()); //Console.WriteLine("CHARACTER ID FROM CLIENT: " + client.CharacterId); SubPacket sp = new SubPacket(GamePacketOpCode.Handshake, 0, 0, packet.GetBytes(), SubPacketTypes.GamePacket); BasePacket packetToSend = BasePacket.CreatePacket(sp, true, false); WorldClientConnection loginServer = new WorldClientConnection(); loginServer.socket = socket; loginServer.QueuePacket(packetToSend); loginServer.FlushQueuedSendPackets(); } catch (Exception e) { Console.WriteLine(e); } break; case (((ushort)GamePacketOpCode.Acknowledgement)): AcknowledgePacket ack = new AcknowledgePacket(subPacket.data); if (ack.AckSuccessful) { foreach (var mClient in WorldServer.GetClientConnections()) //check this if any performance issues { Console.WriteLine(ack.CharacterId); if (mClient.CharacterId == ack.CharacterId && mClient.WorldServerToClient) //this is getting the wrong client { //maybe set a boolean in clientconnection that tells whether or not client is created from a server to server communication ConnectedPlayer connectedPlayer = new ConnectedPlayer(ack.CharacterId); connectedPlayer.ClientAddress = ack.ClientAddress; WorldServer.mConnectedPlayerList.Add(connectedPlayer.actorId, connectedPlayer); client = mClient; SubPacket sp = new SubPacket(GamePacketOpCode.Acknowledgement, 0, 0, subPacket.data, SubPacketTypes.GamePacket); client.QueuePacket(BasePacket.CreatePacket(sp, true, false)); client.FlushQueuedSendPackets(); Console.WriteLine("Sending ack back to: " + client.GetFullAddress()); break; } } } break; //if everything okay default: break; } } }
/// <summary> /// contact login server and ensure that the ipaddress of player is currently connected to it /// </summary> /// <param name="socket"></param> /// <param name="subPacket"></param> private void ConfirmClientConnectionWithLoginServer(Socket socket, SubPacket subPacket) { Console.WriteLine("Confirming client connection with login server"); IPAddress[] ip = Dns.GetHostAddresses(LOGIN_SERVER_IP); Character character = new Character(BitConverter.ToUInt32(subPacket.data, 0)); client.HasHandshakedWorldServerToClient = true; character.WorldClientConnection = client; if (!WorldServer.mConnectedPlayerList.ContainsKey(character.CharacterId)) { Console.WriteLine("Inserting character into dictionary: " + character.CharacterId); WorldServer.mConnectedPlayerList.TryAdd(character.CharacterId, character); } else { Console.WriteLine("WARNING! : Connected player already exists and trying to add them into list"); } IPEndPoint remoteEP = new IPEndPoint(ip[0], LOGIN_SERVER_PORT); socket.Connect(remoteEP); HandshakePacket packet = new HandshakePacket(client.GetIp(), client.GetPort(), character.CharacterId); Console.WriteLine("PORT FROM CLIENT:" + client.GetPort()); Console.WriteLine("IP FROM CLIENT:" + client.GetIp()); Console.WriteLine("CHARACTER ID FROM CLIENT: " + character.CharacterId); SubPacket sp = new SubPacket(GamePacketOpCode.Handshake, 0, 0, packet.GetBytes(), SubPacketTypes.GamePacket); BasePacket packetToSend = BasePacket.CreatePacket(sp, true, false); //send packet to login server for confirmation WorldClientConnection connectionToLoginServer = new WorldClientConnection(); connectionToLoginServer.socket = socket; connectionToLoginServer.QueuePacket(packetToSend); connectionToLoginServer.FlushQueuedSendPackets(); connectionToLoginServer.Disconnect(); }