Asynchronous socket Data Send in C#
As said before, every time you send messages, the host checks if the
data must be encrypted and/or compressed, and, if you choose to use some
header, the raw buffer is controlled by the
MessageBuffer
class. This class is created using the
GetPacketMessage
static
method:
public static MessageBuffer GetPacketMessage(
BaseSocketConnection connection, ref byte[] buffer)
{
byte[] workBuffer = null;
workBuffer = CryptUtils.EncryptData(connection, buffer);
if (connection.Header != null && connection.Header.Length >= 0)
{
int headerSize = connection.Header.Length + 2;
byte[] result = new byte[workBuffer.Length + headerSize];
int messageLength = result.Length;
for (int i = 0; i < connection.Header.Length; i++)
{
result[i] = connection.Header[i];
}
result[connection.Header.Length] =
Convert.ToByte((messageLength & 0xFF00) >> 8);
result[connection.Header.Length + 1] =
Convert.ToByte(messageLength & 0xFF);
Array.Copy(workBuffer, 0, result,
headerSize, workBuffer.Length);
return new MessageBuffer(ref buffer, ref result);
}
else
{
return new MessageBuffer(ref buffer, ref workBuffer);
}
}
0 comments:
Post a Comment