using System; using System.Net; namespace OpenMetaverse { // this class encapsulates a single packet that // is either sent or received by a UDP socket public class UDPPacketBuffer { /// Size of the byte array used to store raw packet data public const int DEFAULT_BUFFER_SIZE = 4096; /// Raw packet data buffer public readonly byte[] Data; /// Length of the data to transmit public int DataLength; /// EndPoint of the remote host public EndPoint RemoteEndPoint; /// /// Create an allocated UDP packet buffer for receiving a packet /// public UDPPacketBuffer() { Data = new byte[DEFAULT_BUFFER_SIZE]; // Will be modified later by BeginReceiveFrom() RemoteEndPoint = new IPEndPoint(Settings.BIND_ADDR, 0); } /// /// Create an allocated UDP packet buffer for sending a packet /// /// EndPoint of the remote host public UDPPacketBuffer(IPEndPoint endPoint) { Data = new byte[DEFAULT_BUFFER_SIZE]; RemoteEndPoint = endPoint; } /// /// Create an allocated UDP packet buffer for sending a packet /// /// EndPoint of the remote host /// Size of the buffer to allocate for packet data public UDPPacketBuffer(IPEndPoint endPoint, int bufferSize) { Data = new byte[bufferSize]; RemoteEndPoint = endPoint; } /// /// Create an allocated UDP packet buffer for sending a packet /// public UDPPacketBuffer(byte[] buffer, int bufferSize, IPEndPoint destination, int category) { Data = new byte[bufferSize]; this.CopyFrom(buffer, bufferSize); DataLength = bufferSize; RemoteEndPoint = destination; } /// /// Create an allocated UDP packet buffer for sending a packet /// /// EndPoint of the remote host /// The actual buffer to use for packet data (no allocation). public UDPPacketBuffer(IPEndPoint endPoint, byte[] data) { Data = data; RemoteEndPoint = endPoint; } public void CopyFrom(Array src, int length) { Buffer.BlockCopy(src, 0, this.Data, 0, length); } public void CopyFrom(Array src) { this.CopyFrom(src, src.Length); } public void ResetEndpoint() { RemoteEndPoint = new IPEndPoint(Settings.BIND_ADDR, 0); } } }