예제 #1
0
        private void PrepareDataToSend(SocketAsyncEventArgs sendArgs)
        {
            // TODO: check for errors / connection closing
            // check if all data were send

            // TODO: validate - blocking queue impl

            while (true)
            {
                _sendBuffer.StartPacket();

                INetworkCommandAsync command;
                FramePacket          activeFrameTransfer;
                if (_sendCommandQueue.TryDequeue(out command))
                {
                    if (command.Status == OperationStatus.Request)
                    {
                        _activeRequests.TryAdd(command, 0);
                    }
                    command.WriteCommand(_sendBuffer);
                    _sendBuffer.FinalizePacket();
                }
                // priority frames should be less than 64kB - otherwise same algorithm as for "normal" priority frames must apply
                else if (_prioritySendQueue.TryPeek(out activeFrameTransfer))
                {
                    if (activeFrameTransfer.WriteData(_sendBuffer))
                    {
                        // all frame data were written
                        _prioritySendQueue.TryDequeue(out activeFrameTransfer);
                    }
                    _sendBuffer.FinalizePacket();
                }

                else if (_sendQueue.TryPeek(out activeFrameTransfer))
                {
                    if (activeFrameTransfer.WriteData(_sendBuffer))
                    {
                        // all frame data were written
                        _sendQueue.TryDequeue(out activeFrameTransfer);
                    }
                    _sendBuffer.FinalizePacket();
                }

                if (_sendBuffer.FinalizedPacket)
                {
                    sendArgs.SetBuffer(_sendBuffer.ReadOffset, _sendBuffer.WriteOffset - _sendBuffer.ReadOffset);
                    return;
                }

                if (_connection == null)
                {
                    return;
                }

                _sendEvent.WaitOne();

                if (_connection == null)
                {
                    return;
                }
            }
        }