コード例 #1
0
ファイル: TcpIpPort.cs プロジェクト: 6555355/Scan_yan1
        public void ReceiveDataThread()
        {
            LogWriter.WriteLog(new string[] { string.Format("ReceiveDataThread begin") }, true);
            byte [] chRequest = new byte[BUFSIZE];
            //启动发送打印数据线程
            cachePrinter.Run();

            try
            {
                bool bFileHeader = true;
                int  sendBytes   = 0;
                // The thread's parameter is a handle to a pipe instance.
                while (true)
                {
                    // Read client requests from the pipe.
                    int bytesRec = m_hAcceptSocket.Receive(chRequest);
                    //LogWriter.WriteLog(new string[] { string.Format("Receive Size:{0}", bytesRec) }, true);
                    if (bytesRec > 0)
                    {
                        MyStruct data = new MyStruct();
                        data.buflen = bytesRec;
                        data.buf    = new byte[CachePrinter.BUFSIZE];
                        Buffer.BlockCopy(chRequest, 0, data.buf, 0, bytesRec);
                        cachePrinter.AddPrintData(data);
                    }
                    else if (bytesRec == 0)
                    {
                        cachePrinter.ReceiveDataFinishedFlag(true);
                        break;
                    }
                }
                cachePrinter.WaitSendPrintDataFinish();
                // Flush the pipe to allow the client to read the pipe's contents
                // before disconnecting. Then disconnect the pipe, and close the
                // handle to this pipe instance.
                //FlushFileBuffers(hPipe);
                m_hAcceptSocket.Shutdown(SocketShutdown.Both);
                m_hAcceptSocket.Close();
            }
            catch (Exception)
            {
                m_hAcceptSocket.Shutdown(SocketShutdown.Both);
                m_hAcceptSocket.Close();
            }
        }
コード例 #2
0
        public void ReceiveDataThread()
        {
            LogWriter.WriteLog(new string[] { "[Test]ReceiveData begin" }, true);

            string strRipFileName = "RipPrintData.prt";

            //SystemCall.isPmPrint = false;

            byte[]          chRequest           = new byte[CachePrinter.BUFSIZE];
            int             cbBytesRead         = 0;
            bool            fSuccess            = false;
            bool            isSetWhiteFormPrt   = false;
            bool            isUseJobModeSetting = false;
            SPrinterSetting oldPrinterSetting   = m_IPrinterChange.GetAllParam().PrinterSetting;

            //UVData oldUVData = new UVData();
            //oldUVData.Load();
            //启动发送打印数据线程
            cachePrinter.Run();

            Mutex mutex     = new Mutex(true, MUTEX_PORT_OPEN);
            int   sendBytes = 0;

            if (isRipPrintData)
            {
                File.Delete(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strRipFileName));
            }

            // The thread's parameter is a handle to a pipe instance.
            while (true)
            {
                // Read client requests from the pipe.
                if (m_hPipeHandle != 0)
                {
                    cbBytesRead = 0;
                    fSuccess    = CNamedPipe.ReadFile(
                        m_hPipeHandle,                     // handle to pipe
                        chRequest,                         // buffer to receive data
                        CachePrinter.BUFSIZE,              // size of buffer
                        ref cbBytesRead,                   // number of bytes read
                        0);                                // not overlapped I/O
                }
                if (fSuccess)
                {
                    MyStruct data = new MyStruct();
                    data.buflen = cbBytesRead;
                    data.buf    = new byte[CachePrinter.BUFSIZE];
                    Buffer.BlockCopy(chRequest, 0, data.buf, 0, cbBytesRead);
                    cachePrinter.AddPrintData(data);
                }
                if (!fSuccess || cbBytesRead == 0)
                {
                    cachePrinter.ReceiveDataFinishedFlag(true);
                    break;
                }
            }
            cachePrinter.WaitSendPrintDataFinish();

            if (isSetWhiteFormPrt || isUseJobModeSetting)
            {
                m_IPrinterChange.GetAllParam().PrinterSetting = oldPrinterSetting;
                CoreInterface.SetPrinterSetting(ref m_IPrinterChange.GetAllParam().PrinterSetting);
                LogWriter.WriteLog(new string[] { "[RIP]SetOldPrinterSetting" }, true);
            }

            if (isUseJobModeSetting)
            {
                //oldUVData.Save();
            }


            LogWriter.WriteLog(new string[] { "[RIP]Printer close" }, true);
            mutex.Close();


            // Flush the pipe to allow the client to read the pipe's contents
            // before disconnecting. Then disconnect the pipe, and close the
            // handle to this pipe instance.
            //FlushFileBuffers(hPipe);
            CNamedPipe.DisconnectNamedPipe(m_hPipeHandle);
            ///CloseHandle(hPipe);
            fConnected = false;
        }