public void Setup() { pipe = new Pipe(); testProtocol = new TestProtocol(); data = new byte[MessageSize]; data.AsSpan().Fill(1); }
public async Task ReadingAfterCompleteWorks() { var options = new PipeOptions(useSynchronizationContext: false); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); var protocol = new TestProtocol(data.Length); var reader = connection.CreateReader(); await connection.Application.Output.WriteAsync(data); await connection.Application.Output.CompleteAsync(); var result = await reader.ReadAsync(protocol); Assert.False(result.IsCompleted); Assert.Equal(data, result.Message); reader.Advance(); var resultTask = reader.ReadAsync(protocol); result = await resultTask; Assert.True(result.IsCompleted); reader.Advance(); result = await reader.ReadAsync(protocol); Assert.True(result.IsCompleted); reader.Advance(); }
private void Start() { if (state == NETWORK_STATE.LOCAL_HOST) { this.serverIP = "127.0.0.1"; } else { this.serverIP = "10.211.55.3"; } if (!Network.Instance.IsConnected) { Network.Instance.Initialize(); ProtocolManager.Instance.Initialize(); Network.Instance.Connect(this.serverIP, Defines.SERVER_PORT); } this.loginProtocol = transform.Find("Login Protocol").GetComponent <LoginProtocol>(); this.lobbyProtocol = transform.Find("Lobby Protocol").GetComponent <LobbyProtocol>(); this.roomProtocol = transform.Find("Room Protocol").GetComponent <RoomProtocol>(); this.testProtocol = transform.Find("Test Protocol").GetComponent <TestProtocol>(); this.gameProtocol = transform.Find("Game Protocol").GetComponent <GameProtocol>(); this.loginProtocol.InitializeProtocol(); this.lobbyProtocol.InitializeProtocol(); this.roomProtocol.InitializeProtocol(); this.testProtocol.InitializeProtocol(); this.gameProtocol.InitializeProtocol(); //Network.Instance.Connect(this.serverIP, Defines.SERVER_PORT); }
public async Task ReadMessagesWorks() { var options = new PipeOptions(useSynchronizationContext: false); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); for (int i = 0; i < 3; i++) { await connection.Application.Output.WriteAsync(data); } connection.Application.Output.Complete(); var protocol = new TestProtocol(data.Length); var reader = connection.CreateReader(); var count = 0; while (true) { var result = await reader.ReadAsync(protocol); if (result.IsCompleted) { break; } count++; Assert.Equal(data, result.Message); reader.Advance(); } Assert.Equal(3, count); }
public async Task ConsumePartialBufferWorks() { var protocol = new TestProtocol(); var pipe = new Pipe(); var reader = new MessagePipeReader(PipeReader.Create(pipe.Reader.AsStream()), protocol); pipe.Writer.WriteEmpty(protocol, 10); await pipe.Writer.FlushAsync(); var readResult = await reader.ReadAsync(); Assert.Equal(10, readResult.Buffer.Length); reader.AdvanceTo(readResult.Buffer.GetPosition(4), readResult.Buffer.End); pipe.Writer.WriteEmpty(protocol, 2); await pipe.Writer.FlushAsync(); readResult = await reader.ReadAsync(); // 6 bytes left over plus 2 newly written bytes Assert.Equal(8, readResult.Buffer.Length); reader.AdvanceTo(readResult.Buffer.End); reader.Complete(); pipe.Writer.Complete(); pipe.Reader.Complete(); }
public async Task FullMessageThenPartialMessage() { var options = new PipeOptions(useSynchronizationContext: false); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); var protocol = new TestProtocol(data.Length); var reader = connection.CreateReader(); var resultTask = reader.ReadAsync(protocol); connection.Application.Output.Write(data); connection.Application.Output.Write(data.AsSpan(0, 5)); await connection.Application.Output.FlushAsync(); var result = await resultTask; Assert.Equal(data, result.Message); reader.Advance(); resultTask = reader.ReadAsync(protocol); await connection.Application.Output.WriteAsync(data.AsMemory(5)); result = await resultTask; Assert.Equal(data, result.Message); }
public async Task ReadAfterCancellationTokenFiresWorks() { var options = new PipeOptions(useSynchronizationContext: false); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); var protocol = new TestProtocol(data.Length); var reader = connection.CreateReader(); await connection.Application.Output.WriteAsync(data); var result = await reader.ReadAsync(protocol); Assert.Equal(data.Length, result.Message.Length); reader.Advance(); var cts = new CancellationTokenSource(); cts.Cancel(); await Assert.ThrowsAsync <OperationCanceledException>(async() => await reader.ReadAsync(protocol, cts.Token)); await connection.Application.Output.WriteAsync(data); result = await reader.ReadAsync(protocol); Assert.Equal(data.Length, result.Message.Length); reader.Advance(); }
public async Task BufferingDataPastEndOfStreamCanBeReadAgain() { var protocol = new TestProtocol(); var stream = new ThrowAfterZeroByteReadStream(); var writer = PipeWriter.Create(stream); var reader = new MessagePipeReader(PipeReader.Create(stream), protocol); protocol.WriteMessage(Encoding.ASCII.GetBytes("Hello World"), writer); await writer.FlushAsync().ConfigureAwait(false); stream.Position = 0; var readResult = await reader.ReadAsync(); var buffer = readResult.Buffer; reader.AdvanceTo(buffer.Start, buffer.End); // Make sure IsCompleted is true readResult = await reader.ReadAsync(); buffer = readResult.Buffer; reader.AdvanceTo(buffer.Start, buffer.End); Assert.True(readResult.IsCompleted); var value = await ReadFromPipeAsString(reader); Assert.Equal("Hello World", value); reader.Complete(); }
public async Task CanReadMultipleTimes() { // This needs to run inline to synchronize the reader and writer TaskCompletionSource <object> waitForRead = null; var protocol = new TestProtocol(); async Task DoAsyncRead(PipeReader reader, int[] bufferSizes) { var index = 0; while (true) { var readResult = await reader.ReadAsync().ConfigureAwait(false); if (readResult.IsCompleted) { break; } Assert.Equal(bufferSizes[index], readResult.Buffer.Length); reader.AdvanceTo(readResult.Buffer.End); index++; waitForRead?.TrySetResult(null); } reader.Complete(); } async Task DoAsyncWrites(PipeWriter writer, int[] bufferSizes) { for (var i = 0; i < bufferSizes.Length; i++) { writer.WriteEmpty(protocol, bufferSizes[i]); waitForRead = new TaskCompletionSource <object>(); await writer.FlushAsync().ConfigureAwait(false); await waitForRead.Task; } writer.Complete(); } // We're using the pipe here as a way to pump bytes into the reader asynchronously var pipe = new Pipe(); var options = new StreamPipeReaderOptions(bufferSize: 4096); var reader = new MessagePipeReader(PipeReader.Create(pipe.Reader.AsStream(), options), protocol); var writes = new[] { 4096, 1024, 123, 4096, 100 }; var readingTask = DoAsyncRead(reader, writes); var writingTask = DoAsyncWrites(pipe.Writer, writes); await readingTask; await writingTask; pipe.Reader.Complete(); }
public async Task ReadMessagesAsynchronouslyWorks() { var options = new PipeOptions(useSynchronizationContext: false, readerScheduler: PipeScheduler.Inline, writerScheduler: PipeScheduler.Inline); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); var protocol = new TestProtocol(); async Task WritingTask() { var writer = connection.Application.Output; for (var i = 0; i < 3; i++) { protocol.WriteMessage(data, writer); await writer.FlushAsync().ConfigureAwait(false); } await writer.CompleteAsync().ConfigureAwait(false); } async Task ReadingTask() { var reader = connection.CreatePipeReader(protocol); while (true) { var result = await reader.ReadAsync().ConfigureAwait(false); var buffer = result.Buffer; if (buffer.Length < 3 * data.Length) { reader.AdvanceTo(buffer.Start, buffer.End); continue; } Assert.Equal(Enumerable.Repeat(data, 3).SelectMany(a => a).ToArray(), buffer.ToArray()); reader.AdvanceTo(buffer.End); result = await reader.ReadAsync().ConfigureAwait(false); Assert.True(result.IsCompleted); break; } await reader.CompleteAsync(); } var readingTask = ReadingTask(); var writingTask = WritingTask(); await writingTask; await readingTask; }
public async Task MessageBiggerThanMaxMessageSizeThrows() { var options = new PipeOptions(useSynchronizationContext: false); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); var protocol = new TestProtocol(data.Length); var reader = connection.CreateReader(); var resultTask = reader.ReadAsync(protocol, maximumMessageSize: 5); await connection.Application.Output.WriteAsync(data); await Assert.ThrowsAsync <InvalidDataException>(async() => await resultTask); }
public async Task ReadingWithoutCallingAdvanceThrows() { var options = new PipeOptions(useSynchronizationContext: false); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); var protocol = new TestProtocol(data.Length); var reader = connection.CreateReader(); await connection.Application.Output.WriteAsync(data); var result = await reader.ReadAsync(protocol); Assert.Equal(data, result.Message); await Assert.ThrowsAsync <InvalidOperationException>(async() => await reader.ReadAsync(protocol)); }
public async Task InvalidCursorThrows() { var protocol = new TestProtocol(); var pipe = new Pipe(); pipe.Writer.WriteEmpty(protocol, 10); await pipe.Writer.FlushAsync(); var readResult = await pipe.Reader.ReadAsync(); var buffer = readResult.Buffer; var reader = new MessagePipeReader(PipeReader.Create(Stream.Null), new TestProtocol()); Assert.Throws <ArgumentOutOfRangeException>(() => reader.AdvanceTo(buffer.Start, buffer.End)); pipe.Reader.Complete(); pipe.Writer.Complete(); reader.Complete(); }
private static MessagePipeReader CreateReader(out Func <byte[], Task> writeFunc) { var protocol = new TestProtocol(); var stream = new MemoryStream(); var writer = PipeWriter.Create(stream); var reader = new MessagePipeReader(PipeReader.Create(stream), protocol); long written = 0; writeFunc = async bytes => { var position = stream.Position; stream.Position = written; protocol.WriteMessage(bytes, writer); await writer.FlushAsync().ConfigureAwait(false); written = stream.Position; stream.Position = position; }; return(reader); }
public async Task PartialMessageWorks(int?maxMessageSize) { var options = new PipeOptions(useSynchronizationContext: false); var pair = DuplexPipe.CreateConnectionPair(options, options); await using var connection = new DefaultConnectionContext(Guid.NewGuid().ToString(), pair.Transport, pair.Application); var data = Encoding.UTF8.GetBytes("Hello World"); var protocol = new TestProtocol(data.Length); var reader = connection.CreateReader(); var resultTask = reader.ReadAsync(protocol, maximumMessageSize: maxMessageSize); // Write byte by byte for (int i = 0; i < data.Length; i++) { await connection.Application.Output.WriteAsync(data.AsMemory(i, 1)); } var result = await resultTask; Assert.Equal(data, result.Message); reader.Advance(); }
public async Task CanUseMultipleMessageReadersOnSameUnderlyingReader() { var protocol = new TestProtocol(); var pipe = new Pipe(); var underlyingReader = PipeReader.Create(pipe.Reader.AsStream()); var reader = new MessagePipeReader(underlyingReader, protocol); protocol.WriteMessage(Encoding.ASCII.GetBytes("Hello"), pipe.Writer); protocol.WriteMessage(Encoding.ASCII.GetBytes("World"), pipe.Writer); await pipe.Writer.FlushAsync(); var readResult = await reader.ReadAsync(); Assert.Equal("Hello", Encoding.ASCII.GetString(readResult.Buffer.ToArray())); reader.Complete(); reader = new MessagePipeReader(underlyingReader, protocol); readResult = await reader.ReadAsync(); Assert.Equal("World", Encoding.ASCII.GetString(readResult.Buffer.ToArray())); reader.Complete(); }
public async Task DoesNotReturnCompletedIfUnderlyingReaderIsCompletedIfThereAreMoreMessagesToParse() { var protocol = new TestProtocol(); var bufferWriter = new ArrayBufferWriter <byte>(); protocol.WriteMessage(new byte[100], bufferWriter); protocol.WriteMessage(new byte[100], bufferWriter); var reader = new MessagePipeReader( new CompletingPipeReader(new ReadOnlySequence <byte>(bufferWriter.WrittenMemory)), protocol); var readResult = await reader.ReadAsync(); var buffer = readResult.Buffer; Assert.Equal(100, buffer.Length); Assert.False(readResult.IsCompleted); reader.AdvanceTo(buffer.Start); readResult = await reader.ReadAsync(); buffer = readResult.Buffer; Assert.Equal(200, buffer.Length); Assert.False(readResult.IsCompleted); reader.AdvanceTo(buffer.Start); readResult = await reader.ReadAsync(); buffer = readResult.Buffer; Assert.Equal(200, buffer.Length); Assert.True(readResult.IsCompleted); reader.Complete(); }
public static void Main(string[] args) { rocketCreation = DefaultMakeRocket; List <CLIAction> argParseActions = ArgParser.GetActions(args, out string[] argWarnings, out string[] argErrors); Logging.Print(argErrors, Logging.PrintType.ERROR); Logging.Print(argWarnings, Logging.PrintType.WARNING); bool writebackSTL = false; CLIAction writeBackAct = new CLIAction(); bool writebackExteriorSTL = false; CLIAction writeBackExtAct = new CLIAction(); bool InhibitSTLAnalysis = false; if (argErrors.Count() == 0) { foreach (CLIAction argParseAction in argParseActions) { // Do actions switch (argParseAction.act) { case ActionType.PrintCSVs: SetupCSV(); break; case ActionType.TimeScale: Time.FLIGHT_RESOLUTION = (float)argParseAction.actionValue; break; case ActionType.LoadStl: STLExtractor primarySTL = new STLExtractor((string)argParseAction.actionValue, STLInfo.STLUnits.Centimeters, InhibitSTLAnalysis); if (primarySTL.IsValid) { simRocket = primarySTL.RocketFromSTL(); } break; case ActionType.ArgDebug: PrintArgs(argParseActions); Logging.Print(""); break; case ActionType.Test: string testParams = (string)argParseAction.actionValue; Logging.Print("TEST PROTOCOL INITIATED."); Logging.Print("PARAMETERS: " + testParams); TestProtocol.RunTests(testParams); Logging.Print("TEST CONCLUDED.\n"); break; case ActionType.Writeback: writebackSTL = true; writeBackAct = argParseAction; break; case ActionType.WritebackExterior: writebackExteriorSTL = true; writeBackExtAct = argParseAction; break; case ActionType.InhibitFullAnalysis: InhibitSTLAnalysis = true; break; default: break; } } // STL File writeback if (writebackSTL) { string filename = "Untitled"; if (writeBackAct.actionValue != null && !string.IsNullOrEmpty((string)writeBackAct.actionValue)) { filename = (string)writeBackAct.actionValue; } STLInserter.CreateSTLFile(filename, simRocket.Surfaces, STLInfo.STLType.Binary); } // STL File Exterior writeback if (writebackExteriorSTL) { string filename = "Untitled"; if (writeBackAct.actionValue != null) { filename = (string)writeBackAct.actionValue; } STLInserter.CreateSTLFile(filename, simRocket.ExteriorSurfaces, STLInfo.STLType.Binary); } Logging.Print("SIMULATION BEGINS."); Run(); } Logging.Print("EXITING."); Logging.Close(); }