public void StreamDataExchange_OneWayStream() { bool success = false; bool completed = false; string message = GenerateMessage(); var stream = new MemoryStream(); var reading = new Task(async delegate() { try { byte[] data = null; while (data == null) { data = await StreamHandler.StreamReaderAsync(stream); Thread.Sleep(5); } string receivedMessage = BinaryHandler.FromByteArray <string>(data); success = receivedMessage.Equals(message); completed = true; } catch (Exception ex) { Assert.Fail(ex.Message); return; } }); var writing = new Task(async delegate() { await StreamHandler.StreamWriterAsync( stream, StreamChanelMode.Oneway, BinaryHandler.ToByteArray(message)); }); reading.Start(); writing.Start(); while (!completed) { Thread.Sleep(5); } stream.Dispose(); Assert.IsTrue(success); }
/// <summary> /// Asynchronous reading formated data from stream. /// </summary> /// <typeparam name="T">Type of data after binary decoding.</typeparam> /// <param name="stream">Target stream.</param> /// <returns>Readed binary data.</returns> public static async Task <T> StreamReaderAsync <T>(PipeStream stream) { // Receiving header. byte[] dataSizeBufer = new byte[4]; await stream.ReadAsync(dataSizeBufer, 0, 4); // Receive data. int dataSize = BitConverter.ToInt32(dataSizeBufer, 0); byte[] data = new byte[dataSize]; await stream.ReadAsync(data, 0, dataSize); return(BinaryHandler.FromByteArray <T>(data)); }
public void Deserialize() { byte[] binary; BlobType blob; try { binary = BinaryHandler.ToByteArray(new BlobType() { s = "DeserTest" }); blob = BinaryHandler.FromByteArray <BlobType>(binary); } catch (Exception ex) { Assert.Fail(ex.Message); return; } Assert.IsTrue(blob != null && blob.s.Equals("DeserTest")); }