Esempio n. 1
0
        private void HandleClientComm(object param)
        {
            TcpClient     client       = (TcpClient)param;
            NetworkStream clientStream = client.GetStream();

            byte[] recv = new byte[BUFSIZE];
            string strRecv;
            int    bytesRead;

            while (true)
            {
                try {
                    bytesRead = clientStream.Read(recv, 0, BUFSIZE);
                } catch (Exception ex) {
                    m_log.TraceError("TCP client occur error: " + ex.Message);
                    m_mainForm.Invoke((EventHandler) delegate {
                        m_textBox.BackColor = Color.Red;
                        m_textBox.ForeColor = Color.White;
                        m_textBox.Text      = "TCP client error: " + ex.Message;
                    });
                    return;
                }
                if (bytesRead == 0)
                {
                    break;
                }
                m_log.TraceInfo(">>>>>>>> Start to handle client request. Ver: " + MainFileVersion.AssemblyVersion + " <<<<<<<<");
                strRecv = Encoding.UTF8.GetString(recv, 0, bytesRead);
                IPEndPoint remoteAddress = (IPEndPoint)client.Client.RemoteEndPoint;
                m_log.TraceInfo(string.Format("Received message[{0}], from {1}:{2}", strRecv, remoteAddress.Address, remoteAddress.Port));
                byte[] sendMessage;
                string strVIN = "";
                if (strRecv != null)
                {
                    strVIN = strRecv.Split(',')[0].Trim();
                }
                if (strVIN.Length == 17)
                {
                    sendMessage = Encoding.UTF8.GetBytes("200");
                    m_log.TraceInfo("Received VIN is OK");
                }
                else
                {
                    sendMessage = Encoding.UTF8.GetBytes("400");
                    m_log.TraceError("Received VIN is Illegal");
                    m_mainForm.Invoke((EventHandler) delegate {
                        m_textBox.BackColor = Color.Red;
                        m_textBox.ForeColor = Color.White;
                        m_textBox.Text      = "VIN号长度不为17位";
                    });
                }
                clientStream.Write(sendMessage, 0, sendMessage.Length);
                clientStream.Flush();
                if (strVIN.Length == 17)
                {
                    EmissionInfo ei     = new EmissionInfo();
                    string       errMsg = string.Empty;
                    bool         error  = false;
                    if (m_dynoParam.UseSimData)
                    {
                        ei = m_emiInfoSim;
                        ei.VehicleInfo1.VIN = strVIN;
                        ei.VehicleInfo2.VIN = strVIN;
                    }
                    else
                    {
                        try {
                            m_dbOracle.GetEmissionInfo(strVIN, ei, out errMsg);
                        } catch (Exception ex) {
                            error = true;
                            m_log.TraceError("GetEmissionInfo() error: " + ex.Message);
                            m_mainForm.Invoke((EventHandler) delegate {
                                m_textBox.BackColor = Color.Red;
                                m_textBox.ForeColor = Color.White;
                                m_textBox.Text      = "GetEmissionInfo() error: " + ex.Message;
                            });
                        }
                    }
                    if (!error)
                    {
                        if (errMsg.Length > 0 && errMsg != "null")
                        {
                            m_log.TraceError("USP_GET_ENVIRONMENT_DATA() return error: " + errMsg);
                            m_mainForm.Invoke((EventHandler) delegate {
                                m_textBox.BackColor = Color.Red;
                                m_textBox.ForeColor = Color.White;
                                m_textBox.Text      = errMsg;
                            });
                        }
                        else
                        {
                            m_mainForm.Invoke((EventHandler) delegate {
                                m_textBox.BackColor = Color.LightGreen;
                                m_textBox.ForeColor = Color.Black;
                                m_textBox.Text      = "VIN[" + strVIN + "]测功机参数匹配成功";
                            });
                        }
                    }
                    string JsonFormatted = JsonConvert.SerializeObject(ei, Newtonsoft.Json.Formatting.Indented);
                    m_log.TraceInfo("Send dyno information: " + Environment.NewLine + JsonFormatted);
                    string strSend = JsonConvert.SerializeObject(ei);
                    sendMessage = Encoding.UTF8.GetBytes(strSend);
                    clientStream.Write(sendMessage, 0, sendMessage.Length);
                    clientStream.Flush();
                }
            }
            clientStream.Close();
            client.Close();
        }