Thursday 7 March 2013

Posted by Prasad KM | 04:13 Categories:

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)
    {
        //----- Need header!
        int headerSize = connection.Header.Length + 2;
        byte[] result = new byte[workBuffer.Length + headerSize];

        int messageLength = result.Length;

        //----- Header!
        for (int i = 0; i < connection.Header.Length; i++)
        {
            result[i] = connection.Header[i];
        }

        //----- Length!
        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
    {
        //----- No header!
        return new MessageBuffer(ref buffer, ref workBuffer);
    }
}

0 comments:

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube