示例#1
0
        } // End constructor

        public void StartReceive()
        {
            try
            {
                using (socket)
                {
                    formMain.SetStatusText("Status: Running");
                    formMain.ChangeButtonState(true);
                    while (true)
                    {
                        byte[] data = new byte[1024];

                        if (socket.Available > 0)
                        {
                            IPEndPoint tempasd = new IPEndPoint(IPAddress.Any, 0);
                            data = socket.Receive(ref tempasd);
                            if (data.GetLength(0) == 4) // Is this get status response
                            {
                                MessageProcesser.ProcessStatusResponse(data);
                            }
                            else // For now it is only received Alarm
                            {
                                string     xmlData = System.Text.Encoding.Default.GetString(data);
                                senderInfo si      = new senderInfo();

                                // Assign default value
                                si.errorCode = "none";

                                si = MessageProcesserXML.ProcessAlarmResponseXML(xmlData);
                                if (si.error)
                                {
                                    continue; // Something went wrong, abort
                                }

                                si.trueIPAddress = HashtableAndDatabaseClass.FetchIPFromMac(si.mac);
                                if (si.trueIPAddress == null)
                                {
                                    continue; // Hashtable doesn't countain that MAC, abort everything (writing in log and display too)
                                }
                                si.locationID = HashtableAndDatabaseClass.FetchLocationIDFromMac(si.mac);
                                // we know that we have that key, so dont need to check

                                if (!formMain.GetDebugStatus())
                                {   // If debug checkbox is not checked try to write to database
                                    try
                                    {
                                        HashtableAndDatabaseClass.InsertMessageInDatabase(si, Storage.ReadConnectionString());
                                        formMain.SetDBStatusText("DB Status: OK");
                                    }
                                    catch (Exception)
                                    {
                                        //WriteOnDisplayAndStorage(si);
                                        WriteOnDisplayAndStorageDBDown(si);
                                        formMain.SetDBStatusText("DB Status: DB is down"); // Database is down, update status
                                        continue;
                                    }
                                }
                                // Write log and to display
                                WriteOnDisplayAndStorage(si); // Will use information stored on si structure


                                // Form responce
                                byte[] finalMessage = Encoding.ASCII.GetBytes("ACK " + si.seq);

                                si.trueIPAddress = si.trueIPAddress.Trim(); // Some people use whitespaces infront of IP  :(
                                try
                                {
                                    sender = new IPEndPoint(IPAddress.Parse(si.trueIPAddress), defaultUDPSendingPort);
                                    socket.Send(finalMessage, finalMessage.Length, sender);
                                }
                                catch
                                {
                                    Console.WriteLine("IP from database is corrupted \n");
                                    FormCustomConsole.AddText("IP from database is corrupted" + Environment.NewLine);
                                }
                            } // End else from get data length
                        }     // End  if (socket.Available > 0)

                        if (Kill == true)
                        {
                            if (socket.Available > 0)
                            {
                                Console.WriteLine("More data needs to be read first \n");
                                continue;
                            }
                            Kill = false;
                            Console.WriteLine("Phase Exit \n");
                            FormCustomConsole.AddText("Phase Exit" + Environment.NewLine);
                            formMain.SetStatusText("Status: Stopped");
                            isActive = false;
                            return;
                        }
                        Thread.Sleep(10);
                    } // While
                }     // End using(socket)
            }         // End try
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                Console.WriteLine("Something went wrong with server \n");
                FormCustomConsole.AddText("Something went wrong with server" + Environment.NewLine);
                formMain.SetStatusText("Status: Error");
                isActive = false;
            }
        } // StartReceive