Example #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            //ListViewUpdateData(this.listView1, ClientInfoCommon.ReadClientList());
            ListViewUpdateData(this.listView1, ClientInfoCommon.ReadClientInfoList());
            List <MeterInfo1> tmpMeterInfo1List = new List <MeterInfo1>();

            ClientInfoCommon.ReadMeterList(ref tmpMeterInfo1List);
            ListView2UpdateData(this.listView2, tmpMeterInfo1List);
        }
        }       // End of ReplyClient()

        //2017-11-07 Write by AlexWang 身份识别
        public static void ParseIdentity(ref ClientInfo clientInfo, byte[] buffer, int nDataLen)
        {
            if (nDataLen < 11)
            {
                return;
            }
            bool bVerified = clientInfo.Verified;

            if (bVerified)
            {
            }
            else
            {
                clientInfo.Sim        = GetSimCard(buffer, 3);
                clientInfo.Verified   = true;
                clientInfo.NeedVerify = false;
                ClientInfoCommon.AddClientInfo(clientInfo);
            }

            Log.RecvLog(clientInfo.Sim, "recv", MyHelper.ByteArray2HexStr(buffer, nDataLen));

            byte[] sendData = { 0x10, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
                                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

            sendData[3] = buffer[3];
            sendData[4] = buffer[4];
            sendData[5] = buffer[5];
            sendData[6] = buffer[6];
            sendData[7] = buffer[7];
            sendData[8] = buffer[8];

            DateTime time  = DateTime.Now;
            int      nYear = time.Year;
            int      nMon  = time.Month;
            int      nDay  = time.Day;
            int      nHour = time.Hour;
            int      nMin  = time.Minute;
            int      nSec  = time.Second;

            sendData[9]  = (byte)(nYear & 0xFF);
            sendData[10] = (byte)((nYear >> 8) & 0xFF);
            sendData[11] = (byte)nMon;
            sendData[12] = (byte)nDay;
            sendData[13] = (byte)nHour;
            sendData[14] = (byte)nMin;
            sendData[15] = (byte)nSec;

            byte[] tx2 = MyHelper.CRC16_C(sendData, 16);
            sendData[16] = tx2[1];
            sendData[17] = tx2[0];

            int sndLen = clientInfo.Client.Send(sendData);

            string strSndMsg = MyHelper.ByteArray2HexStr(sendData, sndLen);

            Log.RecvLog(clientInfo.Sim, "ack identity ", strSndMsg.ToString());                                  // 写接收日志
        }
Example #3
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            labelClientCount.Text = ClientInfoCommon.ReadClientCount().ToString();

            //ListViewUpdateData(this.listView1, ClientInfoCommon.ReadClientList());
            //List<MeterInfo1> tmpMeterInfo1List = new List<MeterInfo1>();
            //ClientInfoCommon.ReadMeterList(ref tmpMeterInfo1List);
            //ListView2UpdateData(this.listView2, tmpMeterInfo1List);

            label4.Text = ClientInfoCommon.ReadMsgCount().ToString();           // 更新内部缓存数量
        }
Example #4
0
        public Form1()
        {
            ClientInfoCommon.InitInstance();


            InitializeComponent();
            ListViewInit();
            ListView2Init();
            this.label2.Text = DateTime.Now.ToString();

            System.Timers.Timer t = new System.Timers.Timer(1000);                        // 实例化Timer类,设置间隔时间为500毫秒;
            t.Elapsed  += new System.Timers.ElapsedEventHandler(ClientInfoCommon.theout); // 到达时间的时候执行事件;
            t.AutoReset = true;                                                           // 设置是执行一次(false)还是一直执行(true);
            t.Enabled   = true;                                                           // 是否执行System.Timers.Timer.Elapsed事件;
        }
        // 2018-09-11 解析接收到的数据报文
        public static int ParseRealDataProtocal(ref ClientInfo clientInfo, byte[] buffer, int nDataLen)
        {
            if (nDataLen <= 18)
            {
                return(0);
            }
            bool bVerified = clientInfo.Verified;

            if (bVerified)
            {
            }
            else
            {
                clientInfo.Sim        = GetSimCard(buffer, 3);
                clientInfo.Verified   = true;
                clientInfo.NeedVerify = false;
                ClientInfoCommon.AddClientInfo(clientInfo);
            }

            //Log.RecvLog(clientInfo.Sim, "recv", buffer, nDataLen);                                  // 写接收日志

            // 三相多功能表
            MeterInfo1 mMeterInfo1 = new MeterInfo1();

            mMeterInfo1.InsertTime = DateTime.Now.ToString();
            mMeterInfo1.MsgTime    = GetMsgTime(ref buffer, 9, 7);
            mMeterInfo1.GateId     = GetSimCard(buffer, 3);
            mMeterInfo1.MeterAddr  = GetMeterId(buffer);
            mMeterInfo1.RecvData   = MyHelper.ByteArray2HexStr(buffer, nDataLen);

            // ClientInfoCommon.AddMsg(mMeterInfo1);
            Log.RecvLog(clientInfo.Sim, "recv MsgTime:" + mMeterInfo1.MsgTime + " meterId:" + mMeterInfo1.MeterAddr, MyHelper.ByteArray2HexStr(buffer, nDataLen));
            ReplyClient(ref clientInfo, buffer, nDataLen);
            ClientInfoCommon.MsgUpdate(mMeterInfo1);        // 更新数据到内存缓存

            return(0);
        }       // End of ParseRealDataProtocal()
Example #6
0
        //停止服务
        private void StopServer()
        {
            m_tcpListener.Stop();

            ClientInfoCommon.RemoveAllClientInfo();
        }
Example #7
0
        //客户端处理服务
        private void ClientServer(object obj)
        {
            TcpClient tcpClient = (TcpClient)obj;

            tcpClient.ReceiveTimeout = 1000;
            bool bVerify = false;

            ClientInfo clientInfo = new ClientInfo(tcpClient);

            Byte[] recvPacket = new Byte[1024];
            int    nErr       = 0;
            int    nDataLen   = 0;

            while (true)
            {
                try
                {
                    Array.Clear(recvPacket, 0, 1024);
                    nDataLen = 0;
                    nErr     = RecvPacket(clientInfo.Client, recvPacket, ref nDataLen);

                    if (nDataLen > 0)
                    {
                        try
                        {
                        }
                        catch (Exception ex)
                        {
                            //Log.ErrLog(string.Format("解析报文出错, {0}", ex.Message));
                            Log.RecvLog("Error", "解析报文出错", ex.Message);
                        }
                    }
                    if (0 == nErr)
                    {
                        try
                        {
                            TcpClientProtocol.ParseData(ref clientInfo, recvPacket, nDataLen);
                        }
                        catch (Exception ex)
                        {
                            //Log.ErrLog(string.Format("解析报文出错, {0}", MyHelper.ByteArray2HexStr(recvPacket, nDataLen)));
                            //Log.ErrLog(ex.Message);
                            Log.RecvLog("Error", "解析报文出错", MyHelper.ByteArray2HexStr(recvPacket, nDataLen));
                            Log.RecvLog("Error", "Error", ex.Message);
                        }
                        clientInfo.RecvTime = DateTime.Now;
                    }
                    else if (1 == nErr)
                    {
                        clientInfo.CloseClient();
                        break;
                    }
                    else if (2 == nErr)
                    {
                    }
                    else if (3 == nErr)
                    {
                        // 超时处理
                        if (clientInfo.Verified)
                        {
                            if (clientInfo.RecvTime.AddMinutes(15) < DateTime.Now)
                            {
                                String str = string.Format("{0}时连接上来的{1}客户端已超时断开", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Client.RemoteEndPoint.ToString());
                                Log.RecvLog("Error", "Error", str);
                                clientInfo.CloseClient();
                                break;
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            if (clientInfo.VerifiedTimeout())
                            {
                                // 未经身份验证
                                if (bVerify)
                                {
                                    String str = string.Format("{0}时连接上来的{1}客户端失去心跳响应", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Sim);
                                    Log.RecvLog("Error", "Error", str);
                                }
                                else
                                {
                                    String str = string.Format("{0}时连接上来的{1}是非授权客户端", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Client.RemoteEndPoint.ToString());
                                    Log.RecvLog("Error", "Error", str);
                                }
                                clientInfo.CloseClient();
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    string strErr = ex.Message;
                    if (clientInfo != null)
                    {
                        if (clientInfo.Client != null)
                        {
                            String str = string.Format("{0}时连接上来的{1}客户端异常断开,{2}", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Client.RemoteEndPoint.ToString(), strErr);
                            Log.RecvLog("Error", "Error", str);
                        }
                        else
                        {
                            String str = string.Format("{0}时连接上来的{1}客户端异常断开,{2}", clientInfo.ConnectTime.ToString("yyyy-MM-dd HH:mm:ss"), clientInfo.Sim, strErr);
                            Log.RecvLog("Error", "Error", ex.Message);
                        }
                        clientInfo.CloseClient();
                    }
                    else
                    {
                    }
                    break;
                }
            }

            ClientInfoCommon.RemoveClientInfo(clientInfo);
        }