private byte[] readMessage(NamedPipeClientStream client) { byte[] message = new byte[0]; _offset = 0; _current = new List<byte>(); while (true) { var i = client.ReadByte(); if (i == -1) { _offset = 0; return new byte[0]; } if (i == 0) { var buffer = new byte[_offset]; Array.Copy(_bytes, buffer, _offset); _current.AddRange(buffer); message = _current.ToArray(); _current = new List<byte>(); _offset = 0; break; } _bytes[_offset] = Convert.ToByte(i); _offset++; if (_offset == _bytes.Length) { _current.AddRange(_bytes); _offset = 0; } } return message; }
public void Read_bytes_from_server() { new Thread(Server).Start(); using (var s = new NamedPipeClientStream("pipedream")) { s.Connect(); var read = 0; while (read <100) { read = s.ReadByte(); Console.WriteLine(read + " "); } } }
private static int PingPong_OtherProcess(string inName, string outName) { // Create pipes with the supplied names using (var inbound = new NamedPipeClientStream(".", inName, PipeDirection.In)) using (var outbound = new NamedPipeServerStream(outName, PipeDirection.Out)) { // Wait for the connections to be established Task.WaitAll(inbound.ConnectAsync(), outbound.WaitForConnectionAsync()); // Repeatedly read then write bytes from and to the other process for (int i = 0; i < 10; i++) { int b = inbound.ReadByte(); outbound.WriteByte((byte)b); } } return SuccessExitCode; }
public void PingPong() { // Create names for two pipes string outName = Guid.NewGuid().ToString("N"); string inName = Guid.NewGuid().ToString("N"); // Create the two named pipes, one for each direction, then create // another process with which to communicate using (var outbound = new NamedPipeServerStream(outName, PipeDirection.Out)) using (var inbound = new NamedPipeClientStream(".", inName, PipeDirection.In)) using (var remote = RemoteInvoke(PingPong_OtherProcess, outName, inName)) { // Wait for both pipes to be connected Task.WaitAll(outbound.WaitForConnectionAsync(), inbound.ConnectAsync()); // Repeatedly write then read a byte to and from the other process for (byte i = 0; i < 10; i++) { outbound.WriteByte(i); int received = inbound.ReadByte(); Assert.Equal(i, received); } } }
private static String ReadNextCommandMessage(NamedPipeClientStream pipe) { String xml = null; int data_len = 0; _logger.LogMsg(Level.Debug, "ReadNextCommandMessage(): Attempting to retrieve message from command pipe. "); try { // TO DO: make this more efficient. We also need to handle larger messages / dynamic buffer size. byte[] mybuf = new byte[2048]; byte[] len_buf = new byte[4]; for( int ii = 0; ii < mybuf.Length; ii++ ) { mybuf[ii] = 0xFF; } _logger.LogMsg(Level.Debug, "Checking for pipe stream status."); if (pipe.IsConnected) { _logger.LogMsg(Level.Debug, "Pipe is connected. Attempting binary read."); int itmp = 0; int pos = 0; while (itmp >= 0) { itmp = pipe.ReadByte(); if (itmp == -1) { _logger.LogMsg(Level.Debug, "End of read stream, -1 returned."); break; } mybuf[pos] = (Byte)itmp; if (pos > 3) { if (mybuf[pos] == 0x00) { break; } else { xml += (char)mybuf[pos]; } } else { len_buf[3 - pos] = (byte)itmp; } pos++; } data_len = BitConverter.ToInt32(len_buf, 0); _logger.LogMsg(Level.Debug, String.Format("Bytes read (+1) = {0}, QlkView data length = {1}.", (pos + 1).ToString(), data_len.ToString())); } else { _logger.LogMsg(Level.Warn, "Pipe is not connected."); } } catch (System.Exception ex) { Debug.WriteLine("Error in ReadNextCommandMessage(): " + ex.Message); throw ex; } return xml; }
private void PipeThread(object id) { try { using (var pipe = new NamedPipeClientStream(".", (string)id + "\\root", PipeDirection.In)) { pipe.Connect(); BinaryReader rd = new BinaryReader(pipe); for (;;) { lock (mSync) if (mShutdown) return; int op = pipe.ReadByte(); if (op < 0) return; ProcessPipeThreadEvent(rd, op); } } } catch (Exception ex) { lock (mSync) mError.Add(ex); } }
private static string ReadToEnd(NamedPipeClientStream pipe) { List<char> input = new List<char>(); char c = (char)pipe.ReadByte(); while (c != ProcessingKeys.EndMessage) { input.Add(c); c = (char)pipe.ReadByte(); } if (input.Count > 0) return new string(input.ToArray()); return null; }
private static byte[] ReadBytesToEnd(NamedPipeClientStream pipe) { List<byte> input = new List<byte>(); byte c = (byte)pipe.ReadByte(); while (c != ProcessingKeys.EndMessage) { input.Add(c); c = (byte)pipe.ReadByte(); } if (input.Count > 0) return input.ToArray(); return null; }
protected void ProcessDeleteSnapshot(NamedPipeClientStream pipe, CancellationToken token) { // Are we deleting a whole tree? char c = (char)pipe.ReadByte(); if (c == ProcessingKeys.DeleteSnapshot) { ProcessDeleteTree(pipe, token); return; } //Get name for snapshot (if any) string name = (c != ProcessingKeys.EndMessage ? String.Concat(c, ReadToEnd(pipe)) : null); ManagementObject VSMS = Utility.GetServiceObject("MSVM_VirtualSystemManagementService"); //ManagementClass VSMS = new ManagementClass(new ManagementScope(@"root\virtualization", null).Path.Path, "MSVM_VirtualSystemManagementService", null); ManagementObject Snapshot = Utility.GetSnapshot(VMName, name); if (Snapshot == null) return; ManagementBaseObject input = VSMS.GetMethodParameters("RemoveVirtualSystemSnapshot"); input["SnapshotSettingData"] = Snapshot.Path.Path; if (Utility.WaitForJob(VSMS.InvokeMethod("RemoveVirtualSystemSnapshot", input, null))) write("Deleted snapshot: " + VMName + "::" + (name ?? "current")); else write("Failed to delete snapshot: " + VMName + "::" + (name ?? "current"), EventLogEntryType.Error); }
protected void Listen(NamedPipeClientStream pipe, CancellationToken token) { byte current = (byte) pipe.ReadByte(); while (current != 255 && !token.IsCancellationRequested) { //ProcessMessage(current, pipe, token); if (token.IsCancellationRequested) return; switch (current) { case ProcessingKeys.BootVM: ProcessBootVM(pipe, token); break; case ProcessingKeys.NewSnapshot: ProcessNewSnapshot(pipe, token); break; case ProcessingKeys.OpenSnapshot: ProcessOpenSnapshot(pipe, token); break; case ProcessingKeys.DeleteSnapshot: ProcessDeleteSnapshot(pipe, token); break; case ProcessingKeys.WriteToLog: ProcessWriteToLog(pipe, token); break; case ProcessingKeys.ReadFromLog: ProcessReadFromLog(pipe, token); break; } current = (byte) pipe.ReadByte(); } if (current == 255) this.Dispose(); }
private Response GetResponse(Request request) { _pipeClient = CreateClientPipe(); TryConnect(_pipeClient); _pipeClient.WriteByte((byte)request); _pipeClient.WriteByte((byte)_pos); _pipeClient.WaitForPipeDrain(); Response response = (Response)_pipeClient.ReadByte(); return response; }