private void ReceiveHeader() { lock (Client) { try { if (ConnectionContext == ConnectionContexts.Connectionless) { return; } if (Client.IsDisconnecting()) { return; } if (Client.IsDisconnected()) { return; } //Debug.WriteLine("Test Async"); // Create the state object. SocketReadObject state = new SocketReadObject(); // Begin receiving the data from the remote device. //Debug.WriteLine("Start Receiving"); Socket.BeginReceive(state.sizebuffer, 0, 4, 0, new AsyncCallback(ReceiveHeaderCallback), state); } catch (SocketException e) { Client.Disconnect("Remote client forcibly closed the connection."); return; } catch (Exception e) { Debug.WriteLine(e.ToString()); Client.Disconnect("Failed to receive packet header."); return; } } }
private void ReceiveData(SocketReadObject state) { lock (Client) { try { if (ConnectionContext == ConnectionContexts.Connectionless) { return; } if (Client.IsDisconnecting()) { return; } if (Client.IsDisconnected()) { return; } // Get Size... state.size = (int)(BitConverter.ToUInt32(state.sizebuffer, 0)); state.bytesreceivedsofar = 0; state.databuffer = new byte[state.size]; // Begin receiving the data from the remote device. Socket.BeginReceive(state.databuffer, state.bytesreceivedsofar, state.size - state.bytesreceivedsofar, SocketFlags.None, new AsyncCallback(ReceiveDataCallback), state); } catch (SocketException e) { Client.Disconnect("Remote client forcibly closed the connection."); return; } catch (Exception e) { Debug.WriteLine(e.ToString()); Client.Disconnect("Failed to receive packet body."); return; } } }
private void ReceiveDataCallback(IAsyncResult ar) { Packets.GenericPacket NewPacket = Packets.NoPacket; lock (Client) { try { if (ConnectionContext == ConnectionContexts.Connectionless) { return; } if (Client.IsDisconnecting()) { return; } if (Client.IsDisconnected()) { return; } // Retrieve the state object and the client socket // from the asynchronous state object. SocketReadObject state = (SocketReadObject)ar.AsyncState; // Read data from the remote device. int bytesRead = Socket.EndReceive(ar); if (bytesRead == 0) { //End Of Stream //Debug.WriteLine("End of DataStream"); Client.Disconnect("Recv'd 0 data when trying to receive packet body."); } state.bytesreceivedsofar += bytesRead; if (state.bytesreceivedsofar < state.size) { Socket.BeginReceive(state.databuffer, state.bytesreceivedsofar, state.size - state.bytesreceivedsofar, SocketFlags.None, new AsyncCallback(ReceiveDataCallback), state); return; } else { //Debug.WriteLine(state.sizebuffer.ToDebugHexString() + state.databuffer.ToDebugHexString()); //Debug.TestPoint(); // All bytes have been received. NewPacket = new Packets.GenericPacket(state.sizebuffer.Concat(state.databuffer).ToArray()); //Debug.WriteLine("End Receiving"); } } catch (SocketException e) { Client.Disconnect("Remote client forcibly closed the connection."); return; } catch (Exception e) { Console.WriteLine(e.ToString()); Client.Disconnect("Generic Error when trying to receive packet body."); return; } } //Outside of lock now, let another receive process begin! foreach (WaitForPacketObject ThisWaitForPacketObject in WaitForPacketList.ToArray()) { if (NewPacket == null) { break; } if (NewPacket == Packets.NoPacket) { break; } if (NewPacket.GetRawString() == ThisWaitForPacketObject.Packet.GetRawString()) //Must match based on strings, not bytes... { //Packets match. ThisWaitForPacketObject.Trigger.Set(); //Set the trigger... WaitForPacketList.Remove(ThisWaitForPacketObject); //Remove self. //ThisWaitForPacketObject.Trigger.Dispose(); //Remove the trigger once set. //Debug.WriteLine(Client.Username + " End Wait"); } } ProcessPacket(NewPacket); //Thread.Sleep(500); }
private void ReceiveHeaderCallback(IAsyncResult ar) { lock (Client) { try { if (ConnectionContext == ConnectionContexts.Connectionless) { return; } if (Client.IsDisconnecting()) { return; } if (Client.IsDisconnected()) { return; } // Retrieve the state object and the client socket // from the asynchronous state object. SocketReadObject state = (SocketReadObject)ar.AsyncState; // Read data from the remote device. int bytesRead = Socket.EndReceive(ar); if (bytesRead == 0) { //End Of Stream //Debug.WriteLine("End of DataStrem"); Client.Disconnect("Recv'd 0 data when trying to receive packet header."); } state.bytesreceivedsofar += bytesRead; if (state.bytesreceivedsofar < 4) { // need more data, so store the data received so far. //state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead)); // Get the rest of the data. //client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, //new AsyncCallback(ReceiveHeaderCallback), state); Socket.BeginReceive(state.sizebuffer, state.bytesreceivedsofar, 4 - state.bytesreceivedsofar, SocketFlags.None, new AsyncCallback(ReceiveHeaderCallback), state); return; } else { // All bytes have been received. ReceiveData(state); } } catch (SocketException e) { Client.Disconnect("Remote client forcibly closed the connection."); return; } catch (Exception e) { Debug.WriteLine(e.ToString()); Client.Disconnect("Generic Error when trying to receive packet header."); return; } } }