public void Start() { Console.WriteLine("Магазин создан. Pipe: {0}.", PipeName); while (true) { pipeServer = CreatePipeServer(); pipeServer.WaitForConnection(); try { int userId = pipeServer.ReadByte(); var command = (Request)pipeServer.ReadByte(); if (userId == 0) { id += 1; pipeServer.WriteByte((byte)id); userId = id; } else { pipeServer.WriteByte((byte)userId); } switch (command) { case Request.GoSection: HandleGoSection(userId); break; case Request.LeftSection: HandleLeftSection(userId); break; case Request.IsVendorVacant: HandleIsVendorVacant(userId); break; case Request.NewVendor: HandleNewVendor(userId); break; case Request.NewBuyer: HandleNewBuyer(userId); break; default: Console.WriteLine("Неизвестная команда"); break; } pipeServer.WaitForPipeDrain(); } catch (Exception e) { Console.WriteLine(e); } } }
public async Task NamedPipeWriteViaAsyncFileStream(bool asyncWrites) { string name = Guid.NewGuid().ToString("N"); using (var server = new NamedPipeServerStream(name, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous)) { Task serverTask = Task.Run(async () => { await server.WaitForConnectionAsync(); for (int i = 0; i < 6; i++) Assert.Equal(i, server.ReadByte()); }); WaitNamedPipeW(@"\\.\pipe\" + name, -1); using (SafeFileHandle clientHandle = CreateFileW(@"\\.\pipe\" + name, GENERIC_WRITE, FileShare.None, IntPtr.Zero, FileMode.Open, (int)PipeOptions.Asynchronous, IntPtr.Zero)) using (var client = new FileStream(clientHandle, FileAccess.Write, bufferSize: 3, isAsync: true)) { var data = new[] { new byte[] { 0, 1 }, new byte[] { 2, 3 }, new byte[] { 4, 5 } }; foreach (byte[] arr in data) { if (asyncWrites) await client.WriteAsync(arr, 0, arr.Length); else client.Write(arr, 0, arr.Length); } } await serverTask; } }
static void Main(string[] args) { Console.Title = "Log - Trinix"; if (!Directory.Exists("Logs")) Directory.CreateDirectory("Logs"); Parser parser = null; while (true) { NamedPipeServerStream pipeServer = new NamedPipeServerStream("trinix"); pipeServer.WaitForConnection(); if (parser != null) parser.Dispose(); // MemoryStream ms = new MemoryStream(); //parser = new Parser(); // parser.Parse(ms); StreamWriter log = new StreamWriter("Logs\\" + String.Format("{0:yyyy-MM-dd-HH-mm-ss}", DateTime.Now) + ".log"); try { while (pipeServer.IsConnected) { int v = pipeServer.ReadByte(); if (v == -1) break; // ms.WriteByte((byte)v); Console.Write((char)v); log.Write((char)v); } pipeServer.Close(); log.Close(); } catch { } Console.Write(Environment.NewLine); Console.WriteLine("----------- Connection ended -----------"); Console.Write(Environment.NewLine); } }
public void Start() { Console.WriteLine("Обед философов начался ! Всего {0} столовых приборов !", _forksCount); bool hasStarted = false; while (_connectedPhilosopherCount > 0 || !hasStarted) { _pipeServer = CreateServerPipe(); _pipeServer.WaitForConnection(); var request = (Request)_pipeServer.ReadByte(); switch(request) { case Request.GetPhilosopherPosition: AssignPhilosopherPosition(); hasStarted = true; break; case Request.Eat: Eat(); break; case Request.Think: Think(); break; case Request.Rest: Rest(); break; case Request.TakeFork: TakeFork(); break; case Request.PutFork: PutDownFork(); break; default: break; } _pipeServer.WaitForPipeDrain(); } Console.WriteLine("Все ушли !"); Console.ReadLine(); }
private static string ReadFileName(NamedPipeServerStream server) { char ch; var fileName = ""; while ((ch = (char)server.ReadByte()) != 0) fileName += ch; return fileName; }