private void ConnectToFork(IAsyncResult result) { if (result.IsCompleted) { for (;;) { ForkStream = BluetoothForkClient.GetStream(); if (ForkStream.CanRead) { var myReadBuffer = new byte[1024]; var myCompleteMessage = ""; while (ForkStream.DataAvailable) { Thread.Sleep(1000); ForkStream.Read(myReadBuffer, 0, myReadBuffer.Length); myCompleteMessage += Encoding.ASCII.GetString(myReadBuffer).Replace("\0", ""); } if (!string.IsNullOrWhiteSpace(myCompleteMessage)) { Application.Current.Dispatcher.Invoke( new ThreadStart(() => ParseForkStringToObject(myCompleteMessage))); } } else { MessageBox.Show("Не удалось прочитать данные."); } } } }
public async Task <ForkedStreamBuffer> ReadAsync(ForkStream feed, byte[] buff, int buffOffset, int count) { //if the count is zero return if (count == 0) { return(this); } await this.EnsureInitializedAsync(); //if the source index isn't in the page throw an exception if (feed.Position < _basePos) { throw new ArgumentOutOfRangeException("feed.Position"); } //if the buffer offset is outside the bounds of buff throw if (buffOffset < 0 || buffOffset > buff.Length - 1) { throw new ArgumentOutOfRangeException("buffOffset"); } //if the count is less than zero or larger than the remaining buff if (count < 0 || count > buff.Length - buffOffset) { throw new ArgumentOutOfRangeException("count"); } var startIndex = Convert.ToInt32(feed.Position - _basePos); var cBytes = Math.Min(count, _bytesRead - startIndex); //read to the end of the requested section, or the end of the page if the section goes past the page Array.ConstrainedCopy(_pageData, startIndex, buff, buffOffset, cBytes); //adjust the position of the feed feed._position += cBytes; //adjust the buffer offset buffOffset += cBytes; //adjust the count count -= cBytes; if (feed.Position > EndOfPage) { this.MarkPageRead(); var retPage = await this.GetNextPageAsync(); if (retPage != null) { retPage = await retPage.ReadAsync(feed, buff, buffOffset, count); } return(retPage); } return(this); }
public static Stream[] Fork(this Stream stream, int count, long bufferSize = 0) { Stream[] streams = new Stream[count]; var buff = ForkedStreamBuffer.FromStream(stream, count, bufferSize); for (int i = 0; i < count; i++) { streams[i] = new ForkStream(buff); } return(streams); }