diff --git a/FFXIVClassic Proxy Server/PacketProcessor.cs b/FFXIVClassic Proxy Server/PacketProcessor.cs index d50c10ed..0e385e63 100644 --- a/FFXIVClassic Proxy Server/PacketProcessor.cs +++ b/FFXIVClassic Proxy Server/PacketProcessor.cs @@ -1,4 +1,6 @@ using FFXIVClassic.Common; +using FFXIVClassic_World_Server.DataObjects; +using FFXIVClassic_World_Server.Packets.Receive; using FFXIVClassic_World_Server.Packets.Send.Login; using System; using System.Collections.Generic; @@ -42,7 +44,9 @@ namespace FFXIVClassic_World_Server { //Initial Connect Packet, Create session if (subpacket.header.type == 0x01) - { + { + + #region Hardcoded replies packet.DebugPrintPacket(); byte[] reply1Data = { 0x01, 0x00, 0x00, 0x00, 0x28, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, @@ -69,34 +73,15 @@ namespace FFXIVClassic_World_Server binReader.BaseStream.Seek(0x14, SeekOrigin.Begin); binReader.Write((UInt32)Utils.UnixTimeStampUTC()); } - } - - //Read in Actor Id that owns this connection - uint actorID = 0; - using (MemoryStream mem = new MemoryStream(packet.data)) - { - using (BinaryReader binReader = new BinaryReader(mem)) - { - try - { - byte[] readIn = new byte[12]; - binReader.BaseStream.Seek(0x14, SeekOrigin.Begin); - binReader.Read(readIn, 0, 12); - actorID = UInt32.Parse(Encoding.ASCII.GetString(readIn)); - } - catch (Exception) - { } - } } + #endregion + + HelloPacket hello = new HelloPacket(packet.data); - mServer.AddSession(actorID); - if (packet.header.connectionType == BasePacket.TYPE_ZONE) - Program.Log.Info("Got {0} connection for ActorID {1} @ {2}.", "zone", actorID, client.GetAddress()); + mServer.AddSession(client, Session.Channel.ZONE, hello.sessionId); else if (packet.header.connectionType == BasePacket.TYPE_CHAT) - Program.Log.Info("Got {0} connection for ActorID {1} @ {2}.", "chat", actorID, client.GetAddress()); - - break; + mServer.AddSession(client, Session.Channel.CHAT, hello.sessionId); } //Ping from World Server else if (subpacket.header.type == 0x07) diff --git a/FFXIVClassic Proxy Server/Packets/Receive/HelloPacket.cs b/FFXIVClassic Proxy Server/Packets/Receive/HelloPacket.cs new file mode 100644 index 00000000..6f2992e5 --- /dev/null +++ b/FFXIVClassic Proxy Server/Packets/Receive/HelloPacket.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace FFXIVClassic_World_Server.Packets.Receive +{ + class HelloPacket + { + public bool invalidPacket = false; + public uint sessionId; + + public HelloPacket(byte[] data) + { + using (MemoryStream mem = new MemoryStream(data)) + { + using (BinaryReader binReader = new BinaryReader(mem)) + { + try + { + byte[] readIn = new byte[12]; + binReader.BaseStream.Seek(0x14, SeekOrigin.Begin); + binReader.Read(readIn, 0, 12); + sessionId = UInt32.Parse(Encoding.ASCII.GetString(readIn)); + } + catch (Exception) + { + invalidPacket = true; + } + } + } + } + } +} diff --git a/FFXIVClassic Proxy Server/Packets/Send/0x7ResponsePacket.cs b/FFXIVClassic Proxy Server/Packets/Send/0x7ResponsePacket.cs index 8d5a92df..7f3b2dc7 100644 --- a/FFXIVClassic Proxy Server/Packets/Send/0x7ResponsePacket.cs +++ b/FFXIVClassic Proxy Server/Packets/Send/0x7ResponsePacket.cs @@ -6,9 +6,12 @@ namespace FFXIVClassic_World_Server.Packets.Send.Login { class Login0x7ResponsePacket { + public const ushort OPCODE = 0x0008; + public const uint PACKET_SIZE = 0x18; + public static SubPacket BuildPacket(uint actorID) { - byte[] data = new byte[0x18]; + byte[] data = new byte[PACKET_SIZE]; using (MemoryStream mem = new MemoryStream(data)) { @@ -17,15 +20,14 @@ namespace FFXIVClassic_World_Server.Packets.Send.Login try { binWriter.Write((UInt32)actorID); - binWriter.Write((UInt32)type); + binWriter.Write((UInt32)Utils.UnixTimeStampUTC()); } catch (Exception) - { - } + {} } } - return BasePacket.CreatePacket(data, false, false); + return new SubPacket(false, OPCODE, 0, 0, data); } } } diff --git a/FFXIVClassic Proxy Server/Server.cs b/FFXIVClassic Proxy Server/Server.cs index a24606f0..b82ee279 100644 --- a/FFXIVClassic Proxy Server/Server.cs +++ b/FFXIVClassic Proxy Server/Server.cs @@ -18,7 +18,8 @@ namespace FFXIVClassic_World_Server private Socket mServerSocket; - WorldManager worldManager; + WorldManager mWorldManager; + PacketProcessor mPacketProcessor; private List mConnectionList = new List(); private Dictionary mZoneSessionList = new Dictionary(); @@ -36,9 +37,10 @@ namespace FFXIVClassic_World_Server public bool StartServer() { - worldManager = new WorldManager(this); - worldManager.LoadZoneServerList(); - worldManager.ConnectToZoneServers(); + mPacketProcessor = new PacketProcessor(this); + mWorldManager = new WorldManager(this); + mWorldManager.LoadZoneServerList(); + mWorldManager.ConnectToZoneServers(); IPEndPoint serverEndPoint = new System.Net.IPEndPoint(IPAddress.Parse(ConfigConstants.OPTIONS_BINDIP), int.Parse(ConfigConstants.OPTIONS_PORT)); @@ -123,33 +125,61 @@ namespace FFXIVClassic_World_Server } } - public void AddSession(uint id) + public void AddSession(ClientConnection connection, Session.Channel type, uint id) { - throw new NotImplementedException(); - } + Session session = new Session(id, connection, type); - public void RemoveSession(uint id) - { - if (mChatSessionList.ContainsKey(id)) { - mChatSessionList[id].clientSocket.Disconnect(); - mConnectionList.Remove(mChatSessionList[id].clientSocket); - mChatSessionList.Remove(id); - } - - if (mZoneSessionList.ContainsKey(id)) + switch (type) { - mZoneSessionList[id].clientSocket.Disconnect(); - mConnectionList.Remove(mZoneSessionList[id].clientSocket); - mZoneSessionList.Remove(id); + case Session.Channel.ZONE: + if (!mZoneSessionList.ContainsKey(id)) + mZoneSessionList.Add(id, session); + break; + case Session.Channel.CHAT: + if (!mChatSessionList.ContainsKey(id)) + mChatSessionList.Add(id, session); + break; } } - public Session GetSession(uint targetSession) + public void RemoveSession(Session.Channel type, uint id) { - if (mZoneSessionList.ContainsKey(targetSession)) - return mZoneSessionList[targetSession]; - else - return null; + switch (type) + { + case Session.Channel.ZONE: + if (mZoneSessionList.ContainsKey(id)) + { + mZoneSessionList[id].clientSocket.Disconnect(); + mConnectionList.Remove(mZoneSessionList[id].clientSocket); + mZoneSessionList.Remove(id); + } + break; + case Session.Channel.CHAT: + if (mChatSessionList.ContainsKey(id)) + { + mChatSessionList[id].clientSocket.Disconnect(); + mConnectionList.Remove(mChatSessionList[id].clientSocket); + mChatSessionList.Remove(id); + } + break; + } + } + + public Session GetSession(uint targetSession, Session.Channel type = Session.Channel.ZONE) + { + switch (type) + { + case Session.Channel.ZONE: + if (mZoneSessionList.ContainsKey(targetSession)) + return mZoneSessionList[targetSession]; + break; + case Session.Channel.CHAT: + if (mChatSessionList.ContainsKey(targetSession)) + return mChatSessionList[targetSession]; + break; + } + + return null; } /// @@ -191,7 +221,7 @@ namespace FFXIVClassic_World_Server break; else { - //mProcessor.ProcessPacket(conn, basePacket); + mPacketProcessor.ProcessPacket(conn, basePacket); } } @@ -274,7 +304,7 @@ namespace FFXIVClassic_World_Server public WorldManager GetWorldManager() { - return worldManager; + return mWorldManager; } }