private void ReceiveNextInstruction() { Buffer = new byte[BufferSize]; int received = ClientSocket.Receive(Buffer, SocketFlags.None); if (received == 0) { return; } byte[] data = new byte[received]; Array.Copy(Buffer, data, received); string text = Encoding.UTF8.GetString(data); Console.ForegroundColor = ConsoleColor.Green; Debug("Received Message: " + text, DebugParams); Console.ForegroundColor = ConsoleColor.White; NetComInstruction[] instructionList = NetComInstruction.Parse(this, text).ToArray(); foreach (NetComInstruction instr in instructionList) { IncommingInstructions.Add(instr, null); } }
/// <summary> /// Gets called when a message is received /// </summary> /// <param name="AR">IAsyncResult</param> private void ReceiveCallback(IAsyncResult AR) { Socket current = (Socket)AR.AsyncState; int received; try { received = current.EndReceive(AR); } catch (SocketException) { Debug("Client forcefully disconnected.", DebugParams); // Don't shutdown because the socket may be disposed and its disconnected anyway. current.Close(); LClientList.RemoveAt(current); return; } byte[] recBuf = new byte[received]; Array.Copy(Buffer, recBuf, received); string text = Encoding.UTF8.GetString(recBuf); Console.ForegroundColor = ConsoleColor.Cyan; Debug("Received message: " + text, DebugParams); Console.ForegroundColor = ConsoleColor.White; NetComInstruction[] instructionList = NetComInstruction.Parse(this, text).ToArray(); foreach (NetComInstruction instr in instructionList) { IncommingInstructions.Add(instr, LClientList[current]); } current.BeginReceive(Buffer, 0, Buffer.Length, SocketFlags.None, ReceiveCallback, current); }