async protected Task StartAsync(string Command, params string[] Arguments) { await Task.Run(() => { var StartInfo = new ProcessStartInfo(Command, String.Join(" ", Arguments)); ; StartInfo.CreateNoWindow = true; StartInfo.UseShellExecute = false; StartInfo.RedirectStandardError = true; StartInfo.RedirectStandardInput = true; StartInfo.RedirectStandardOutput = true; this.SystemProcess = SystemProcess.Start(StartInfo); this.SystemProcess.EnableRaisingEvents = true; this.Out = new NodeBufferedStream(this.SystemProcess.StandardOutput.BaseStream); this.Error = new NodeBufferedStream(this.SystemProcess.StandardError.BaseStream); this.SystemProcess.Exited += SystemProcess_Exited; }); }
async public Task TestReadLineAsync() { var MemoryStream = new MemoryStream(); var StreamWriter = new StreamWriter(MemoryStream) { AutoFlush = true }; StreamWriter.Write("a\r\n"); StreamWriter.Write("\r\n"); StreamWriter.Write("bb\r\n"); StreamWriter.Write("ccc\r\n"); StreamWriter.Write("dddd\r\n"); MemoryStream.Position = 0; var Stream = new NodeBufferedStream(MemoryStream, 3); Assert.AreEqual("a", await Stream.ReadLineAsync(Encoding.UTF8)); Assert.AreEqual("", await Stream.ReadLineAsync(Encoding.UTF8)); Assert.AreEqual("bb", await Stream.ReadLineAsync(Encoding.UTF8)); Assert.AreEqual("ccc", await Stream.ReadLineAsync(Encoding.UTF8)); Assert.AreEqual("dddd", await Stream.ReadLineAsync(Encoding.UTF8)); }
async public Task ReadLineAsyncLatencyIssuesTest() { ThreadPool.SetMaxThreads(1, 1); var MemoryStream = new ProducerConsumerStream(new ProducerConsumer<byte>()); var StreamWriter = new StreamWriter(MemoryStream) { AutoFlush = true }; StreamWriter.Write("line1\r"); var Task2 = Task.Run(async () => { await Task.Delay(2); StreamWriter.Write("\n"); StreamWriter.Write("line2\r\n"); }); var Stream = new NodeBufferedStream(MemoryStream, 3); Assert.AreEqual("line1", await Stream.ReadLineAsync(Encoding.UTF8)); Assert.AreEqual("line2", await Stream.ReadLineAsync(Encoding.UTF8)); await Task2; }
static async public Task<WebSocketPacket> ReadPacketFromStreamAsync(int Version, NodeBufferedStream Stream) { var Packet = default(WebSocketPacket); if (Version <= 0) { var PayloadBegin = await Stream.ReadBytesAsync(1); if (PayloadBegin[0] != 0x00) throw(new Exception("Invalid Packet")); Packet.Opcode = OpcodeEnum.TextFrame; Packet.Payload = await Stream.ReadBytesUntilByteAsync(0xFF); } else { var Header = new byte[2]; var Size = new byte[8]; var SizeSize = 0; var Mask = new byte[4]; var Data = new MemoryStream(); var Temp = new byte[128]; WebSocketPacket.OpcodeEnum Opcode; bool IsFinal; bool IsMasked; int PayloadLength; do { await Stream.ReadAsync(Header, 0, 2); IsFinal = (((Header[0] >> 7) & 0x1) != 0); Opcode = (WebSocketPacket.OpcodeEnum)((Header[0] >> 0) & 0x7); PayloadLength = (Header[1] >> 0) & 0x7F; IsMasked = ((Header[1] >> 7) & 0x1) != 0; if (Opcode != OpcodeEnum.ContinuationFrame) { Packet.Opcode = Opcode; } // EXTENDED PayloadLength if (PayloadLength >= 0x7E) { if (PayloadLength == 0x7E) { SizeSize = 2; } else if (PayloadLength == 0x7F) { SizeSize = 8; } await Stream.ReadAsync(Size, 0, SizeSize); PayloadLength = 0; for (int n = 0; n < SizeSize; n++) { PayloadLength <<= 8; PayloadLength |= Size[n]; } } // MASK if (IsMasked) { await Stream.ReadAsync(Mask, 0, 4); } // Read Payload await Stream.ReadAsync(Temp, 0, PayloadLength); // Perform unmasking if (IsMasked) { for (int n = 0; n < PayloadLength; n++) Temp[n] ^= Mask[n % 4]; } Data.Write(Temp, 0, PayloadLength); } while (!IsFinal); Packet.Payload = Data.ToArray(); } return Packet; }