public HeapPointers CreateOutgoingMessage(ArraySegment <byte> payload, out byte headerSize, out bool dealloc) { // Calculate the amount of fragments required int fragmentsRequired = (payload.Count + (connection.MTU - 1)) / connection.MTU; if (fragmentsRequired > config.MaxFragments) { if (Logging.CurrentLogLevel <= LogLevel.Error) { Logging.LogError("Tried to create message that was too large. [Size=" + payload.Count + "] [FragmentsRequired=" + fragmentsRequired + "] [Config.MaxFragments=" + config.MaxFragments + "]"); } dealloc = false; headerSize = 0; return(null); } lock (_lock) { PendingOutgoingPacket unsafeOutgoing = _sendSequencer.GetUnsafe(_lastOutboundSequenceNumber + 1, out bool isSafe); if (unsafeOutgoing.Alive && !isSafe) { if (Logging.CurrentLogLevel <= LogLevel.Error) { Logging.LogError("Outgoing packet window is exhausted. Disconnecting"); } connection.Disconnect(false); dealloc = false; headerSize = 0; return(null); } // Increment the sequence number _lastOutboundSequenceNumber++; // Set header size headerSize = 6; // Alloc array HeapPointers memoryParts = memoryManager.AllocHeapPointers((uint)fragmentsRequired); int position = 0; for (ushort i = 0; i < fragmentsRequired; i++) { // Calculate message size int messageSize = Math.Min(connection.MTU, payload.Count - position); // Allocate memory for each fragment memoryParts.Pointers[memoryParts.VirtualOffset + i] = memoryManager.AllocHeapMemory((uint)(messageSize + 6)); // Write headers ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[0] = HeaderPacker.Pack((byte)MessageType.Data, false); ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[1] = channelId; // Write the sequence ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[2] = (byte)_lastOutboundSequenceNumber; ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[3] = (byte)(_lastOutboundSequenceNumber >> 8); // Write the fragment ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[4] = (byte)(i & 32767); ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer[5] = (byte)(((i & 32767) >> 8) | (byte)(i == fragmentsRequired - 1 ? 128 : 0)); // Write the payload Buffer.BlockCopy(payload.Array, payload.Offset + position, ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]).Buffer, 6, messageSize); // Increase the position position += messageSize; } // Tell the caller NOT to dealloc the memory, the channel needs it for resend purposes. dealloc = false; // Alloc outgoing fragment structs HeapPointers outgoingFragments = memoryManager.AllocHeapPointers((uint)fragmentsRequired); for (int i = 0; i < fragmentsRequired; i++) { // Add the memory to the outgoing sequencer array outgoingFragments.Pointers[outgoingFragments.VirtualOffset + i] = new PendingOutgoingFragment() { Alive = true, Attempts = 1, LastSent = DateTime.Now, FirstSent = DateTime.Now, Sequence = _lastOutboundSequenceNumber, Memory = ((HeapMemory)memoryParts.Pointers[memoryParts.VirtualOffset + i]) }; } // Add the memory to the outgoing sequencer _sendSequencer[_lastOutboundSequenceNumber] = new PendingOutgoingPacket() { Alive = true, Fragments = outgoingFragments }; return(memoryParts); } }
public HeapPointers CreateOutgoingMessage(ArraySegment <byte> payload, out byte headerSize, out bool dealloc) { if (payload.Count > connection.MTU) { if (Logging.CurrentLogLevel <= LogLevel.Error) { Logging.LogError("Tried to send message that was too large. Use a fragmented channel instead. [Size=" + payload.Count + "] [MaxMessageSize=" + config.MaxFragments + "]"); } dealloc = false; headerSize = 0; return(null); } lock (_lock) { PendingOutgoingPacket unsafeOutgoing = _sendSequencer.GetUnsafe(_lastOutboundSequenceNumber + 1, out bool isSafe); if (unsafeOutgoing.Alive && !isSafe) { if (Logging.CurrentLogLevel <= LogLevel.Error) { Logging.LogError("Outgoing packet window is exhausted. Disconnecting"); } connection.Disconnect(false); dealloc = false; headerSize = 0; return(null); } // Increment the sequence number _lastOutboundSequenceNumber++; // Set header size headerSize = 4; // Allocate the memory HeapMemory memory = memoryManager.AllocHeapMemory((uint)payload.Count + 4); // Write headers memory.Buffer[0] = HeaderPacker.Pack((byte)MessageType.Data, false); memory.Buffer[1] = channelId; // Write the sequence memory.Buffer[2] = (byte)_lastOutboundSequenceNumber; memory.Buffer[3] = (byte)(_lastOutboundSequenceNumber >> 8); // Copy the payload Buffer.BlockCopy(payload.Array, payload.Offset, memory.Buffer, 4, payload.Count); // Add the memory to pending _sendSequencer[_lastOutboundSequenceNumber] = new PendingOutgoingPacket() { Alive = true, Sequence = _lastOutboundSequenceNumber, Attempts = 1, LastSent = DateTime.Now, FirstSent = DateTime.Now, Memory = memory }; // Tell the caller NOT to dealloc the memory, the channel needs it for resend purposes. dealloc = false; // Allocate pointers HeapPointers pointers = memoryManager.AllocHeapPointers(1); // Point the first pointer to the memory pointers.Pointers[pointers.VirtualOffset] = memory; return(pointers); } }
public ArraySegment <byte>?HandleIncomingMessagePoll(ArraySegment <byte> payload, out byte headerBytes, out bool hasMore) { // Read the sequence number ushort sequence = (ushort)(payload.Array[payload.Offset] | (ushort)(payload.Array[payload.Offset + 1] << 8)); // Read the raw fragment data ushort encodedFragment = (ushort)(payload.Array[payload.Offset + 2] | (ushort)(payload.Array[payload.Offset + 3] << 8)); // The fragmentId is the last 15 least significant bits ushort fragment = (ushort)(encodedFragment & 32767); // IsFinal is the most significant bit bool isFinal = (ushort)((encodedFragment & 32768) >> 15) == 1; // Set the headerBytes headerBytes = 4; if (fragment > config.MaxFragments) { if (Logging.CurrentLogLevel <= LogLevel.Error) { Logging.LogError("FragmentId was too large. [FragmentId=" + fragment + "] [Config.MaxFragments=" + config.MaxFragments + "]. The fragment was silently dropped, expect a timeout."); } hasMore = false; return(null); } lock (_lock) { if (SequencingUtils.Distance(sequence, _incomingLowestAckedSequence, sizeof(ushort)) <= 0 || (_receiveSequencer[sequence].Alive && _receiveSequencer[sequence].IsComplete) || (_receiveSequencer[sequence].Alive && _receiveSequencer[sequence].Fragments != null && _receiveSequencer[sequence].Fragments.VirtualCount > fragment && _receiveSequencer[sequence].Fragments.Pointers[_receiveSequencer[sequence].Fragments.VirtualOffset + fragment] != null && !((HeapMemory)_receiveSequencer[sequence].Fragments.Pointers[_receiveSequencer[sequence].Fragments.VirtualOffset + fragment]).isDead)) { // We have already acked this message. Ack again connection.IncomingDuplicatePackets++; connection.IncomingDuplicateUserBytes += (ulong)payload.Count - 2; connection.IncomingDuplicateTotalBytes += (ulong)payload.Count + 2; SendAckEncoded(sequence, encodedFragment); hasMore = false; return(null); } else { // This is a packet after the last. One that is not yet completed PendingIncomingPacket unsafeIncoming = _receiveSequencer.GetUnsafe(sequence, out bool isSafe); if (unsafeIncoming.Alive && !isSafe) { if (Logging.CurrentLogLevel <= LogLevel.Error) { Logging.LogError("Incoming packet window is exhausted. Disconnecting"); } connection.Disconnect(false); hasMore = false; return(null); } else if (!_receiveSequencer[sequence].Alive) { // If this is the first fragment we ever get, index the data. // TODO: Alloc more size and just expand later in the pointer array HeapPointers fragmentPointers = memoryManager.AllocHeapPointers((uint)fragment + 1); _receiveSequencer[sequence] = new PendingIncomingPacket() { Alive = true, Fragments = fragmentPointers, Sequence = sequence, Size = isFinal ? (ushort?)(fragment + 1) : null }; } else { // If the first fragment we got was fragment 1 / 500. The fragments array will only be of size 128. We need to potentially resize it if (_receiveSequencer[sequence].Fragments.Pointers.Length - _receiveSequencer[sequence].Fragments.VirtualOffset <= fragment) { // We need to expand the fragments array. // Alloc new array HeapPointers newPointers = memoryManager.AllocHeapPointers((uint)fragment + 1); // Copy old values Array.Copy(_receiveSequencer[sequence].Fragments.Pointers, newPointers.Pointers, _receiveSequencer[sequence].Fragments.Pointers.Length); // Return the memory for the old memoryManager.DeAlloc(_receiveSequencer[sequence].Fragments); // Update the index _receiveSequencer[sequence] = new PendingIncomingPacket() { Fragments = newPointers, Alive = _receiveSequencer[sequence].Alive, Sequence = _receiveSequencer[sequence].Sequence, Size = isFinal ? (ushort?)(fragment + 1) : _receiveSequencer[sequence].Size }; } // We might also have to expand the virtual count if (_receiveSequencer[sequence].Fragments.VirtualCount <= fragment) { // Update the new virtual count _receiveSequencer[sequence].Fragments.VirtualCount = (uint)fragment + 1; // Update the struct to set the size if it has changed (TODO: Check if needed) _receiveSequencer[sequence] = new PendingIncomingPacket() { Fragments = _receiveSequencer[sequence].Fragments, Alive = _receiveSequencer[sequence].Alive, Sequence = _receiveSequencer[sequence].Sequence, Size = isFinal ? (ushort?)(fragment + 1) : _receiveSequencer[sequence].Size }; } } if (_receiveSequencer[sequence].Fragments.Pointers[_receiveSequencer[sequence].Fragments.VirtualOffset + fragment] == null || ((HeapMemory)_receiveSequencer[sequence].Fragments.Pointers[_receiveSequencer[sequence].Fragments.VirtualOffset + fragment]).isDead) { // Alloc some memory for the fragment HeapMemory memory = memoryManager.AllocHeapMemory((uint)payload.Count - 4); // Copy the payload Buffer.BlockCopy(payload.Array, payload.Offset + 4, memory.Buffer, 0, payload.Count - 4); // Add fragment to index _receiveSequencer[sequence].Fragments.Pointers[_receiveSequencer[sequence].Fragments.VirtualOffset + fragment] = memory; } // Send ack SendAckEncoded(sequence, encodedFragment); // Sequenced never returns the original memory. Thus we need to return null and tell the caller to Poll instead. hasMore = _receiveSequencer[_incomingLowestAckedSequence + 1].Alive && _receiveSequencer[_incomingLowestAckedSequence + 1].IsComplete; return(null); } } }
public ArraySegment <byte>?HandleIncomingMessagePoll(ArraySegment <byte> payload, out byte headerBytes, out bool hasMore) { // Read the sequence number ushort sequence = (ushort)(payload.Array[payload.Offset] | (ushort)(payload.Array[payload.Offset + 1] << 8)); // Set the headerBytes headerBytes = 2; lock (_lock) { if (SequencingUtils.Distance(sequence, _incomingLowestAckedSequence, sizeof(ushort)) <= 0 || _receiveSequencer[sequence].Alive) { // We have already acked this message. Ack again connection.IncomingDuplicatePackets++; connection.IncomingDuplicateUserBytes += (ulong)payload.Count - 2; connection.IncomingDuplicateTotalBytes += (ulong)payload.Count + 2; SendAck(sequence); hasMore = false; return(null); } else if (sequence == _incomingLowestAckedSequence + 1) { // This is the packet right after // If the one after is alive, we give set hasMore to true hasMore = _receiveSequencer[_incomingLowestAckedSequence + 2].Alive; _incomingLowestAckedSequence++; // Send ack SendAck(sequence); return(new ArraySegment <byte>(payload.Array, payload.Offset + 2, payload.Count - 2)); } else if (SequencingUtils.Distance(sequence, _incomingLowestAckedSequence, sizeof(ushort)) > 0) { // Future packet PendingIncomingPacket unsafeIncoming = _receiveSequencer.GetUnsafe(sequence, out bool isSafe); if (unsafeIncoming.Alive && !isSafe) { if (Logging.CurrentLogLevel <= LogLevel.Error) { Logging.LogError("Incoming packet window is exhausted. Disconnecting"); } connection.Disconnect(false); hasMore = false; return(null); } else if (!_receiveSequencer[sequence].Alive) { // Alloc payload plus header memory HeapMemory memory = memoryManager.AllocHeapMemory((uint)payload.Count - 2); // Copy the payload Buffer.BlockCopy(payload.Array, payload.Offset + 2, memory.Buffer, 0, payload.Count - 2); // Add to sequencer _receiveSequencer[sequence] = new PendingIncomingPacket() { Alive = true, Memory = memory, Sequence = sequence }; // Send ack SendAck(sequence); } } hasMore = false; return(null); } }