public virtual int Send(Connection connection, byte[] buffer, int start, int count, SocketFlags flags)
{
if (connection == null)
{
throw new ArgumentNullException("connection");
}
if (buffer == null)
{
throw new ArgumentNullException("buffer");
}
var totalBytesSent = 0;
var bytesRemaining = buffer.Length;
try
{
while (bytesRemaining > 0) // Ensure we send every byte.
{
// [D3Inferno]
// Use Connection wrapper to send the data so that it can be sent either over
// the normal NetworkStream (prior to Tls Authentication) or over the encrypted
// SslStream.
// Send the remaining data.
//int bytesSent = connection.Socket.Send(buffer, totalBytesSent, bytesRemaining, flags);
if (connection == null || connection.Socket == null || connection._server == null)
{
Disconnect(connection);
return(0);
}
int bytesSent = connection._Send(buffer, totalBytesSent, bytesRemaining, flags);
if (bytesSent > 0)
{
OnDataSent(new ConnectionDataEventArgs(connection, buffer.Enumerate(totalBytesSent, bytesSent))); // Raise the Data Sent event.
}
// Decrement bytes remaining and increment bytes sent.
bytesRemaining -= bytesSent;
totalBytesSent += bytesSent;
}
}
catch (SocketException)
{
RemoveConnection(connection, true); // An error occured while sending, connection has disconnected.
}
catch (Exception e)
{
RemoveConnection(connection, true); // An error occured while sending, it is possible that the connection has a problem.
// Logger.DebugException(e, "Send");
}
return(totalBytesSent);
}