Example #1
0
        public async Task <TcpMessage> Receieve()
        {
            var buffer         = new byte[_tcpClient.ReceiveBufferSize];
            var availableBytes = await _tcpClient.GetStream().ReadAsync(buffer, 0, buffer.Length);

            var result = buffer.Take(availableBytes).ToArray();

            return(TcpMessage.Decode(result));
        }
Example #2
0
        public async Task Send(byte[] packet)
        {
            if (!_tcpClient.Connected)
                throw new InvalidOperationException("Client not connected to server.");

            var tcpMessage = new TcpMessage(sendCounter, packet);

            await _tcpClient.GetStream().WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length);
            sendCounter++;
        }
Example #3
0
        public async Task Send(byte[] packet)
        {
            if (!_tcpClient.Connected)
            {
                throw new InvalidOperationException("Client not connected to server.");
            }

            var tcpMessage = new TcpMessage(sendCounter, packet);

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

            sendCounter++;
        }
Example #4
0
        public async Task Send(byte[] packet, CancellationToken token = default(CancellationToken))
        {
            if (!_tcpClient.Connected)
            {
                throw new InvalidOperationException("Client not connected to server.");
            }

            var tcpMessage = new TcpMessage(sendCounter, packet);

            await _stream.WriteAsync(tcpMessage.Encode(), 0, tcpMessage.Encode().Length, token).ConfigureAwait(false);

            sendCounter++;
        }
Example #5
0
        public async Task <byte[]> Receive(CancellationToken token = default(CancellationToken))
        {
            token.ThrowIfCancellationRequested();

            TcpMessage result = await this.transport.Receive(token).ConfigureAwait(false);

            using (MemoryStream memoryStream = new MemoryStream(result.Body))
            {
                using (BinaryReader binaryReader = new BinaryReader(memoryStream))
                {
                    long authKeyid     = binaryReader.ReadInt64();
                    long messageId     = binaryReader.ReadInt64();
                    int  messageLength = binaryReader.ReadInt32();

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

                    return(response);
                }
            }
        }