Пример #1
0
        // Will hold the buffers until they are sent
        public static void ScheduleSend(LaneSetup setup, DateTime now, ToSend[] tosend, uint count)
        {
            for (uint i = 0; i < count; i++)
            {
                Lane lane = tosend[i].Lane;

                if (!tosend[i].Reliable)
                {
                    uint target = (uint)lane.Out.Length;
                    if (lane.OutCount == lane.Out.Length)
                    {
                        for (uint k = 0; k < lane.Out.Length; k++)
                        {
                            if (lane.Out[k].Source == null)
                            {
                                target = k;
                                break;
                            }
                        }
                    }
                    else
                    {
                        target = lane.OutCount++;
                    }

                    if (target == lane.Out.Length)
                    {
                        // Dropped
                        continue;
                    }

                    if (tosend[i].CanKeepData)
                    {
                        lane.Out[target].Source = tosend[i].Data;
                    }
                    else
                    {
                        lane.Out[target].Source = setup.Factory.GetBuffer(tosend[i].Data.bytesize);
                        Bitstream.Insert(lane.Out[target].Source, tosend[i].Data);
                        Bitstream.SyncByte(lane.Out[target].Source);
                        lane.Out[target].Source.Flip();
                    }

                    lane.Out[target].Source.userdata = 1;                     // refcount

                    lane.Out[target].Begin           = 0;
                    lane.Out[target].End             = tosend[i].Data.bytesize;
                    lane.Out[target].IsFinalPiece    = true;
                    lane.Out[target].SendTime        = now;
                    lane.Out[target].InitialSendTime = now;
                    lane.Out[target].SendCount       = 0;
                    lane.Out[target].SeqId           = ++lane.OutgoingSeqUnreliable;
                    lane.Out[target].Reliable        = false;
                }
                else
                {
                    uint   segmentSize = setup.MaxPacketSize - 32 - setup.ReservedHeaderBytes;
                    uint   numSegments = (uint)(((tosend[i].Data.bytesize - tosend[i].Data.bytepos) + segmentSize - 1) / segmentSize);
                    uint[] outSlots    = new uint[256];
                    if (numSegments == 0 || numSegments > outSlots.Length)
                    {
                        continue;
                    }

                    uint write = 0;
                    for (uint k = 0; k < lane.OutCount; k++)
                    {
                        if (lane.Out[k].Source == null)
                        {
                            outSlots[write++] = k;
                            if (write == numSegments)
                            {
                                break;
                            }
                        }
                    }

                    uint left = numSegments - write;
                    if ((lane.Out.Length - lane.OutCount) < left)
                    {
                        // no room.
                        continue;
                    }

                    for (uint k = 0; k < left; k++)
                    {
                        outSlots[write++] = lane.OutCount++;
                    }

                    Bitstream.Buffer source = tosend[i].Data;
                    if (!tosend[i].CanKeepData)
                    {
                        source = setup.Factory.GetBuffer(tosend[i].Data.bytesize);
                        Bitstream.Insert(source, tosend[i].Data);
                        source.Flip();
                    }

                    source.userdata = 0;
                    uint RangeBegin = 0;
                    for (uint k = 0; k < numSegments; k++)
                    {
                        uint bytesLeft = source.bytesize - RangeBegin;
                        if (bytesLeft == 0)
                        {
                            Console.WriteLine("ERROR: bytesLeft=0!");
                            continue;
                        }
                        uint toWrite = bytesLeft < segmentSize ? bytesLeft : segmentSize;
                        uint slot    = outSlots[k];
                        source.userdata++;
                        lane.Out[slot].Source          = source;
                        lane.Out[slot].Begin           = RangeBegin;
                        lane.Out[slot].End             = RangeBegin + toWrite;
                        lane.Out[slot].IsFinalPiece    = k == (numSegments - 1);
                        lane.Out[slot].SendTime        = now;
                        lane.Out[slot].InitialSendTime = now;
                        lane.Out[slot].SendCount       = 0;
                        lane.Out[slot].Reliable        = true;
                        lane.Out[slot].SeqId           = ++lane.OutgoingSeqReliable;
                        RangeBegin = RangeBegin + toWrite;
                    }
                }
            }
        }
Пример #2
0
        // Return if there is more to come.
        public static bool ProcessLanesIncoming(LaneSetup setup, Lane[] lanes, Done[] output, out uint numOut)
        {
            numOut = 0;

            // Check the progress if we can pick another one.
            for (int i = 0; i < lanes.Length; i++)
            {
                Lane lane = lanes[i];

                while (lane.DoneTail != lane.DoneHead)
                {
                    uint t = lane.DoneTail % (uint)lane.Done.Length;
                    output[numOut] = lane.Done[t];
                    lane.DoneTail  = lane.DoneTail + 1;
                    if (++numOut == output.Length)
                    {
                        return(true);
                    }
                }

                if (lane.ProgressHead == lane.ProgressTail)
                {
                    continue;
                }

                uint numProgress = (uint)lane.Progress.Length;

                // Clear out any seqId=0 (these are empty slots).
                while (lane.ProgressHead != lane.ProgressTail && lane.Progress[lane.ProgressTail % numProgress].SeqId == 0)
                {
                    lane.ProgressTail++;
                }

                // And from the head too...
                while (lane.ProgressHead != lane.ProgressTail && lane.Progress[(numProgress + lane.ProgressHead - 1) % numProgress].SeqId == 0)
                {
                    lane.ProgressHead--;
                }

                if (lane.ProgressHead == lane.ProgressTail)
                {
                    continue;
                }

                // Sort them by seq. There shouldn't be so many here. Maybe do something more clever if the count grows big.
                uint count = lane.ProgressHead - lane.ProgressTail;
                while (count > 1)
                {
                    bool swapped = false;
                    for (uint j = 0; j < (count - 1); j++)
                    {
                        uint idx0 = (lane.ProgressTail + j) % numProgress;
                        uint idx1 = (lane.ProgressTail + j + 1) % numProgress;
                        if (lane.Progress[idx0].SeqId > lane.Progress[idx1].SeqId)
                        {
                            InProgress tmp = lane.Progress[idx0];
                            lane.Progress[idx0] = lane.Progress[idx1];
                            lane.Progress[idx1] = tmp;
                            swapped             = true;
                        }
                    }
                    if (!swapped)
                    {
                        break;
                    }
                }

                // Guaranteed to have them sorted in order now.
                uint head = lane.ProgressTail;
                uint next = lanes[i].ReliableSeq + 1;

                uint[] aggregateIdx   = new uint[128];
                uint   aggregateCount = 0;

                uint tail = lane.ProgressTail;
                while (tail != lane.ProgressHead)
                {
                    uint idx = tail % numProgress;
                    if (lane.Progress[idx].SeqId != next)
                    {
                        break;
                    }
                    if (lane.Progress[idx].IsFinalPiece)
                    {
                        if (aggregateCount == 0)
                        {
                            output[numOut].ArrivalTime    = lane.Progress[idx].ArrivalTime;
                            output[numOut].CompletionTime = lane.Progress[idx].ArrivalTime;
                            output[numOut].Reliable       = true;
                            output[numOut].SeqId          = lane.Progress[idx].SeqId;
                            output[numOut].Data           = lane.Progress[idx].Data;
                            output[numOut].Lane           = lane;
                            lane.Progress[idx].SeqId      = 0;
                            lane.ReliableSeq = next;
                            tail             = tail + 1;
                            next             = next + 1;
                            if (++numOut == output.Length)
                            {
                                return(true);
                            }
                        }
                        else
                        {
                            // There is always one room because check belowe makes sure of that.
                            aggregateIdx[aggregateCount++] = idx;
                            uint bits = 0;
                            for (int k = 0; k < aggregateCount; k++)
                            {
                                uint ki = aggregateIdx[k];
                                bits = bits + lane.Progress[ki].Data.BitsLeft();
                            }
                            Bitstream.Buffer total = setup.Factory.GetBuffer(bits / 8 + 16);
                            for (int k = 0; k < aggregateCount; k++)
                            {
                                uint ki = aggregateIdx[k];
                                Bitstream.Insert(total, lane.Progress[ki].Data);
                                lane.Progress[ki].SeqId = 0;
                            }
                            total.Flip();
                            output[numOut].ArrivalTime    = lane.Progress[aggregateIdx[0]].ArrivalTime;
                            output[numOut].CompletionTime = lane.Progress[idx].ArrivalTime;
                            output[numOut].Reliable       = true;
                            output[numOut].SeqId          = lane.Progress[idx].SeqId;
                            output[numOut].Data           = total;
                            output[numOut].Lane           = lane;
                            lane.ReliableSeq = next;
                            tail             = tail + 1;
                            next             = next + 1;
                            aggregateCount   = 0;
                            if (++numOut == output.Length)
                            {
                                return(true);
                            }
                        }
                    }
                    else
                    {
                        aggregateIdx[aggregateCount] = idx;
                        next = next + 1;
                        tail = tail + 1;
                        if (++aggregateCount >= (aggregateIdx.Length - 1))
                        {
                            // THis should never happen.
                            Console.WriteLine("ERROR: Progress buffer reset because aggregateIdx overflowed.");
                            lane.ProgressHead = 0;
                            lane.ProgressTail = 0;
                            break;
                        }
                    }
                }
            }
            return(false);
        }
Пример #3
0
        // This will start reading from the buffers and modify the incoming array.
        public static void HandleIncomingPackets(LaneSetup setup, Incoming[] packets, uint packetsCount)
        {
            // Zero pass make own buffers.
            for (uint i = 0; i < packetsCount; i++)
            {
                if (!packets[i].CanKeepData)
                {
                    Bitstream.Buffer tmp  = packets[i].Packet;
                    Bitstream.Buffer copy = setup.Factory.GetBuffer((uint)tmp.buf.Length);
                    Bitstream.Insert(copy, tmp);
                    copy.Flip();
                    packets[i].Packet = copy;
                }
            }

            // First pass extract.
            for (uint i = 0; i < packetsCount; i++)
            {
                Bitstream.Buffer tmp  = packets[i].Packet;
                Lane             lane = packets[i].Lane;
                lane.Stats.RecvCount++;
                lane.Stats.RecvBytes += tmp.BitsLeft() / 8;

                bool reliable = Bitstream.ReadBits(tmp, 1) == 1;
                uint seq      = Bitstream.ReadCompressedUint(tmp);
                packets[i].Internal.Seq        = seq;
                packets[i].Internal.IsReliable = reliable;

                if (seq == 0)
                {
                    lane.Stats.RecvAckOnly++;
                }

                if (reliable)
                {
                    packets[i].Internal.IsFinalPiece = Bitstream.ReadBits(tmp, 1) == 1;

                    if (!packets[i].Internal.IsFinalPiece)
                    {
                        lane.Stats.RecvNonFinal++;
                    }

                    if (seq != 0)
                    {
                        if (lane.AckCount < lane.Acks.Length)
                        {
                            lane.Acks[lane.AckCount++] = seq;
                        }
                    }

                    while (true)
                    {
                        uint seqAck = Bitstream.ReadCompressedUint(tmp);
                        if (seqAck != 0)
                        {
                            for (int j = 0; j != lane.OutCount; j++)
                            {
                                if (lane.Out[j].SeqId == seqAck)
                                {
                                    double msLag = (packets[i].ArrivalTime - lane.Out[j].InitialSendTime).TotalMilliseconds;
                                    if (msLag > 0)
                                    {
                                        uint ms = (uint)msLag;
                                        if (ms < lane.LagMsMin || lane.LagMsMin == 0)
                                        {
                                            if (ms >= setup.MinResendTimeMs)
                                            {
                                                lane.LagMsMin = ms;
                                                lane.ResendMs = 2 * ms;
                                            }
                                        }
                                    }

                                    if (lane.Out[j].Source != null)
                                    {
                                        if (--lane.Out[j].Source.userdata == 0)
                                        {
                                            setup.Factory.ReturnBuffer(lane.Out[j].Source);
                                        }
                                        lane.Out[j].Source = null;
                                    }
                                }
                            }
                            continue;
                        }
                        break;
                    }
                }
                else
                {
                    packets[i].Internal.IsFinalPiece = true;
                }

                Bitstream.SyncByte(tmp);
            }

            // Unreliable packets, these are placed in the out queue after reading out the sequence and mode.
            for (int i = 0; i < packets.Length; i++)
            {
                if (packets[i].Internal.IsReliable || packets[i].Internal.Seq == 0)
                {
                    continue;
                }
                Lane lane = packets[i].Lane;
                if (lane.DoneHead - lane.DoneTail != lane.Done.Length)
                {
                    uint head = lane.DoneHead % (uint)lane.Done.Length;
                    lane.Done[head].Data           = packets[i].Packet;
                    lane.Done[head].Reliable       = false;
                    lane.Done[head].SeqId          = packets[i].Internal.Seq;
                    lane.Done[head].ArrivalTime    = packets[i].ArrivalTime;
                    lane.Done[head].CompletionTime = packets[i].ArrivalTime;
                    lane.Done[head].Lane           = lane;
                    lane.DoneHead = lane.DoneHead + 1;
                }
            }

            // Handle reliable. These go straight
            for (int i = 0; i < packets.Length; i++)
            {
                if (!packets[i].Internal.IsReliable || packets[i].Internal.Seq == 0)
                {
                    continue;
                }

                Lane lane = packets[i].Lane;

                if (packets[i].Internal.Seq <= lane.ReliableSeq)
                {
                    lane.Stats.RecvDupes++;
                    continue;
                }

                // If packet arrives a second time, ignore.
                uint numProgress = (uint)lane.Progress.Length;
                bool hadit       = false;
                for (uint p = lane.ProgressTail; p != lane.ProgressHead; p++)
                {
                    uint idx = p % numProgress;
                    if (lane.Progress[idx].SeqId == packets[i].Internal.Seq)
                    {
                        hadit = true;
                        break;
                    }
                }

                if (hadit)
                {
                    // Already got the packet for this sequence id.
                    lane.Stats.RecvDupes++;
                    continue;
                }

                if (lane.ProgressHead - lane.ProgressTail != lane.Progress.Length)
                {
                    uint head = lane.ProgressHead % (uint)lane.Progress.Length;
                    lane.Progress[head].Data         = packets[i].Packet;
                    lane.Progress[head].IsFinalPiece = packets[i].Internal.IsFinalPiece;
                    lane.Progress[head].ArrivalTime  = packets[i].ArrivalTime;
                    lane.Progress[head].SeqId        = packets[i].Internal.Seq;
                    lane.ProgressHead = lane.ProgressHead + 1;
                }
            }
        }