using System;
using System.Text;
using System.Collections.Generic;
using Ion.Specialized.Encoding;
namespace Ion.Net.Messages
{
public class ServerMessage : IHabboMessage
{
#region Fields
///
/// The ID of this message as an unsigned 32 bit integer.
///
private uint mID;
///
/// The content of this message as a System.Collections.Generic.List(byte).
///
private List mContent;
#endregion
#region Properties
///
/// Gets the ID of this message as an unsigned 32 bit integer.
///
public uint ID
{
get { return mID; }
}
///
/// Gets theheader of this message, by Base64 encoding the message ID to a 3 byte string.
///
public string Header
{
get
{
return IonEnvironment.GetDefaultTextEncoding().GetString(Base64Encoding.Encodeuint(mID, 2));
}
}
///
/// Gets the length of the content in this message.
///
public int contentLength
{
get { return mContent.Count; }
}
#endregion
#region Constructors
///
/// Constructs a weak ServerMessage, which is not useable until Initialize() is called.
///
public ServerMessage()
{
// Requires a call to Initialize before usage
}
///
/// Constructs a ServerMessage object with a given ID and no content.
///
/// The ID of this message as an unsigned 32 bit integer.
public ServerMessage(uint ID)
{
Initialize(ID);
}
#endregion
#region Methods
///
/// Clears all the content in the message and sets the message ID.
///
/// The ID of this message as an unsigned 32 bit integer.
public void Initialize(uint ID)
{
mID = ID;
mContent = new List();
}
///
/// Clears the message content.
///
public void Clear()
{
mContent.Clear();
}
///
/// Returns the total content of this message as a string.
///
/// String
public string GetContentString()
{
return IonEnvironment.GetDefaultTextEncoding().GetString(mContent.ToArray());
}
///
/// Appends a single byte to the message content.
///
/// The byte to append.
public void Append(byte b)
{
mContent.Add(b);
}
///
/// Appends a byte array to the message content.
///
/// The byte array to append.
public void Append(byte[] bzData)
{
if (bzData != null && bzData.Length > 0)
mContent.AddRange(bzData);
}
///
/// Encodes a string with the environment's default text encoding and appends it to the message content.
///
/// The string to append.
public void Append(string s)
{
Append(s, null);
}
///
/// Encodes a string with a given text encoding and appends it to the message content.
///
/// The string to append.
/// A System.Text.Encoding to use for encoding the string.
public void Append(string s, Encoding pEncoding)
{
if (s != null && s.Length > 0)
{
if (pEncoding == null)
pEncoding = IonEnvironment.GetDefaultTextEncoding();
Append(pEncoding.GetBytes(s));
}
}
///
/// Appends a 32 bit integer in it's string representation to the message content.
///
/// The 32 bit integer to append.
public void Append(Int32 i)
{
Append(i.ToString(), Encoding.ASCII);
}
///
/// Appends a 32 bit unsigned integer in it's string representation to the message content.
///
/// The 32 bit unsigned integer to append.
public void Append(uint i)
{
Append((Int32)i);
}
///
/// Appends a wire encoded boolean to the message content.
///
/// The boolean to encode and append.
public void AppendBoolean(bool b)
{
if (b == true)
mContent.Add(WireEncoding.POSITIVE);
else
mContent.Add(WireEncoding.NEGATIVE);
}
///
/// Appends a wire encoded 32 bit integer to the message content.
///
/// The 32 bit integer to encode and append.
public void AppendInt32(Int32 i)
{
Append(WireEncoding.EncodeInt32(i));
}
///
/// Appends a wire encoded 32 bit unsigned integer to the message content.
///
/// The 32 bit unsigned integer to encode and append.
/// AppendInt32
public void AppendUInt32(uint i)
{
AppendInt32((Int32)i);
}
///
/// Appends a string with the default string breaker byte to the message content.
///
/// The string to append.
public void AppendString(string s)
{
AppendString(s, 2);
}
///
/// Appends a string with a given string breaker byte to the message content.
///
/// The string to append.
/// The byte used to mark the end of the string.
public void AppendString(string s, byte Breaker)
{
Append(s); // Append string with default encoding
Append(Breaker); // Append breaker
}
public void AppendObject(ISerializableObject obj)
{
obj.Serialize(this);
}
public byte[] GetBytes()
{
byte[] Data = new byte[contentLength + 2 + 1];
byte[] Header = Base64Encoding.Encodeuint(mID, 2);
Data[0] = Header[0];
Data[1] = Header[1];
for (int i = 0; i < this.contentLength; i++)
{
Data[i + 2] = mContent[i];
}
Data[Data.Length - 1] = 1;
return Data;
}
#endregion
}
}