public void Start(string pipeName, string id) { _pipe = new NamedPipeClientStream(pipeName); _pipe.Connect(); _pipe.BeginRead(_readBuffer, 0, _readBuffer.Length, OnRead, null); Write("hello", id); }
public NamedPipe(string pipeName, int maxPipes = 10) { NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut); byte[] buf = new byte[1024]; pipeClient.BeginRead(buf, 0, 1024, new AsyncCallback(test), null); // NamedPipeServerStream pipeServer = new NamedPipeServerStream(".", pipeName, PipeDirection.InOut, numThreads); }
// Use this for initialization void Start () { pipeClient = new NamedPipeClientStream( ".", "testpipe", PipeDirection.InOut); Debug.Log("Pipe Begin"); pipeClient.Connect(10000); Debug.Log("Pipe Done: " + pipeClient.IsConnected); // 启动接收 inBuffer = new byte[1024]; pipeClient.BeginRead(inBuffer, 0, pipeClient.InBufferSize, onSvrDataReceived, pipeClient); }
/// <summary> /// Create an IPC client and attempt to connect to server. /// </summary> /// <param name="serverName"> /// Name of server to connect to. /// </param> /// <param name="writeThrough"> /// If true writes will bypass system cache and go straight to the pipe. /// </param> public ClientInstance(string serverName, bool writeThrough) { myBuffer = new byte[myBuffSize]; myMemoryStream = new MemoryStream(myBuffSize); myMemoryStreamLock = new object(); myThreadRunning = true; myThread = new Thread(new ThreadStart(ProcessClientStream)); myThread.Start(); myNamedPipeClientStream = new NamedPipeClientStream( ".", serverName, PipeDirection.InOut, writeThrough ? PipeOptions.Asynchronous | PipeOptions.WriteThrough : PipeOptions.Asynchronous); myNamedPipeClientStreamLock = new object(); try { myNamedPipeClientStream.Connect(); } catch { return; } myNamedPipeClientStream.BeginRead(myBuffer, 0, myBuffer.Length, new AsyncCallback(OnClientReceive), null); }
private void QvxCommandWorker() { try { if (pipeName == null) return; object state = new object(); object connection = null; using (NamedPipeClientStream pipeClient = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut)) { var buf = new byte[4]; var buf2 = new byte[4]; Int32 count = 0; Int32 datalength = 0; pipeClient.Connect(1000); while (pipeClient.IsConnected) { try { #region Get QvxRequest var iar = pipeClient.BeginRead(buf, 0, 4, null, state); while (!iar.IsCompleted) Thread.Sleep(1); count = pipeClient.EndRead(iar); if (count != 4) throw new Exception("Invalid Count Length"); buf2[0] = buf[3]; buf2[1] = buf[2]; buf2[2] = buf[1]; buf2[3] = buf[0]; datalength = BitConverter.ToInt32(buf2, 0); var data = new byte[datalength]; count = pipeClient.Read(data, 0, datalength); if (count != datalength) throw new Exception("Invalid Data Length"); var sdata = ASCIIEncoding.ASCII.GetString(data); sdata = sdata.Replace("\0", ""); QvxRequest request; try { request = QvxRequest.Deserialize(sdata); } catch (Exception ex) { logger.Error(ex); throw ex; } request.QVWindow = QVWindow; request.Connection = connection; #endregion #region Handle QvxRequets QvxReply result = null; if (HandleQvxRequest != null) result = HandleQvxRequest(request); if (result == null) result = new QvxReply() { Result = QvxResult.QVX_UNKNOWN_ERROR }; #endregion #region Send QvxReply sdata = " " + result.Serialize() + "\0"; datalength = sdata.Length - 4; buf2 = ASCIIEncoding.ASCII.GetBytes(sdata); buf = BitConverter.GetBytes(datalength); buf2[0] = buf[3]; buf2[1] = buf[2]; buf2[2] = buf[1]; buf2[3] = buf[0]; pipeClient.Write(buf2, 0, buf2.Length); pipeClient.WaitForPipeDrain(); #endregion #region Handle result States if (result.Terminate) close = true; if (result.Connection != null) connection = result.Connection; if (result.SetConnectionNULL) connection = null; #endregion } catch (Exception ex) { logger.Error(ex); Thread.Sleep(500); close = true; } if (close) { close = false; pipeClient.Close(); } Thread.Sleep(5); } } running = false; } catch (Exception ex) { logger.Error(ex); } }