Beispiel #1
0
        public async Task <int> ReadBytesAsync(byte[] bytes)
        {
            int otherIndex = 0;

            if (currentIndex < buffer.Length)
            {
                for ( ; currentIndex < buffer.Length && otherIndex < bytes.Length; currentIndex++, otherIndex++)
                {
                    bytes[otherIndex] = buffer.GetByte(currentIndex);
                }
            }

            while (!headersRead || (buffer.Length == buffer.Capacity && otherIndex < bytes.Length))
            {
                await inputStream.ReadAsync(buffer, buffer.Capacity, DefaultStreamOptions);

                headersRead = true;

                for (currentIndex = 0; currentIndex < buffer.Length && otherIndex < bytes.Length; currentIndex++, otherIndex++)
                {
                    bytes[otherIndex] = buffer.GetByte(currentIndex);
                }
            }

            return(otherIndex);
        }
Beispiel #2
0
        private async void PollInput(IAsyncAction operation)
        {
            while (!_tokenSource.IsCancellationRequested)
            {
                try
                {
                    IBuffer sizeBuffer = new Buffer(2);
                    await _socket.InputStream.ReadAsync(sizeBuffer, 2, InputStreamOptions.None);

                    uint size = (uint)((sizeBuffer.GetByte(0) | sizeBuffer.GetByte(1) << 8));

                    if (size != 0)
                    {
                        IBuffer report = new Buffer(size);
                        await _socket.InputStream.ReadAsync(report, size, InputStreamOptions.None);

                        if (ReportReceived != null)
                        {
                            ReportReceived(this, new ReportReceivedEventArgs {
                                Report = report.ToArray()
                            });
                        }
                    }
                }
                catch (Exception)
                {
                    // swallow exceptions...if we tank here, it's likely a disconnect and we can't do much anyway
                }
            }
        }
        private async Task PollInputAsync()
        {
            while (!_tokenSource.IsCancellationRequested)
            {
                try
                {
                    IBuffer sizeBuffer = new Buffer(2);
                    await _socket.InputStream.ReadAsync(sizeBuffer, 2, InputStreamOptions.None);

                    uint size = (uint)(sizeBuffer.GetByte(0) | sizeBuffer.GetByte(1) << 8);

                    if (size != 0)
                    {
                        IBuffer data = new Buffer(size);
                        await _socket.InputStream.ReadAsync(data, size, InputStreamOptions.None);

                        RaiseDataReceived(data.ToArray());
                    }
                }
                catch (TaskCanceledException)
                {
                    return;
                }
                catch (Exception)
                {
                    // swallow exceptions...if we tank here, it's likely a disconnect and we can't do much anyway
                }
            }
        }
Beispiel #4
0
        public void When_GetByteGreaterThanLength()
        {
            var sut = new Buffer(42)
            {
                Length = 21
            };

            sut.GetByte(41);             // We only assert this won't fail
        }
        private async void PollInput(IAsyncAction operation)
        {
            while (!_tokenSource.IsCancellationRequested)
            {
                try
                {
                    IBuffer sizeBuffer = new Buffer(2);
                    await _socket.InputStream.ReadAsync(sizeBuffer, 2, InputStreamOptions.None);
                    uint size = (uint)((sizeBuffer.GetByte(0) | sizeBuffer.GetByte(1) << 8));

                    if (size != 0)
                    {
                        IBuffer report = new Buffer(size);
                        await _socket.InputStream.ReadAsync(report, size, InputStreamOptions.None);
                        if (ReportReceived != null)
                            ReportReceived(this, new ReportReceivedEventArgs { Report = report.ToArray() });
                    }
                }
                catch (Exception)
                {
                    // swallow exceptions...if we tank here, it's likely a disconnect and we can't do much anyway
                }
            }
        }
Beispiel #6
0
        public void When_GetByteGreaterThanCapacity()
        {
            var sut = new Buffer(42);

            sut.GetByte(42);
        }
Beispiel #7
0
 private byte ReadByteFromBuffer()
 {
     return(_buffer.GetByte((uint)(_bufferPosition++)));
 }