using System;
using System.IO;
using ServerEngine.DataManagement.DataWrappers;
using ServerEngine.PacketManagement.Definitions;
namespace ServerEngine.NetworkManagement
{
public sealed class NetworkMessage
{
private IPacketTemplate packetTemplate;
private MemoryStream packetData;
///
/// Initializes a new instance of the class.
///
public NetworkMessage(NetID netID)
{
NetID = netID;
packetData = new MemoryStream();
}
///
/// This property contains the first two bytes of the packet.
/// If the packet is smaller than two bytes, this has { 0xFF, 0xFF }
///
public UInt16 Header
{
get
{
if (packetData.Length >= 2)
{
var tmp = packetData.Position;
packetData.Seek(0, SeekOrigin.Begin);
var buffer = new byte[2];
packetData.Read(buffer, 0, 2);
packetData.Position = tmp;
return BitConverter.ToUInt16(buffer, 0);
}
return (UInt16)(packetTemplate != null ? packetTemplate.Header : 0xFFFF);
}
}
///
/// This property contains the unique number
/// that every network client object gets after instantiation.
///
public NetID NetID { get; private set; }
///
/// This property contains the raw packet data as a byte list
///
public MemoryStream PacketData
{
get { return packetData; }
set { packetData = value; }
}
///
/// This property contains the packet data as a packet struct of type IPacketTemplate
///
public IPacketTemplate PacketTemplate
{
get { return packetTemplate; }
set { packetTemplate = value; }
}
}
}