private void HandleClient(object cObj) { TcpClient Client = (TcpClient)cObj; Tmp.Entry Entry = new Tmp.Entry(); GlobalVar.nStream = Client.GetStream(); byte[] Header = new byte[8]; Entry.IP = Client.Client.RemoteEndPoint.ToString().Split(':')[0]; GlobalFunc.Write("Client [{0}] - Connected", Entry.IP); using (PoorManStream pmStream = new PoorManStream(GlobalVar.nStream)) { using (EndianIO MainIO = new EndianIO(Header, EndianStyle.BigEndian)) { if (GlobalVar.nStream.Read(Header, 0, 8) != 8) { GlobalFunc.WriteError(ConsoleColor.Red, "[SERVER]", "Header recieved unexpected size!"); Client.Close(); } uint Command = MainIO.Reader.ReadUInt32(); int PacketSize = MainIO.Reader.ReadInt32(); byte[] Buffer = new byte[PacketSize]; using (EndianIO WriterIO = new EndianIO(pmStream, EndianStyle.BigEndian)) { using (EndianIO ReaderIO = new EndianIO(Buffer, EndianStyle.BigEndian) { Writer = new EndianWriter(pmStream, EndianStyle.BigEndian) }) { if (pmStream.Read(Buffer, 0, PacketSize) != PacketSize) { GlobalFunc.WriteError(ConsoleColor.Red, "[SERVER]", "Packet recieved unexpected size!"); Client.Close(); } switch (Command) { case (uint)cmdCode.GET_SESSION: SESSION.Get(ref Entry, WriterIO, ReaderIO); break; case (uint)cmdCode.GET_STATUS: STATUS.Get(ref Entry, WriterIO, ReaderIO); break; case (uint)cmdCode.GET_CHAL_RESPONSE: break; case (uint)cmdCode.UPDATE_PRESENCE: PRESENCE.Update(ref Entry, WriterIO, ReaderIO); break; case (uint)cmdCode.GET_XOSC: break; case (uint)cmdCode.GET_INFO: break; case (uint)cmdCode.SND_SPOOFY: break; case (uint)cmdCode.GET_MESSAGE: break; case (uint)cmdCode.GET_PATCHES: break; case (uint)cmdCode.GET_GUIDE_INFO: break; default: break; } } } } } }
private void clientHandle(object clientObj) { TcpClient Client = (TcpClient)clientObj; TmpEntry entry = new TmpEntry(); NetworkStream nStream = Client.GetStream(); PoorManStream pmStream = new PoorManStream(nStream); IPAddr = Client.Client.RemoteEndPoint.ToString(); //try { GlobalFunc.Write("{0} [{1}] PING", "Client", IPAddr); entry.client_ip = IPAddr.Split(':')[0]; mainBuff = new byte[8]; structBuff = new byte[0x10]; if (nStream.Read(mainBuff, 0, 8) != 8) { Client.Close(); GlobalFunc.Write("Client [{0}] Main Buffer Overflow", IPAddr); return; } EndianIO mainIO = new EndianIO(mainBuff, EndianStyle.BigEndian); uint CommandID = mainIO.Reader.ReadUInt32(); buffCount = mainIO.Reader.ReadInt32(); if (buffCount >= 0x8000) { Client.Close(); GlobalFunc.Write("Client [{0}] Main Buffer Overflow 0x8000 - Ret 0x{1}", IPAddr, buffCount.ToString("X")); return; } mainBuff = new byte[buffCount]; EndianIO writerIO = new EndianIO(pmStream, EndianStyle.BigEndian); EndianIO readerIO = new EndianIO(mainBuff, EndianStyle.BigEndian) { Writer = new EndianWriter(pmStream, EndianStyle.BigEndian) }; EndianWriter structIO = new EndianIO(structBuff, EndianStyle.BigEndian).Writer; if (pmStream.Read(mainBuff, 0, buffCount) != buffCount) { Client.Close(); GlobalFunc.Write("Client [{0}] Unexpected Buffer Size", IPAddr); return; } GlobalFunc.Write("COMMAND ID: {0}", Enum.GetName(typeof(cmdCode), Convert.ToUInt32(CommandID))); switch (Convert.ToUInt32(CommandID)) { case (uint)cmdCode.GET_SESSION: GET_SESSION(ref entry, readerIO, writerIO); break; case (uint)cmdCode.GET_STATUS: GET_STATUS(ref entry, readerIO, writerIO); break; case (uint)cmdCode.GET_CHAL_RESPONSE: GET_CHAL_RESPONSE(ref entry, readerIO, writerIO); break; case (uint)cmdCode.UPDATE_PRESENCE: UPDATE_PRESENCE(ref entry, readerIO, writerIO); break; case (uint)cmdCode.GET_XOSC: GET_XOSC(ref entry, readerIO, writerIO); break; case (uint)cmdCode.GET_INFO: break; case (uint)cmdCode.SND_SPOOFY: // No need right now break; case (uint)cmdCode.GET_MESSAGE: GET_MESSAGE(ref entry, readerIO, writerIO, structIO); break; case (uint)cmdCode.GET_PATCHES: GET_PATCH_DATA(ref entry, readerIO, writerIO); break; case (uint)cmdCode.GET_GUIDE_INFO: GET_GUIDE_INFO(ref entry, readerIO, writerIO, structIO); break; default: GlobalFunc.Write("Client [{0}] INVALID COMMAND CODE! Client Closed.", IPAddr); Client.Close(); break; } writerIO.Close(); readerIO.Close(); structIO.Close(); //} catch (Exception ex) { // if (Globals.b_printCrash) // GlobalFunc.Write("Client [{0}] Client Crash! Closed Connection. Detailed: {1} || {2}", IPAddr, ex.Source, ex.Message); // Client.Close(); //} }