/// <summary> /// Read from UDP /// </summary> /// <param name="buffer"></param> /// <param name="size"></param> /// <returns></returns> public unsafe uint Read(byte *buffer, uint size) { // We can't do this if we just connect if (!m_ipSpecified) { return(0); } UDPBacklogEntry *entry = (UDPBacklogEntry *)m_queue.Pop(); if (entry == null) { return(0); } if (size > entry->Size) { size = entry->Size; } Memory.Memcpy(buffer, entry->Buffer, (int)size); Heap.Free(entry); return(size); }
/// <summary> /// Get size /// </summary> /// <returns></returns> public unsafe uint GetSize() { if (!m_connected) { return(0); } UDPBacklogEntry *entry = (UDPBacklogEntry *)m_queue.Peek(); return(entry->Size); }
/// <summary> /// Read packet from UDP /// </summary> /// <param name="buffer"></param> /// <param name="size"></param> /// <returns></returns> public unsafe uint ReadPacket(byte *buffer, uint size) { // Minimum size of 6 if (size < 6) { return(0); } UDPBacklogEntry *entry = (UDPBacklogEntry *)m_queue.Pop(); if (entry == null) { return(0); } if (size > entry->Size + sizeof(UDPPacketHeader)) { size = entry->Size + (uint)sizeof(UDPPacketHeader); } uint offset = (uint)sizeof(UDPPacketHeader); uint sizeData = size - offset; UDPPacketHeader *packet = (UDPPacketHeader *)buffer; packet->Size = sizeData; Memory.Memcpy(packet->IP, entry->IP, 4); if (sizeData > 0) { Memory.Memcpy(buffer + offset, entry->Buffer, (int)sizeData); } Heap.Free(entry); return(size); }
/// <summary> /// Receive on socket /// </summary> /// <param name="buffer"></param> /// <param name="size"></param> public unsafe void Receive(byte[] ip, byte *buffer, uint size) { // Do not accept empty packets if (size == 0) { return; } UDPBacklogEntry *entry = (UDPBacklogEntry *)Heap.Alloc(sizeof(UDPBacklogEntry)); if (size >= 2048) { size = 2048; } Memory.Memcpy(entry->Buffer, buffer, (int)size); entry->Size = size; Memory.Memcpy(entry->IP, Util.ObjectToVoidPtr(ip), 4); m_queue.Push(entry); }