public void Send(Packets.GenericPacket Output) { try { if (ConnectionContext == ConnectionContexts.Connectionless) { return; } if (Client.IsDisconnecting()) { return; } if (Client.IsDisconnected()) { return; } // Begin sending the data to the remote device. SocketSendObject SendOp = new SocketSendObject(); SendOp.outpacket = Output; Socket.BeginSend(Output.GetRawBytes(), 0, (int)Output.Size + 4, SocketFlags.None, new AsyncCallback(SendCallback), SendOp); } catch (SocketException e) { Client.Disconnect("Remote client forcibly closed the connection."); return; } catch (Exception e) { Debug.WriteLine(e.ToString()); } }
private void SendCallback(IAsyncResult ar) { try { if (ConnectionContext == ConnectionContexts.Connectionless) { return; } if (Client.IsDisconnecting()) { return; } if (Client.IsDisconnected()) { return; } // Retrieve the socket from the state object. SocketSendObject SendOp = (SocketSendObject)ar.AsyncState; // Complete sending the data to the remote device. int bytesSent = Socket.EndSend(ar); //Debug.WriteLine("Sent " + bytesSent.ToString() + " bytes."); if (bytesSent < SendOp.outpacket.Size + 4) { Debug.WriteLine("Didn't send all packet data???"); } // All bytes have been sent. } catch (SocketException e) { Client.Disconnect("Remote client forcibly closed the connection."); return; } catch (Exception e) { Debug.WriteLine(e.ToString()); } }