public void DoAcceptTcpClientCallback(IAsyncResult ar) { TcpListener listener = (TcpListener)ar.AsyncState; TcpClient client = listener.EndAcceptTcpClient(ar); client.ReceiveBufferSize = 1024 * 1024; NetworkStream stream = client.GetStream(); CMD_STREAM stCmd = new CMD_STREAM(); CMD_STREAM stRes = new CMD_STREAM(); //コマンド受信 if (cmdProc != null) { byte[] bHead = new byte[8]; if (stream.Read(bHead, 0, bHead.Length) == 8) { stCmd.uiParam = BitConverter.ToUInt32(bHead, 0); stCmd.uiSize = BitConverter.ToUInt32(bHead, 4); if (stCmd.uiSize > 0) { stCmd.bData = new Byte[stCmd.uiSize]; } int readSize = 0; while (readSize < stCmd.uiSize) { readSize += stream.Read(stCmd.bData, readSize, (int)stCmd.uiSize); } cmdProc.Invoke(cmdParam, stCmd, ref stRes); Array.Copy(BitConverter.GetBytes(stRes.uiParam), 0, bHead, 0, sizeof(uint)); Array.Copy(BitConverter.GetBytes(stRes.uiSize), 0, bHead, 4, sizeof(uint)); stream.Write(bHead, 0, 8); if (stRes.uiSize > 0) { stream.Write(stRes.bData, 0, (int)stRes.uiSize); } } } else { stRes.uiSize = 0; stRes.uiParam = 1; } stream.Dispose(); client.Client.Close(); server.BeginAcceptTcpClient(new AsyncCallback(DoAcceptTcpClientCallback), server); }
public void Run() { IntPtr hPipe = IntPtr.Zero; IntPtr hEventCmdWait = IntPtr.Zero; IntPtr hEventConnect = IntPtr.Zero; IntPtr[] hEventArray = new IntPtr[2]; NativeOverlapped stOver = new NativeOverlapped(); if (m_iCtrlCmdEventID != -1) { string strCmdEvent; strCmdEvent = CMD_CTRL_EVENT_WAIT + m_iCtrlCmdEventID.ToString(); hEventCmdWait = CommonUtil._CreateEvent(false, true, strCmdEvent); } hEventConnect = CommonUtil._CreateEvent(false, false, m_strEventName); hEventArray[0] = m_hStopEvent; hEventArray[1] = CommonUtil._CreateEvent(false, false, null); while (true) { if (hPipe == IntPtr.Zero) { while (true) { hPipe = CommonUtil._CreateNamedPipe(m_strPipeName, CommonUtil.PIPE_ACCESS_DUPLEX | CommonUtil.FILE_FLAG_WRITE_THROUGH | CommonUtil.FILE_FLAG_OVERLAPPED, CommonUtil.PIPE_TYPE_BYTE, 1, SEND_BUFF_SIZE, RES_BUFF_SIZE, PIPE_TIMEOUT); if (Marshal.GetLastWin32Error() == CommonUtil.ERROR_PIPE_BUSY) { Thread.Sleep(100); } else { if (hPipe == IntPtr.Zero) { hPipe = IntPtr.Zero; } break; } } stOver.EventHandle = hEventArray[1]; CommonUtil._ConnectNamedPipe(hPipe, ref stOver); CommonUtil._SetEvent(hEventConnect); } uint uiRes = CommonUtil._WaitForMultipleObjects(2, hEventArray, false, CommonUtil.INFINITE); if (uiRes == CommonUtil.WAIT_OBJECT_0) { //STOP break; } else if (uiRes == CommonUtil.WAIT_OBJECT_0 + 1) { //ほかのサーバーで処理中? if (hEventCmdWait != IntPtr.Zero) { CommonUtil._WaitForSingleObject(hEventCmdWait, CommonUtil.INFINITE); } //コマンド受信 if (m_pCmdProc != null) { CMD_STREAM stCmd = new CMD_STREAM(); CMD_STREAM stRes = new CMD_STREAM(); uint dwRead = 0; uint dwWrite = 0; do { byte[] bHead = new byte[8]; Array.Copy(BitConverter.GetBytes(stCmd.uiParam), 0, bHead, 0, sizeof(uint)); Array.Copy(BitConverter.GetBytes(stCmd.uiSize), 0, bHead, 4, sizeof(uint)); if (CommonUtil._ReadFile(hPipe, ref bHead, sizeof(uint) * 2, out dwRead, IntPtr.Zero) == false) { break; } stCmd.uiParam = BitConverter.ToUInt32(bHead, 0); stCmd.uiSize = BitConverter.ToUInt32(bHead, 4); if (stCmd.uiSize > 0) { stCmd.bData = new byte[stCmd.uiSize]; uint dwReadNum = 0; while (dwReadNum < stCmd.uiSize) { uint dwReadSize = 0; if (stCmd.uiSize - dwReadNum < SEND_BUFF_SIZE) { dwReadSize = stCmd.uiSize - dwReadNum; } else { dwReadSize = SEND_BUFF_SIZE; } byte[] bRead = new byte[dwReadSize]; if (CommonUtil._ReadFile(hPipe, ref bRead, dwReadSize, out dwRead, IntPtr.Zero) == false) { break; } Array.Copy(bRead, dwReadNum, stCmd.bData, 0, dwRead); dwReadNum += dwRead; } if (dwReadNum < stCmd.uiSize) { break; } } m_pCmdProc.Invoke(m_pParam, stCmd, ref stRes); if (stRes.uiParam == (uint)ErrCode.CMD_NO_RES) { break; } Array.Copy(BitConverter.GetBytes(stRes.uiParam), 0, bHead, 0, sizeof(uint)); Array.Copy(BitConverter.GetBytes(stRes.uiSize), 0, bHead, 4, sizeof(uint)); if (CommonUtil._WriteFile(hPipe, ref bHead, sizeof(uint) * 2, out dwWrite, IntPtr.Zero) == false) { break; } if (stRes.uiSize > 0) { if (stRes.bData == null) { break; } if (CommonUtil._WriteFile(hPipe, ref stRes.bData, stRes.uiSize, out dwWrite, IntPtr.Zero) == false) { break; } } } while (stRes.uiParam == (uint)ErrCode.CMD_NEXT); //Emun用の繰り返し } CommonUtil._FlushFileBuffers(hPipe); CommonUtil._DisconnectNamedPipe(hPipe); CommonUtil._CloseHandle(hPipe); hPipe = IntPtr.Zero; if (hEventCmdWait != IntPtr.Zero) { CommonUtil._SetEvent(hEventCmdWait); } } } if (hPipe != IntPtr.Zero) { CommonUtil._FlushFileBuffers(hPipe); CommonUtil._DisconnectNamedPipe(hPipe); CommonUtil._CloseHandle(hPipe); } CommonUtil._CloseHandle(hEventArray[1]); CommonUtil._CloseHandle(hEventConnect); if (hEventCmdWait != IntPtr.Zero) { CommonUtil._CloseHandle(hEventCmdWait); } }
public bool StartServer(string strEventName, string strPipeName, CMD_CALLBACK_PROC pfnCmdProc, object pParam) { if (pfnCmdProc == null || strEventName.Length == 0 || strPipeName.Length == 0) { return(false); } if (m_ServerThread != null) { return(false); } m_StopFlag = false; m_PulseEvent = new AutoResetEvent(false); m_ServerThread = new Thread(new ThreadStart(() => { using (EventWaitHandle eventConnect = new EventWaitHandle(false, EventResetMode.AutoReset, strEventName)) using (NamedPipeServerStream pipe = new NamedPipeServerStream( strPipeName.Substring(strPipeName.StartsWith("\\\\.\\pipe\\", StringComparison.OrdinalIgnoreCase) ? 9 : 0), PipeDirection.InOut, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous)) { while (m_StopFlag == false) { pipe.BeginWaitForConnection(asyncResult => { try { pipe.EndWaitForConnection(asyncResult); m_PulseEvent.Set(); } catch (ObjectDisposedException) { } }, null); eventConnect.Set(); m_PulseEvent.WaitOne(); if (pipe.IsConnected) { byte[] bHead = new byte[8]; if (pipe.Read(bHead, 0, 8) == 8) { CMD_STREAM stCmd = new CMD_STREAM(); stCmd.uiParam = BitConverter.ToUInt32(bHead, 0); stCmd.uiSize = BitConverter.ToUInt32(bHead, 4); stCmd.bData = stCmd.uiSize == 0 ? null : new byte[stCmd.uiSize]; if (stCmd.uiSize == 0 || pipe.Read(stCmd.bData, 0, stCmd.bData.Length) == stCmd.bData.Length) { CMD_STREAM stRes = new CMD_STREAM(); pfnCmdProc.Invoke(pParam, stCmd, ref stRes); if (stRes.uiParam == (uint)ErrCode.CMD_NEXT) { // Emun用の繰り返しは対応しない throw new InvalidOperationException(); } else if (stRes.uiParam != (uint)ErrCode.CMD_NO_RES) { BitConverter.GetBytes(stRes.uiParam).CopyTo(bHead, 0); BitConverter.GetBytes(stRes.uiSize).CopyTo(bHead, 4); pipe.Write(bHead, 0, 8); if (stRes.uiSize != 0 && stRes.bData != null && stRes.bData.Length >= stRes.uiSize) { pipe.Write(stRes.bData, 0, (int)stRes.uiSize); } } } } pipe.WaitForPipeDrain(); pipe.Disconnect(); } } } })); m_ServerThread.Start(); return(true); }