示例#1
0
        public void HandleClientComm(object client)
        {
            StringBuilder builder      = new StringBuilder();
            TcpClient     tcpClient    = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            byte[] message = new byte[4096];
            int    bytesRead;

            while (true)
            {
                bytesRead = 0;
                //builder = null;

                try
                {
                    do
                    {
                        bytesRead = clientStream.Read(message, 0, message.Length);
                        builder.Append(Encoding.UTF8.GetString(message, 0, bytesRead));
                        //Console.WriteLine("\n");
                    }while (clientStream.DataAvailable);
                }
                catch (SocketException e)
                {
                    //Console.WriteLine(e);
                    Console.WriteLine("Клиент отключился");
                    //a socket error has occured
                    break;
                }
                catch (IOException e)
                {
                    //Console.WriteLine(e);
                    Console.WriteLine("Клиент отключился{IOException}!");
                    //a socket error has occured
                    break;
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    Console.WriteLine(e.Message);
                    //a socket error has occured
                    break;
                }

                if (bytesRead == 0)
                {
                    //the client has disconnected from the server
                    break;
                }


                //message has successfully been received
                ASCIIEncoding encoder = new ASCIIEncoding();
                System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));

                string _message = builder.ToString();
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")}-{((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()}\n{new string('*', 50)}\n{_message}\n{new string('*', 50)}\n");
                Logger.WriteText($"{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")}-remoteIP:{((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()}\n{new string('*', 50)}\n");
                Logger.WriteText(_message);


                if (!string.IsNullOrEmpty(_message))
                {
                    LogicLayer.RemoveModelAll();
                    ParseMessage(_message);
                }
            }

            tcpClient.Close();
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="_message"></param>
        private void ParseMessage(string _message)
        {
            string BarCode = "n/a";

            File.AppendAllText(fileLogOriginMessage, $"{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")}\n{new string('*', 50)}\n{_message}\n{new string('*', 50)}\n");

            string[] stringSeparatorsLines = new string[] { "\r" /*, "\n"*/ };
            string[] lines = _message.Split(stringSeparatorsLines, StringSplitOptions.None);
            _count = 0;

            //if (!string.IsNullOrEmpty(message))
            //{
            foreach (string s in lines)
            {
                string[] stringSeparatorsRows = new string[] { "|", ":" /*, "+", "-"*/, " " };
                string[] rows = s.Split(stringSeparatorsRows, StringSplitOptions.RemoveEmptyEntries);
                _rows = 0;

                if (s.Contains("ID"))
                {
                    _count = 0;
                }

                //Console.WriteLine();

                if ((_count == 0) && (rows.Length > 2))
                {
                    if (!string.IsNullOrEmpty(rows[2]))
                    {
                        BarCode = rows[2].Trim(new char[] { '^', 'M', 'R' });
                        //LogicLayer.RemoveModelAll();
                    }
                    else
                    {
                        BarCode = "n/a";
                    }
                }
                else if ((_count == 0) && (rows.Length < 3))
                {
                    BarCode = "n/a";
                    //LogicLayer.RemoveModelAll();
                }

                if (rows.Length > 3)
                {
                    if (Goods.Contains(rows[1]))
                    {
                        //add items to model
                        LogicLayer.AddModel(new Model(code: BarCode, goods: rows[1], vidUnits: rows[2], value01: rows[3]));
                    }
                }
                else if ((rows.Length <= 3) && (rows.Length >= 2))
                {
                    if (GoodsFrom2.Contains(rows[1]))
                    {
                        LogicLayer.AddModel(new Model(code: BarCode, goods: rows[1], value01: rows[2]));
                    }
                }
                //}

                //lock (fileLock)
                //{
                //    using (StreamWriter myStream = new StreamWriter(fileLogParseMessage, true))
                //    {
                //        myStream.Write($"\n");
                //    }
                //}

                _count++;
            }

            foreach (Model spw in LogicLayer.GetModel)
            {
                //Console.ForegroundColor = spw.Code != "n/a" ? ConsoleColor.Green : ConsoleColor.Red;
                //Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")} - [BarCode] - {spw.Code}");
                //Console.ForegroundColor = ConsoleColor.White;

                Console.ForegroundColor = ConsoleColor.Cyan;
                Console.WriteLine($"{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")} {spw.Code} \t {spw.Goods} \t {spw.Value01}");
                File.AppendAllText(fileLogParseMessage, $"\n{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")} \t {spw.Code} \t {spw.Goods} \t {spw.Value01}");

                if (spw.Code != "n/a")
                {
                    string query = QueryGetG(spw)?.Query; // получить запрос
                    if (!string.IsNullOrEmpty(query) && query != "Query NULL")
                    {
                        Console.ForegroundColor = ConsoleColor.Yellow;
                        Console.WriteLine(query);
                        File.AppendAllText(fileLogQuerry, $"\n{new string('*', 12)}-START:[{spw.Code}]-{new string('*', 12)}\n");
                        File.AppendAllText(fileLogQuerry, $"\n[{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")}] \t {spw.Code} \t {spw.Goods} \t {spw.Value01}");
                        File.AppendAllText(fileLogQuerry, $"\n[{DateTime.Now.ToString("dd.MM.yyyy HH:mm:ss.fff")}]{new string('*', 12)}-[{spw.Code}]-{new string('*', 12)}");
                        File.AppendAllText(fileLogQuerry, $"\n{query}");
                        File.AppendAllText(fileLogQuerry, $"\n{new string('*', 12)}-END-{new string('*', 12)}\n");
                        //Console.ForegroundColor = ConsoleColor.White;

                        //if (!string.IsNullOrEmpty(query) && query != "Query NULL") UpdateRowBdThread(spw); //!!!
                    }
                    else
                    {
                        Console.WriteLine(query);
                    }
                }
            }
        }