private static void ConnectCallback(IAsyncResult ar) { // Signal the main thread to continue. _connectSignal.Set(); if (_stopSignal) { return; } TcpListener listener = (TcpListener)ar.AsyncState; TcpClient client = listener.EndAcceptTcpClient(ar); //client.SendTimeout = 5000; //client.ReceiveTimeout = 5000; string remoteIP = client.Client.RemoteEndPoint.ToString().Split(':')[0]; try { Utils.Log("Get report submitted from " + remoteIP); using (var socketStream = client.GetStream()) { ReportInfo report = ReportSendReceiver.ReceiveReport(socketStream); report.CallingIP = remoteIP; HandleReport(report, socketStream); } client.Close(); } catch (Exception ex) { Utils.Log("Exception when handle connection from {0}, error: {1}", remoteIP, ex.Message); Utils.Log(ex.StackTrace); } }
private static void ThreadFunc(object ctx) { while (true) { try { if (_stopSignal) { break; } //1. get new pdf file string pdfFile = QueryPdfFile(); if (string.IsNullOrEmpty(pdfFile)) { Thread.Sleep(2000); continue; } //2. start handle report ReportInfo report = new ReportInfo(); report.PdfReport = File.ReadAllBytes(pdfFile); report.Status = ReportStatus.SubmitInitial; Utils.Log("Start to handle new report, id:{0}, path:{1}", report.Id, pdfFile); bool reportDone = false; do { TcpClient client = new TcpClient(); //client.SendTimeout = 5000;//5s //client.ReceiveTimeout = 5000;//5s client.Connect(IPAddress.Parse(ServerIP), ServerPort);//it will throw an exception if failed to connect. using (NetworkStream socketStream = client.GetStream()) { Utils.Log("Send report with status: " + report.Status); ReportSendReceiver.SendReport(report, socketStream); report = ReportSendReceiver.ReceiveReport(socketStream); Utils.Log("Receive report with status: " + report.Status); if (report.IsServerDone() || report.HasError()) { reportDone = true; } else { _reportConfirmedSignal.Reset(); _reportToConfirm = report; if (ReportSendEvent != null) { ReportSendEvent(new ReportSendEventArg() { HasError = false, Report = report, NeedConfirm = report.NeedConfirm() }); } _reportConfirmedSignal.WaitOne(); _reportToConfirm = null; } } client.Close(); }while (!reportDone); //4. clear work if (ReportSendEvent != null) { ReportSendEvent(new ReportSendEventArg() { HasError = report.HasError(), Report = report, NeedConfirm = report.NeedConfirm() }); } Utils.Log("Report done with status: " + report.Status); if (report.HasError()) { //move to error report folder Utils.Log("Report has error, will send to errorbox. error: " + report.ErrorMessage); string targetFile = Path.Combine(PdfReportFolder, "Errorbox", Path.GetFileName(pdfFile)); File.Move(pdfFile, targetFile); } else { File.Delete(pdfFile); } } catch (Exception ex) { if (ReportSendEvent != null) { ReportSendEvent(new ReportSendEventArg() { HasError = true, ErrorMessage = ex.Message }); } Utils.Log("Exception happen from Report Client main thread, message: " + ex.Message); Utils.Log(ex.StackTrace); Thread.Sleep(1000); } } _isRunning = false; Utils.Log("Report Client stopped"); }