Exemplo n.º 1
0
            public unsafe int Send <T>(T driver, NetworkPipeline pipeline, NetworkConnection connection, NativeSlice <byte> payloadData) where T : struct, INetworkPipelineSender
            {
                var p = m_Pipelines[pipeline.Id - 1];

                var connectionId = connection.m_NetworkId;
                int startStage   = 0;

                // TODO: not really read-only, just hacking the safety system
                NativeArray <byte> tmpBuffer = sendBuffer;
                int *sendBufferLock          = (int *)tmpBuffer.GetUnsafeReadOnlyPtr();

                sendBufferLock += connectionId * sizePerConnection[SendSizeOffset] / 4;

                while (Interlocked.CompareExchange(ref *sendBufferLock, 1, 0) != 0)
                {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                    throw new InvalidOperationException("The parallel network driver needs to process a single unique connection per job, processing a single connection multiple times in a parallel for is not supported.");
#endif
                }

                NativeList <UpdatePipeline> currentUpdates = new NativeList <UpdatePipeline>(128, Allocator.Temp);
                ProcessPipelineSend(driver, startStage, pipeline, connection, payloadData, currentUpdates);
                // Move the updates requested in this iteration to the concurrent queue so it can be read/parsed in update routine
                for (int i = 0; i < currentUpdates.Length; ++i)
                {
                    m_SendStageNeedsUpdateWrite.Enqueue(currentUpdates[i]);
                }

                Interlocked.Exchange(ref *sendBufferLock, 0);
                return(payloadData.Length);
            }
Exemplo n.º 2
0
        private void ProcessReceiveStage(int stage, NetworkPipeline pipeline, int internalBufferOffset, int internalSharedBufferOffset, ref NetworkPipelineContext ctx, ref NativeSlice <byte> inboundBuffer, ref NativeList <int> resumeQ, ref bool needsUpdate, ref bool needsSendUpdate)
        {
            bool needsResume = false;
            var  p           = m_Pipelines[pipeline.Id - 1];

            ctx.internalProcessBuffer =
                new NativeSlice <byte>(m_ReceiveBuffer, internalBufferOffset, m_StageCollection.GetReceiveCapacity(m_StageList[p.FirstStageIndex + stage]));
            ctx.internalSharedProcessBuffer =
                new NativeSlice <byte>(m_SharedBuffer, internalSharedBufferOffset, m_StageCollection.GetSharedStateCapacity(m_StageList[p.FirstStageIndex + stage]));
            var stageId = m_StageList[p.FirstStageIndex + stage];

            inboundBuffer = m_StageCollection.InvokeReceive(stageId, ctx, inboundBuffer, ref needsResume, ref needsUpdate, ref needsSendUpdate);

            if (needsResume)
            {
                resumeQ.Add(stage);
            }
        }
Exemplo n.º 3
0
        public void GetPipelineBuffers(NetworkPipeline pipelineId, int stageId, NetworkConnection connection,
                                       ref NativeSlice <byte> readProcessingBuffer, ref NativeSlice <byte> writeProcessingBuffer,
                                       ref NativeSlice <byte> sharedBuffer)
        {
            var pipeline = m_Pipelines[pipelineId.Id - 1];

            int recvBufferOffset   = pipeline.receiveBufferOffset + sizePerConnection[RecveiveSizeOffset] * connection.InternalId;
            int sendBufferOffset   = pipeline.sendBufferOffset + sizePerConnection[SendSizeOffset] * connection.InternalId;
            int sharedBufferOffset = pipeline.sharedBufferOffset + sizePerConnection[SharedSizeOffset] * connection.InternalId;

            int  stageIndexInList;
            bool stageNotFound = true;

            for (stageIndexInList = pipeline.FirstStageIndex;
                 stageIndexInList < pipeline.FirstStageIndex + pipeline.NumStages;
                 stageIndexInList++)
            {
                if (m_StageList[stageIndexInList] == stageId)
                {
                    stageNotFound = false;
                    break;
                }
                sendBufferOffset   += m_StageCollection.GetSendCapacity(m_StageList[stageIndexInList]);
                recvBufferOffset   += m_StageCollection.GetReceiveCapacity(m_StageList[stageIndexInList]);
                sharedBufferOffset += m_StageCollection.GetSharedStateCapacity(m_StageList[stageIndexInList]);
            }

            if (stageNotFound)
#if ENABLE_UNITY_COLLECTIONS_CHECKS
            { throw new InvalidOperationException("Could not find stage ID " + stageId +
                                                  " make sure the type for this stage ID is added when the pipeline is created."); }
#else
            { return; }
#endif

            writeProcessingBuffer =
                new NativeSlice <byte>(m_SendBuffer, sendBufferOffset, m_StageCollection.GetSendCapacity(m_StageList[stageIndexInList]));
            readProcessingBuffer =
                new NativeSlice <byte>(m_ReceiveBuffer, recvBufferOffset, m_StageCollection.GetReceiveCapacity(m_StageList[stageIndexInList]));
            sharedBuffer =
                new NativeSlice <byte>(m_SharedBuffer, sharedBufferOffset, m_StageCollection.GetSharedStateCapacity(m_StageList[stageIndexInList]));
        }
Exemplo n.º 4
0
        private static void AddSendUpdate(NetworkConnection connection, int stageId, NetworkPipeline pipelineId, NativeList <UpdatePipeline> currentUpdates)
        {
            var newUpdate = new UpdatePipeline
            {
                connection = connection, stage = stageId, pipeline = pipelineId
            };
            bool uniqueItem = true;

            for (int j = 0; j < currentUpdates.Length; ++j)
            {
                if (currentUpdates[j].stage == newUpdate.stage &&
                    currentUpdates[j].pipeline.Id == newUpdate.pipeline.Id &&
                    currentUpdates[j].connection == newUpdate.connection)
                {
                    uniqueItem = false;
                }
            }
            if (uniqueItem)
            {
                currentUpdates.Add(newUpdate);
            }
        }
Exemplo n.º 5
0
        private void ProcessReceiveStagesFrom <T>(T driver, int startStage, NetworkPipeline pipeline, NetworkConnection connection, NativeSlice <byte> buffer) where T : struct, INetworkPipelineReceiver
        {
            var p            = m_Pipelines[pipeline.Id - 1];
            var connectionId = connection.m_NetworkId;
            var resumeQ      = new NativeList <int>(16, Allocator.Temp);
            int resumeQStart = 0;

            NetworkPipelineContext ctx = default(NetworkPipelineContext);

            ctx.timestamp = Timestamp;
            var inboundBuffer = new NativeSlice <byte>(buffer, 0, buffer.Length);

            ctx.header = default(DataStreamWriter);
            NativeList <UpdatePipeline> sendUpdates = new NativeList <UpdatePipeline>(128, Allocator.Temp);

            while (true)
            {
                bool needsUpdate                = false;
                bool needsSendUpdate            = false;
                int  internalBufferOffset       = p.receiveBufferOffset + sizePerConnection[RecveiveSizeOffset] * connectionId;
                int  internalSharedBufferOffset = p.sharedBufferOffset + sizePerConnection[SharedSizeOffset] * connectionId;

                // Adjust offset accounting for stages in front of the starting stage, since we're parsing the stages in reverse order
                for (int st = 0; st < startStage; ++st)
                {
                    internalBufferOffset       += m_StageCollection.GetReceiveCapacity(m_StageList[p.FirstStageIndex + st]);
                    internalSharedBufferOffset += m_StageCollection.GetSharedStateCapacity(m_StageList[p.FirstStageIndex + st]);
                }

                for (int i = startStage; i >= 0; --i)
                {
                    ProcessReceiveStage(i, pipeline, internalBufferOffset, internalSharedBufferOffset, ref ctx, ref inboundBuffer, ref resumeQ, ref needsUpdate, ref needsSendUpdate);
                    if (needsUpdate)
                    {
                        var newUpdate = new UpdatePipeline
                        {
                            connection = connection, stage = i, pipeline = pipeline
                        };
                        bool uniqueItem = true;
                        for (int j = 0; j < m_ReceiveStageNeedsUpdate.Length; ++j)
                        {
                            if (m_ReceiveStageNeedsUpdate[j].stage == newUpdate.stage &&
                                m_ReceiveStageNeedsUpdate[j].pipeline.Id == newUpdate.pipeline.Id &&
                                m_ReceiveStageNeedsUpdate[j].connection == newUpdate.connection)
                            {
                                uniqueItem = false;
                            }
                        }
                        if (uniqueItem)
                        {
                            m_ReceiveStageNeedsUpdate.Add(newUpdate);
                        }
                    }

                    if (needsSendUpdate)
                    {
                        AddSendUpdate(connection, i, pipeline, m_SendStageNeedsUpdate);
                    }

                    if (inboundBuffer.Length == 0)
                    {
                        break;
                    }

                    // Offset needs to be adjusted for the next pipeline (the one in front of this one)
                    if (i > 0)
                    {
                        internalBufferOffset -=
                            m_StageCollection.GetReceiveCapacity(m_StageList[p.FirstStageIndex + i - 1]);
                        internalSharedBufferOffset -=
                            m_StageCollection.GetSharedStateCapacity(m_StageList[p.FirstStageIndex + i - 1]);
                    }

                    needsUpdate = false;
                }

                if (inboundBuffer.Length != 0)
                {
                    driver.PushDataEvent(connection, inboundBuffer);
                }

                if (resumeQStart >= resumeQ.Length)
                {
                    return;
                }

                startStage    = resumeQ[resumeQStart++];
                inboundBuffer = default(NativeSlice <byte>);
            }
        }
Exemplo n.º 6
0
            internal unsafe void ProcessPipelineSend <T>(T driver, int startStage, NetworkPipeline pipeline, NetworkConnection connection,
                                                         NativeSlice <byte> payloadBuffer, NativeList <UpdatePipeline> currentUpdates) where T : struct, INetworkPipelineSender
            {
                NetworkPipelineContext ctx = default(NetworkPipelineContext);

                ctx.timestamp = m_timestamp[0];
                var p            = m_Pipelines[pipeline.Id - 1];
                var connectionId = connection.m_NetworkId;

                var resumeQ      = new NativeList <int>(16, Allocator.Temp);
                int resumeQStart = 0;

                ctx.header = new DataStreamWriter(p.headerCapacity, Allocator.Temp);

                var inboundBuffer = default(InboundBufferVec);

                inboundBuffer.buffer1 = payloadBuffer;

                var prevHeader = new DataStreamWriter(p.headerCapacity, Allocator.Temp);

                while (true)
                {
                    int internalBufferOffset       = p.sendBufferOffset + sizePerConnection[SendSizeOffset] * connectionId;
                    int internalSharedBufferOffset = p.sharedBufferOffset + sizePerConnection[SharedSizeOffset] * connectionId;

                    bool needsUpdate = false;
                    // If this is not the first stage we need to fast forward the buffer offset to the correct place
                    if (startStage > 0)
                    {
                        for (int i = 0; i < startStage; ++i)
                        {
                            internalBufferOffset       += m_StageCollection.GetSendCapacity(m_StageList[p.FirstStageIndex + i]);
                            internalSharedBufferOffset += m_StageCollection.GetSharedStateCapacity(m_StageList[p.FirstStageIndex + i]);
                        }
                    }

                    for (int i = startStage; i < p.NumStages; ++i)
                    {
                        var prevInbound = inboundBuffer;
                        ProcessSendStage(i, internalBufferOffset, internalSharedBufferOffset, p, ref resumeQ, ref ctx, ref inboundBuffer, ref needsUpdate);
                        if (inboundBuffer.buffer1 == prevInbound.buffer1 &&
                            inboundBuffer.buffer2 == prevInbound.buffer2)
                        {
                            if (ctx.header.Length > 0)
                            {
                                if (prevHeader.Length > 0)
                                {
                                    ctx.header.WriteBytes(prevHeader.GetUnsafeReadOnlyPtr(), prevHeader.Length);
                                }
                                prevHeader.Clear();
                                var tempHeader = ctx.header;
                                ctx.header = prevHeader;
                                prevHeader = tempHeader;
                                if (inboundBuffer.buffer2.Length == 0)
                                {
                                    inboundBuffer.buffer2 = inboundBuffer.buffer1;
                                }
                                inboundBuffer.buffer1 = prevHeader.GetNativeSlice(0, prevHeader.Length);
                            }
                        }
                        else
                        {
#if ENABLE_UNITY_COLLECTIONS_CHECKS
                            if (inboundBuffer.buffer2.Length > 0)
                            {
                                throw new InvalidOperationException("Pipeline send stages must return either the unmodified inbound buffers or a consolidated version with a single buffer");
                            }
#endif
                            // Prev header is now part of payload
                            prevHeader.Clear();
                            if (ctx.header.Length > 0)
                            {
                                var tempHeader = ctx.header;
                                ctx.header            = prevHeader;
                                prevHeader            = tempHeader;
                                inboundBuffer.buffer2 = inboundBuffer.buffer1;
                                inboundBuffer.buffer1 = prevHeader.GetNativeSlice(0, prevHeader.Length);
                            }
                        }
                        if (needsUpdate)
                        {
                            AddSendUpdate(connection, i, pipeline, currentUpdates);
                        }
                        if (inboundBuffer.buffer1.Length == 0)
                        {
                            break;
                        }

                        needsUpdate = false;

                        internalBufferOffset       += ctx.internalProcessBuffer.Length;
                        internalSharedBufferOffset += ctx.internalSharedProcessBuffer.Length;
                    }

                    if (inboundBuffer.buffer1.Length != 0)
                    {
                        var iov        = stackalloc network_iovec[4];
                        var pipelineId = pipeline.Id;
                        iov[0].buf = &pipelineId;
                        iov[0].len = 1;
                        iov[1].buf = ctx.header.GetUnsafePtr();
                        iov[1].len = ctx.header.Length;
                        iov[2].buf = inboundBuffer.buffer1.GetUnsafeReadOnlyPtr();
                        iov[2].len = inboundBuffer.buffer1.Length;
                        if (inboundBuffer.buffer2.Length > 0)
                        {
                            iov[3].buf = inboundBuffer.buffer2.GetUnsafeReadOnlyPtr();
                            iov[3].len = inboundBuffer.buffer2.Length;
                            // FIXME: handle send errors
                            driver.Send(connection, iov, 4);
                        }
                        else
                        {
                            driver.Send(connection, iov, 3);
                        }
                    }

                    if (resumeQStart >= resumeQ.Length)
                    {
                        break;
                    }

                    startStage = resumeQ[resumeQStart++];

                    prevHeader.Clear();
                    inboundBuffer = default(InboundBufferVec);
                }
            }
Exemplo n.º 7
0
 public bool Equals(NetworkPipeline connection)
 {
     return(connection.Id == Id);
 }