예제 #1
0
        /// <summary>
        /// Thread method that does the communication to the client phone. This 
        /// thread tries to receive from phone and forward to proxy
        /// </summary>
        private void SocketListenerThreadPhone()
        {
            int size = 0;
            Byte[] byteBuffer = new Byte[1024 * 100];
            Byte[] byBuffer = new Byte[1024];
            FileStream fout = null;
            int iCounter = 0;
            int iFileLen = 0;
            int iEmailDataLen = 0;
            String sEmailData = "";
            String sFolder = "";
            String sFileBmp = "";
            String sFileJpg = "";
            String sFileSrc = "";
            String sFileDest = "";
            String sLang = "";
            String sName = "";
            String sSrc = "";
            String sDst = "";
            int iStato = -1;
            int iLenRes = 0;
            DataProc d = null;
            System.Text.Encoding enc;

            m_lastReceiveDateTime = DateTime.Now;
            m_currentReceiveDateTime = DateTime.Now;
            _t = new System.Threading.Timer(new TimerCallback(CheckTimeOut), null, 60000, 60000);

            while (!m_stopClient)
            {
                try
                {
                    size = m_clientSocket.Receive(byteBuffer, (int)0, (int)1024, SocketFlags.None);
                    if (size > 0)
                    {
                        m_currentReceiveDateTime = DateTime.Now;
                        switch (iStato)
                        {
                            case -1:
                                if (size == 1)
                                {
                                    iCounter = 0;
                                    if (byteBuffer[0] == 0)
                                        iStato = 0;
                                    else
                                        iStato = 5;
                                    m_clientSocket.Send(byteBuffer, 1, SocketFlags.None);
                                }
                                else 
                                {   // check if a ping request
                                    if (size == 4 && byteBuffer[0] == 'p' && byteBuffer[1] == 'i' && byteBuffer[2] == 'n' && byteBuffer[3] == 'g')
                                        m_clientSocket.Send(byteBuffer, size, SocketFlags.None);
                                    else
                                    {
                                        Program.MainForm.AddLog(String.Format("BAD PROTOCOL.STATE {0}, SIZE{1}", iStato, size.ToString()));
                                        m_stopClient = true;
                                        m_markedForDeletion = true;
                                    }
                                }
                                break;
                            case 0: // receive header settings
                                byteBuffer[size] = 0;
                                enc = System.Text.Encoding.ASCII;
                                String sHeader = enc.GetString(byteBuffer);
                                sHeader = sHeader.Substring(0, size);
                                string[] sInfo = sHeader.Split('|');
                                sLang = sInfo[7] + "|" + sInfo[8];

                                sFolder = Program.MainForm.GetNewFolder(ref sName);
                                sFileBmp = sFolder + "\\0.bmp";
                                sFileJpg = sFolder + "\\0.jpg";
                                sFileSrc = sFolder + "\\source.dat";
                                sFileDest = sFolder + "\\dest.dat";
                                fout = new FileStream(sFileJpg, FileMode.Create);
                                Program.MainForm.AddLog("************ NEW PROCESS *****************");
                                Program.MainForm.AddLog("ID:" + sName);
                                Program.MainForm.AddLog("Lang:" + sLang);

                                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                                byBuffer = encoding.GetBytes(sName);
                                m_clientSocket.Send(byBuffer, sName.Length, SocketFlags.None);
                                iStato++;
                                
                                break;
                            case 1: // receive size image
                                if (size == 4)
                                {
                                    iFileLen = byteBuffer[3];
                                    iFileLen <<= 8;
                                    iFileLen |= byteBuffer[2];
                                    iFileLen <<= 8;
                                    iFileLen |= byteBuffer[1];
                                    iFileLen <<= 8;
                                    iFileLen |= byteBuffer[0];
                                    m_clientSocket.Send(byteBuffer, 4, 0);
                                    Program.MainForm.AddLog("Size jpg:" + iFileLen.ToString());
                                    iStato++;
                                }
                                else
                                {
                                    Program.MainForm.AddLog(String.Format("BAD PROTOCOL.STATE {0}, SIZE{1}", iStato, size.ToString()));
                                    m_stopClient = true;
                                    m_markedForDeletion = true;
                                }
                                break;
                            case 2: // raw image
                                //Program.MainForm.AddLog("Received:" + iCounter.ToString());
                                iCounter += size;
                                fout.Write(byteBuffer, 0, size);
                                if (iCounter == iFileLen && !m_stopClient)
                                {
                                    Program.MainForm.AddLog("File completed");
                                    byteBuffer[0] = 1;
                                    m_clientSocket.Send(byteBuffer, 1, 0);
                                    fout.Close();
                                    Program.MainForm.NewImage(sFileJpg, sFolder);
                                    Image img = Image.FromFile(sFileJpg);
                                    img.Save(sFileBmp, ImageFormat.Bmp);

                                    LeadWorker ocrWorker = new LeadWorker(Program.MainForm._ocrEngine, Program.MainForm._codecs);
                                    sSrc = ocrWorker.Start(sFolder, sFileBmp, getLangFrom(sLang));
                                    sDst="";
                                    Byte byTranslator = 0;
                                    try
                                    {
                                        if (sSrc != "")
                                        {
                                            TranslatedTranslator tr = new TranslatedTranslator();
                                            sDst = tr.Translate(sSrc, new TranslateDir(getLangFrom(sLang), getLangTo(sLang)));
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                        byTranslator = 1;
                                        Program.MainForm.AddLog(String.Format("Translated translation failed tray with google, Exception:{0}", ex.Message));
                                        GoogleTranslator gt = new GoogleTranslator();
                                        try
                                        {
                                            sDst = gt.Translate(sSrc, new TranslateDir(getLangFrom(sLang), getLangTo(sLang)));
                                        }
                                        catch (Exception ex2)
                                        {
                                            Program.MainForm.AddLog(String.Format("Google translation failed Exception:{0}", ex2.Message));
                                        }
                                    }
                                    
                                    iLenRes = Encoding.UTF8.GetBytes(sSrc).Length;
                                    byteBuffer[4] = (byte)(iLenRes >> 24);
                                    byteBuffer[3] = (byte)((iLenRes >> 16) & 0xFF);
                                    byteBuffer[2] = (byte)((iLenRes >> 8) & 0xFF);
                                    byteBuffer[1] = (byte)(iLenRes & 0xFF);
                                    byteBuffer[0] = byTranslator;
                                    m_clientSocket.Send(byteBuffer, 5, 0);
                                    if (iLenRes > 0)
                                    {
                                        TextWriter tw = new StreamWriter(sFileSrc, true, System.Text.Encoding.UTF8);
                                        tw.WriteLine(sSrc);
                                        tw.Close();

                                        byBuffer = Encoding.UTF8.GetBytes(sSrc);
                                        m_clientSocket.Send(byBuffer, iLenRes, 0);
                                        Program.MainForm.AddLog("SENT SOURCE:" + sSrc);
                                    }
                                    Program.MainForm.AddLog(String.Format("Len result ocr source:{0}", iLenRes));
                                    iStato++;
                                }
                                else if (iCounter > iFileLen && !m_stopClient)
                                {
                                    Program.MainForm.AddLog(String.Format("BAD PROTOCOL.STATE {0}, RAW DATA IMAGE TOO LARGE ", iStato));
                                    m_stopClient = true;
                                    m_markedForDeletion = true;
                                }

                                break;
                            case 3: // result upload 1   
                                if (size == 1)
                                {
                                    if (byteBuffer[0] != 1)
                                    {
                                        Program.MainForm.AddLog(String.Format("BAD ANSWER FROM CLIENT. STATE: {0}", iStato));
                                        m_stopClient = true;
                                        m_markedForDeletion = true;
                                    }
                                    else
                                    {   // go ahead with dest lang string
                                        //iLenRes = sResultLangDest.Length;
                                        iLenRes = Encoding.UTF8.GetBytes(sDst).Length;
                                        byteBuffer[3] = (byte)(iLenRes >> 24);
                                        byteBuffer[2] = (byte)((iLenRes >> 16) & 0xFF);
                                        byteBuffer[1] = (byte)((iLenRes >> 8) & 0xFF);
                                        byteBuffer[0] = (byte)(iLenRes & 0xFF);
                                        m_clientSocket.Send(byteBuffer, 4, 0);
                                        if (iLenRes > 0)
                                        {
                                            //System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                                            //byBuffer = encoding.GetBytes(sResultLangDest);
                                            TextWriter tw = new StreamWriter(sFileDest, true, System.Text.Encoding.UTF8);
                                            tw.WriteLine(sDst);
                                            tw.Close();

                                            byBuffer = Encoding.UTF8.GetBytes(sDst);
                                            m_clientSocket.Send(byBuffer, iLenRes, 0);
                                            Program.MainForm.AddLog("SENT DEST:" + sDst);
                                        }
                                        Program.MainForm.AddLog(String.Format("Len result ocr dest:{0}", iLenRes));
                                        iStato++;
                                    }
                                }
                                else
                                {
                                    Program.MainForm.AddLog(String.Format("BAD PROTOCOL.STATE {0}, SIZE{1}", iStato, size.ToString()));
                                    m_stopClient = true;
                                    m_markedForDeletion = true;
                                }
                                break;
                            case 4: // result upload 2  
                                Program.MainForm.AddLog("HANSHAKE PROTOCOL COMPLETED*************");
                                iStato = -1;
                                break;

                            case 5:
                                if (size == 2)
                                {
                                    iEmailDataLen = byteBuffer[1];
                                    iEmailDataLen <<= 8;
                                    iEmailDataLen |= byteBuffer[0];
                                    m_clientSocket.Send(byteBuffer, 2, 0);
                                    iStato++;
                                }
                                else
                                {
                                    Program.MainForm.AddLog(String.Format("BAD PROTOCOL.STATE {0}, SIZE{1}", iStato, size.ToString()));
                                    m_stopClient = true;
                                    m_markedForDeletion = true;
                                }
                                break;

                            case 6:
                                byteBuffer[iEmailDataLen] = 0;
                                enc = System.Text.Encoding.ASCII;
                                sEmailData = enc.GetString(byteBuffer);
                                sEmailData = sEmailData.Substring(0, iEmailDataLen);
                                String sFolderData = sEmailData.Substring(0, 10);
                                String sFile = Application.StartupPath + "\\" + sFolderData + "\\sending.txt";
                                bool exists = System.IO.Directory.Exists(Application.StartupPath + "\\" + sFolderData);
                                if (File.Exists(sFile))
                                    File.Delete(sFile);
                                Program.MainForm.AddLog(String.Format("Request email {0} folder {1}", sEmailData, sFolderData));
                                if (exists)
                                {
                                    byteBuffer[0] = 1;
                                    TextWriter tw = new StreamWriter(sFile);
                                    tw.WriteLine(sEmailData);
                                    tw.Close();
                                }
                                else
                                {
                                    Program.MainForm.AddLog(String.Format("Request email failed folder not found {0}", sFolderData));
                                    byteBuffer[0] = 0;
                                    m_clientSocket.Send(byteBuffer, 1, 0);
                                    Thread.Sleep(2000);
                                    m_stopClient = true;
                                    m_markedForDeletion = true;
                                }
                                iStato = -1;
                                m_clientSocket.Send(byteBuffer, 1, 0);
                                break;
                        }
                    }
                    else
                    {
                        m_stopClient = true;
                        m_markedForDeletion = true;
                    }
                }
                catch (Exception Ex)
                {
                    Program.MainForm.AddLog(String.Format("EXCEPTION state{0}:{1}", iStato, Ex.Message));
                    Program.MainForm.AddLog("************ END PROCESS *****************");
                    m_stopClient = true;
                    m_markedForDeletion = true;
                }
            }
            if (d != null)
                d.setCanDelete();
            _t.Dispose();
        }
예제 #2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                bool bCanClear = true;
                // loop for all request pending
                foreach (Object obj in _arrDataProc)
                {
                    timer1.Enabled = false;
                    DataProc d = (DataProc)obj;
                    if (d!=null && !d.getProcessCompleted()) // already processed ?
                    {
                        LeadWorker ocrWorker = new LeadWorker(_ocrEngine, _codecs);
                        bCanClear = false;
                        this.Cursor = Cursors.WaitCursor;

                        ResetControls();
                        //pictureBox1.Image = Image.FromFile(d.getFileBmp());
                        pictureBox1.Image = Image.FromFile(d.getFileJpg());
                        String sText = "";
                        txtSrc.Text = "";
                        txtDest.Text = "";
                        
                        sText = ocrWorker.Start(d.getFolder(), d.getFolder() + "\\0.jpg", d.getLangFrom());
                        if (sText.Length > 1)
                        {
                            sText = sText.ToLower();
                            sText = sText.Substring(0, 1).ToUpper() + sText.Substring(1);
                        }
                        String sTran = "";
                        TranslatedTranslator tr = new TranslatedTranslator();
                        d.setTranslator(0xff);
                        try
                        {
                            sTran = tr.Translate(sText, new TranslateDir(d.getLangFrom(), d.getLangTo()));
                            d.setTranslator(0);
                        }
                        catch (Exception ex1)
                        {
                            AddLog(String.Format("Translated translation failed tray with google, Exception:{0}", ex1.Message));
                            GoogleTranslator gt = new GoogleTranslator();
                            try
                            {
                                sTran = gt.Translate(sText, new TranslateDir(d.getLangFrom(), d.getLangTo()));
                                d.setTranslator(1);
                            }
                            catch (Exception ex2)
                            {
                                AddLog(String.Format("Google translation failed Exception:{0}", ex2.Message));
                            }
                        }
                        d.setProcessCompleted(sText,sTran);
                        
                        
                        Application.DoEvents();

                        AddLog("ALL PROCESS COMPLETED *************");
                        Application.DoEvents();
                    }
                    else if (d.getCanDelete())
                    {
                        d = null;
                    }
                }
                if (bCanClear)
                    _arrDataProc.Clear();
                timer1.Enabled = true;
            }
            catch (Exception ex)
            {   
                AddLog("Exception:"+ex.Message);
            }
            this.Cursor = Cursors.Default;
            timer1.Enabled = true;
        }
예제 #3
0
 private void butGetTextTranslated_Click_1(object sender, EventArgs e)
 {
     String sTran = "";
     String sSource = txtSrc.Text;
     if (radTrans.Checked)
     {
         TranslatedTranslator tr = new TranslatedTranslator();
         try
         {
             sTran = tr.Translate(sSource, new TranslateDir(txtLanSrc.Text, txLantDest.Text));
         }
         catch (Exception ex)
         {
             AddLog(String.Format("Exception:{0}", ex.Message));
         }
     }
     else
     {
         GoogleTranslator tr = new GoogleTranslator();
         try
         {
             sTran = tr.Translate(sSource, new TranslateDir(txtLanSrc.Text, txLantDest.Text));
         }
         catch (Exception ex)
         {
             AddLog(String.Format("Exception:{0}", ex.Message));
         }
     }
     txtDest.Text = sTran;
 }
예제 #4
0
 private void butLoadImage_Click(object sender, EventArgs e)
 {
     OpenFileDialog fileOpen = new OpenFileDialog();
     fileOpen.Filter = "Jpg Files (*.jpg)|*.jpg|Bmp files (*.bmp)|*.bmp";
     fileOpen.FilterIndex = 0;
     fileOpen.RestoreDirectory = false; //true;
     if (fileOpen.ShowDialog() == DialogResult.OK)
     {
         String sFolder = Application.StartupPath + "\\tmp";
         System.IO.Directory.CreateDirectory(sFolder);
         NewImage(fileOpen.FileName, sFolder);
         LeadWorker ocrWorker = new LeadWorker(_ocrEngine, _codecs);
         String sSrc = ocrWorker.Start(sFolder, fileOpen.FileName, lblOcrFrom.Text);
         AddLog("OCR:" + sSrc);
         String sDst = "";
         try
         {   if (sSrc != "")
             {   TranslatedTranslator tr = new TranslatedTranslator();
                 sDst = tr.Translate(sSrc, new TranslateDir(lblOcrFrom.Text, lblOcrTo.Text));
             }
         }
         catch (Exception ex)
         {
             Program.MainForm.AddLog(String.Format("Translated translation failed tray with google, Exception:{0}", ex.Message));
             GoogleTranslator gt = new GoogleTranslator();
             try
             {
                 sDst = gt.Translate(sSrc, new TranslateDir(lblOcrFrom.Text, lblOcrTo.Text));
             }
             catch (Exception ex2)
             {
                 Program.MainForm.AddLog(String.Format("Google translation failed Exception:{0}", ex2.Message));
             }
         }
         AddLog("Translated:" + sDst);
     }
 }