/// <summary> /// Handle on client connected /// </summary> /// <param name="result">AsyncResult</param> private void OnClientConnected(IAsyncResult result) { IpcPipe pipeInst = (IpcPipe)result.AsyncState; // Complete the client connection NamedPipeServerStream pipe = (NamedPipeServerStream)result.AsyncState; try { pipe.EndWaitForConnection(result); } catch (Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); pipeInst.m_options.m_callBackObj.OnNewConnection(this, IpcConnectStatus.FAIL_WAIT_FOR_CONNECTION_FAILED); return; } try { m_pipeHandle.BeginRead(m_readBuffer, 0, m_options.m_numOfReadBytes, OnReadComplete, this); m_connected = true; m_options.m_callBackObj.OnNewConnection(this, IpcConnectStatus.SUCCESS); } catch (System.Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); KillConnection(); m_options.m_callBackObj.OnNewConnection(this, IpcConnectStatus.FAIL_READ_FAILED); } }
/// <summary> /// Handle when read is completed /// </summary> /// <param name="result">AsyncResult</param> private void OnReadComplete(IAsyncResult result) { IpcPipe pipeInst = (IpcPipe)result.AsyncState; int readByte = 0; byte[] readBuffer = null; try { readByte = pipeInst.m_pipeHandle.EndRead(result); readBuffer = m_readBuffer.ToArray(); } catch (System.Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); pipeInst.KillConnection(); return; } try { pipeInst.m_pipeHandle.BeginRead(pipeInst.m_readBuffer, 0, pipeInst.m_options.m_numOfReadBytes, OnReadComplete, pipeInst); } catch (System.Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); pipeInst.KillConnection(); } pipeInst.m_options.m_callBackObj.OnReadComplete(pipeInst, readBuffer, readByte); }
/// <summary> ///Handles when Write is completed /// </summary> /// <param name="result">AsyncResult</param> private void OnWriteComplete(IAsyncResult result) { IpcPipe pipeInst = (IpcPipe)result.AsyncState; try { pipeInst.m_pipeHandle.EndWrite(result); } catch (System.Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); KillConnection(); pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.FAIL_WRITE_FAILED); return; } lock (pipeInst.m_writeQueue) { if (pipeInst.m_writeQueue.Count > 0) { PipeWriteElem elem = pipeInst.m_writeQueue.Dequeue(); if (pipeInst.m_writeQueue.Count() > 0) { PipeWriteElem nextElem = pipeInst.m_writeQueue.Dequeue(); try { m_pipeHandle.BeginWrite(nextElem.m_data, nextElem.m_offset, nextElem.m_dataSize, OnWriteComplete, this); } catch (System.Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.SUCCESS); KillConnection(); pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.FAIL_WRITE_FAILED); return; } } pipeInst.m_options.m_callBackObj.OnWriteComplete(pipeInst, IpcWriteStatus.SUCCESS); } } }
/// <summary> /// Actual server start function /// </summary> protected override void Execute() { IpcStartStatus status = IpcStartStatus.SUCCESS; try { IpcPipeOps pipeOptions = new IpcPipeOps(m_options.m_pipeName, this, m_options.m_numOfReadBytes, m_options.m_numOfWriteBytes); for (int trav = 0; trav < m_options.m_maximumInstances; trav++) { IpcPipe pipeInst = new IpcPipe(pipeOptions); pipeInst.Create(); } m_started = true; } catch (Exception ex) { Console.WriteLine(ex.Message + " >" + ex.StackTrace); m_started = false; status = IpcStartStatus.FAIL_PIPE_CREATE_FAILED; } m_options.m_callBackObj.OnServerStarted(this, status); }