using System;
using System.Text;
using System.Collections.Generic;
using System.IO;
using Aleeda.Specialized.Encoding;
namespace Aleeda.Net.Messages
{
public class ServerMessage
{
#region Fields
///
/// The ID of this message as an unsigned 32 bit integer.
///
private uint fID;
///
///The MemoryStream creates streams that have memory as a backing store instead of a disk or a network connection.
///
private MemoryStream Body;
#endregion
#region Properties
///
/// Gets the ID of this message as an unsigned 32 bit integer.
///
public uint ID
{
get { return fID; }
}
///
/// Gets the header of this message, by Base64 encoding the message ID to a 2 byte string.
///
public string Header
{
get
{
return AleedaEnvironment.GetDefaultEncoding().GetString(Base64Encoding.Encodeuint(ID, 2));
}
}
///
/// Gets the length of the content in this message.
///
public int contentLength
{
get
{
return (int)Body.Length;
}
}
#endregion
#region Constructors
///
/// Constructs a weak OutgoingMessage, which is not useable until Initialize() is called.
///
public ServerMessage()
{
// Requires a call to Initialize before usage
}
///
/// Constructs a OutgoingMessage 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)
{
this.fID = ID;
this.Body = new MemoryStream();
}
///
/// Clears the message content.
///
public void Clear()
{
Body.Dispose();
}
///
/// Returns the total content of this message as a string.
///
/// String
public string GetContentString()
{
return AleedaEnvironment.GetDefaultEncoding().GetString(Body.ToArray());
}
///
/// Appends a single byte to the message content.
///
/// The byte to append.
public void Append(byte b)
{
this.Body.WriteByte(b);
}
public void AppendChar(byte b)
{
this.Body.WriteByte(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)
this.Body.Write(bzData, 0, bzData.Length);
}
///
/// 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, AleedaEnvironment.GetDefaultEncoding());
}
///
/// 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 Encoding)
{
if (!string.IsNullOrEmpty(s))
{
if (Encoding == null)
Encoding = AleedaEnvironment.GetDefaultEncoding();
Append(Encoding.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.UTF8);
}
///
/// 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)
Body.WriteByte(WireEncoding.POSITIVE);
else
Body.WriteByte(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 void AppendRawInt32(Int32 i)
{
Append(i.ToString(), Encoding.ASCII);
}
// public void AppendRawInt32WithBreak(Int32 i, byte b)
// {
// AppendString(i.ToString(), b);
// }
public void AppendRawInt32WithBreak(Int32 i, byte b)
{
AppendString(i.ToString(), b);
}
public void AppendRawUInt(uint i)
{
Int32 _i = (Int32)i;
AppendRawInt32(_i);
}
public byte[] GetBytes()
{
byte[] Data = new byte[contentLength + 3];
byte[] Header = Base64Encoding.Encodeuint(ID, 2);
byte[] realData = Body.ToArray();
Data[0] = Header[0];
Data[1] = Header[1];
for (int i = 0; i < contentLength; i++)
{
Data[i + 2] = realData[i];
}
Data[Data.Length - 1] = 1;
return Data;
}
#endregion
}
}
// public class ServerMessage : IHabboMessage
// {
// #region Fields
// ///
// /// The ID of this message as an unsigned 32 bit integer.
// ///
// private uint mID;
// //private string B64Header;
// ///
// /// 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 AleedaEnvironment.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 AleedaEnvironment.GetDefaultTextEncoding().GetString(mContent.ToArray());
// }
// ///
// /// Appends a single byte to the message content.
// ///
// /// The byte to append.
// public void Append(byte b)
// {
// mContent.Add(b);
// }
// public void Append(object b)
// {
// Append(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 = AleedaEnvironment.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());
// }
// ///
// /// 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));
// }
// public void AppendInt32(Int32 i, byte b)
// {
// Append(WireEncoding.EncodeInt32(i));
// Append(b);
// }
// ///
// /// 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);
// }
// public void AppendRawInt32WithBreak(Int32 i, byte b)
// {
// AppendString(i.ToString(), b);
// }
// public void AppendRawInt32(Int32 i)
// {
// AppendString(i.ToString(), Encoding.ASCII);
// }
// public void AppendBytes(byte[] Data)
// {
// if (Data == null || Data.Length == 0)
// {
// return;
// }
// mContent.AddRange(Data);
// }
// public void AppendString(string s, Encoding Encoding)
// {
// if (s == null || s.Length == 0)
// {
// return;
// }
// AppendBytes(Encoding.GetBytes(s));
// }
// ///
// /// 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 AppendChar(byte Breaker)
// {
// 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
// }