- vssprotocol01.zip
- VSSProtocol
- _ReSharper.VSSProtocol
- a97cfa68-c7c0-4928-ab36-0048f6ff04f0.Metadata
- CachesImage.bin
- e4b6b052-bb1b-4ae6-a8ae-f5809445cc0d.Metadata
- ReflectionCache.xml
- TodoCache
- WordIndex.New
- .format
- 0
- 27996041.dat
- 2
- 2140e1bb.dat
- 5
- 670e1070.dat
- VsspChat
- VSSProtocol.resharper
- VSSProtocol.resharper.user
- VSSProtocol.sln
- VSSProtocol
- bin
- Debug
- DataLayer
- Exceptions
- Messages
- obj
- Debug
- Refactor
- VSSProtocol.dll
- TempPE
- Properties
- Session
- States
- Users
- Utils
- VSSProtocol.csproj
- VSSProtocol.csproj.user
- vsspchat.zip
|
using System;
using System.IO;
using VSSProtocol.DataLayer;
namespace VSSProtocol.Messages
{
/// <summary>
/// An helper class used for easy generatinon of hello client messages.
/// The message includes the suit and a nonce number.
/// The first byte is the suit and the rest is the nonce.
/// Pay attention that
/// </summary>
internal class HelloClientMessage : DataPacket
{
/// <summary>
/// What is the size of nonce that is assumed.
/// </summary>
public static readonly int NONCE_SIZE_BYTES = 8;
/// <summary>
/// Chosen suit.
/// </summary>
private readonly Suits.Suit chosenSuit;
/// <summary>
/// Nonce.
/// </summary>
private readonly byte[] nonce;
#region Factory methods.
/// <summary>
/// Create new ClientHello message.
/// </summary>
/// <param name="chosenSuit">Chosen suit or suits.</param>
/// <param name="nonce">Random value.</param>
/// <returns>Message</returns>
public static HelloClientMessage CreateMessage(Suits.Suit chosenSuit, byte[] nonce)
{
return new HelloClientMessage(chosenSuit, nonce);
}
/// <summary>
/// Create new message from a packet.
/// </summary>
/// <param name="p">Packet to create from.</param>
/// <returns>ClientHello message.</returns>
/// <exception cref="InvalidDataException">Message is not of type ClientHello.</exception>
public static HelloClientMessage CreateMessage(DataPacket p)
{
// Check that data packet is actually of type ClientHello.
if (p.PacketType == PacketTypeEnum.ClientHello)
{
return new HelloClientMessage(p.Data);
}
else
{
throw new InvalidDataException("Message is not of type ClientHello.");
}
}
#endregion
#region Private constructors
private HelloClientMessage(Suits.Suit chosenSuit, byte[] nonce)
: base(PacketTypeEnum.ClientHello)
{
if (nonce == null)
{
throw new ArgumentNullException();
}
this.nonce = nonce;
this.chosenSuit = chosenSuit;
// Get encoded message.
byte[] message = getMessage(nonce, chosenSuit);
setData(message);
}
/// <summary>
/// Gets bytes representation of nonce and suit.
/// First byte is the suite and the rest is the nonce. Nonce size allowed is NONCE_SIZE_BYTES
/// </summary>
/// <param name="nonce">Random value.</param>
/// <param name="chosenSuit">Suit</param>
/// <returns>Data encoded as byte array.</returns>
/// <exception cref="ArgumentOutOfRangeException">Nonce size allowed is NONCE_SIZE_BYTES</exception>
internal static byte[] getMessage(byte[] nonce, Suits.Suit chosenSuit)
{
byte[] nonceByte = nonce;
// Check that its NONCE_SIZE_BYTES bytes long.
if (nonceByte.Length != NONCE_SIZE_BYTES)
{
throw new ArgumentOutOfRangeException("Nonce should be " + NONCE_SIZE_BYTES + " bytes long");
}
byte[] message = new byte[nonceByte.Length + 1]; // One for suit.
// Prepare the byte array that I wish to send.
message[0] = (byte)chosenSuit;
Array.Copy(nonceByte, 0, message, 1, nonceByte.Length);
return message;
}
/// <summary>
/// Convert a raw message into something readable.
/// </summary>
/// <param name="message">Message to convert.</param>
private HelloClientMessage(byte[] message)
: base(PacketTypeEnum.ClientHello)
{
setData(message);
// Get chosen suit.
chosenSuit = (Suits.Suit)message[0];
// Get nonce value.
nonce = new byte[NONCE_SIZE_BYTES];
Array.Copy(message, 1, nonce, 0, message.Length - 1);
}
#endregion
/// <summary>
/// Chosen suit.
/// </summary>
public Suits.Suit ChosenSuit
{
get { return chosenSuit; }
}
/// <summary>
/// Nonce.
/// </summary>
public byte[] Nonce
{
get { return nonce; }
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
A computer science master student at Bar Ilan University under the supervision of Dr. Gal Kaminka.
Dealing mainly with trajectory mining.