private async Task PushToQueue(TcpMessage message)
        {
            await AsyncHelper.RedirectToThreadPool();

            _messageQueue.Enqueue(message);
            await SendAllMessagesFromQueue();
        }
        public async Task SendMessage(TcpMessage tcpMessage)
        {
            await AsyncHelper.RedirectToThreadPool();

            var mesSeqNo = _messageSequenceNumber++;

            Debug.WriteLine($"Sending message with seq_no {mesSeqNo}");

            var encodedMessage = tcpMessage.Encode();

            await TcpService.Send(encodedMessage, tcpMessage.CancellationToken);
        }
        public async Task <Task <bool> > Send(byte[] packet, CancellationToken cancellationToken = default(CancellationToken))
        {
            await AsyncHelper.RedirectToThreadPool();

            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();

            TcpMessage tcpMessage = new TcpMessage(_messageSequenceNumber, packet, tcs, cancellationToken);

            await PushToQueue(tcpMessage);

            return(tcs.Task);
        }
Exemple #4
0
        public async Task <byte[]> Receive()
        {
            await AsyncHelper.RedirectToThreadPool();

            TcpMessage result = await transport.Receieve();

            using (var memoryStream = new MemoryStream(result.body))
            {
                using (BinaryReader binaryReader = new BinaryReader(memoryStream))
                {
                    binaryReader.ReadInt64(); // authKeyid
                    binaryReader.ReadInt64(); // messageId
                    int messageLength = binaryReader.ReadInt32();

                    byte[] response = binaryReader.ReadBytes(messageLength);

                    return(response);
                }
            }
        }
Exemple #5
0
        public async Task Send(byte[] packet)
        {
            if (!tcpClient.Connected)
            {
                throw new InvalidOperationException("Client not connected to server.");
            }

            var tcpMessage = new TcpMessage(sendCounter, packet);

            try
            {
                await sendLock.WaitAsync();

                await tcpClient.GetStream().WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length);

                sendCounter++;
            }
            finally
            {
                sendLock.Release();
            }
        }
        private async Task SendFromQueue()
        {
            await AsyncHelper.RedirectToThreadPool();

            while (!_messageQueue.IsEmpty)
            {
                _messageQueue.TryDequeue(out var item);
                TcpMessage message = item;

                try
                {
                    await SendMessage(message);

                    message.TaskCompletionSource.SetResult(true);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Sending confirmation for messages failed", ex);

                    message.TaskCompletionSource.SetException(ex);
                }
            }
        }