예제 #1
0
        static readonly object lockerDir  = new object(); //each thread server must access to an exclusive section one at time


        public void EntryPoint()
        {
            try  // start both udp and tcp
            {
                threadUDP = new Thread(EntryUDP);
                threadUDP.IsBackground = true;
                threadUDP.Start();

                threadTCP = new Thread(EntryTCP);
                threadTCP.IsBackground = true;
                threadTCP.SetApartmentState(ApartmentState.STA);
                threadTCP.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                LANSharingApp.LogFile(e.Message, e.ToString(), e.Source);
                // delegate the operation on form to the GUI
                MessageFormError mfe = new MessageFormError("UDP or TCP service not available");
                LANSharingApp.gui.Invoke((MethodInvoker) delegate() {
                    mfe.ShowDialog();
                });
                LANSharingApp.closing = true;
                LANSharingApp.serverThread.Join();
                LANSharingApp.clearTempFiles();
                LANSharingApp.flushTmpDirectory();
                Application.Exit();
            }
        }
예제 #2
0
        private void refresh_Click(object sender, EventArgs e)
        {
            //call the method to update the list of Online Users
            this.Timer_Tick_Tock(sender, e);
            MessageFormError mfe = new MessageFormError("Online users list has been refreshed");

            mfe.ShowDialog();
        }
        public static void UpdateUserPreference()
        {
            try
            {
                StreamWriter pref;

                if (File.Exists(Application.StartupPath + @"\\userPreferences.txt"))
                {
                    File.Delete(Application.StartupPath + @"\\userPreferences.txt");
                }

                pref = new StreamWriter(Application.StartupPath + @"\\userPreferences.txt");
                // Write to the file:
                pref.WriteLine("image_flag " + image_flag);
                pref.WriteLine("user_image_path " + user_image_path);
                pref.WriteLine("user_small_image_path " + user_small_image_path);
                pref.WriteLine("sys_Sound_Flag " + sysSoundFlag);
                pref.WriteLine("save_profile " + saveProfile);
                lock (lockerPathSave)
                {
                    pref.WriteLine("path_save " + pathSave);
                }

                // Close the stream:
                pref.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                LogFile(e.Message, e.ToString(), e.Source);


                if (LANSharingApp.sysSoundFlag == 1)
                {
                    audio_error.Play();
                }
                // delegate the operation on form to the GUI
                MessageFormError mfe = new MessageFormError("An exception has occurred in updating userPreference file");
                LANSharingApp.gui.Invoke((MethodInvoker) delegate() {
                    mfe.ShowDialog();
                });
                closing = true;
                LANSharingApp.serverThread.Join();
                clearTempFiles();
                flushTmpDirectory();
                Application.Exit();
            }
        }
예제 #4
0
        // start to notify wiht UDP packets the online presence
        public void EntryNotifyPresence()
        {
            try
            {
                while (!LANSharingApp.closing)
                {
                    //check ethernet, need this line to generate exception!!!
                    string localIP = LANSharingApp.umu.GetLocalIP();

                    //check if localIP == Admin.getIP
                    if (!localIP.Equals(LANSharingApp.umu.getAdmin().getIp().ToString()))
                    {
                        LANSharingApp.umu.getAdmin().setIP(localIP);
                        Console.WriteLine("Internet Network Changed: Restart Procedure.");
                        // delegate the operation on form to the GUI
                        LANSharingApp.gui.Invoke((MethodInvoker) delegate() {
                            Application.Restart();
                        });
                    }

                    // send broadcast every 2s, only if ONLINE
                    if (LANSharingApp.umu.getAdmin().getState().CompareTo("online") == 0)
                    {
                        sendBroadcastPacket(LANSharingApp.umu.getAdmin().getString());
                        //Console.WriteLine("UDP packet Admin: "+ LANSharingApp.umu.getAdmin().getString());
                    }

                    // need a timer
                    Thread.Sleep(2000);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                LANSharingApp.LogFile(e.Message, e.ToString(), e.Source);

                LANSharingApp.closing = true;
                // delegate the operation on form to the GUI
                MessageFormError mfe = new MessageFormError("Error: no lan connection detected, connect to a lan then restart application.");
                LANSharingApp.gui.Invoke((MethodInvoker) delegate() {
                    mfe.ShowDialog();
                });
                LANSharingApp.serverThread.Join();
                LANSharingApp.clearTempFiles();
                LANSharingApp.flushTmpDirectory();
                Application.Exit();
            }
        }
        public static string[] readUserPreference()
        {
            try
            {
                StreamReader sr;
                sr = new StreamReader(Application.StartupPath + @"\\userPreferences.txt");

                string[] s = new string[6];
                string   line;
                int      i = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    string[] pref = line.Split(' ');
                    s[i] = pref[1];
                    i++;
                }

                sr.Close();
                return(s);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                LogFile(e.Message, e.ToString(), e.Source);


                if (LANSharingApp.sysSoundFlag == 1)
                {
                    audio_error.Play();
                }
                // delegate the operation on form to the GUI
                MessageFormError mfe = new MessageFormError("An exception has occurred in reading userPreference file");
                LANSharingApp.gui.Invoke((MethodInvoker) delegate() {
                    mfe.ShowDialog();
                });
                closing = true;
                LANSharingApp.serverThread.Join();
                clearTempFiles();
                flushTmpDirectory();
                Application.Exit();
                return(null);
            }
        }
        static void Main(string[] args)
        {
            if (!Directory.Exists(tmpPath))
            {
                Directory.CreateDirectory(tmpPath);
            }

            //check if the app is already running
            running = Process.GetProcessesByName(System.IO.Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location)).Length > 1;
            //Console.WriteLine("ALready running? " + running);

            if (running)
            {
                string selected = null;

                if (args.Length == 1) //apertura da context menu
                {
                    selected = args[0];
                    lock (LANSharingApp.lockerPathSend)
                    {
                        pathSend = selected;
                    }
                }
                // open the memory-mapped
                MemoryMappedFile mmf = MemoryMappedFile.OpenExisting("myMappedFile");
                // declare accessor to write on file
                MemoryMappedViewAccessor accessor = mmf.CreateViewAccessor();
                if (selected != null)
                {
                    //write in the file: Length|Path
                    //4 offset cause length is on 32 bit
                    byte[] Buffer = ASCIIEncoding.ASCII.GetBytes(selected);
                    accessor.Write(0, Buffer.Length);
                    accessor.Flush();
                    accessor.WriteArray(4, Buffer, 0, Buffer.Length);
                    accessor.Flush();
                    //write path in the memory mapped file
                }
                else
                {
                    //write in the file: Lenght|Path
                    accessor.Write(0, 0);
                }

                //MessageBox.Show("Application already running in background!!!");
                closing = false;
            }
            else
            {
                if (!closing)// there are no process active
                {
                    // create a memory-mapped file of length 1000 bytes and give it a 'map name' of 'test'
                    MemoryMappedFile mmf = MemoryMappedFile.CreateNew("myMappedFile", 1000);

                    string selected = null;

                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);

                    if (args.Length == 1) //apertura da context menu
                    {
                        selected = args[0];
                        lock (lockerPathSend)
                        {
                            pathSend = selected;
                        }
                    }

                    //manage user preference

                    if (File.Exists(Application.StartupPath + @"\\userPreferences.txt"))

                    {
                        //userPref already exists --> load old user Pref
                        try
                        {
                            string[] s = new string[6];
                            s = readUserPreference();
                            if (s[0].Equals("0"))
                            {
                                //default user image is used
                                LANSharingApp.user_image            = global::LANSharing.Properties.Resources.user_80;
                                LANSharingApp.user_small_image      = global::LANSharing.Properties.Resources.user_32;
                                LANSharingApp.image_flag            = 0;
                                LANSharingApp.user_image_path       = "no";
                                LANSharingApp.user_small_image_path = "no";
                                Console.WriteLine("Default Image");
                            }
                            else
                            {
                                //LANSharingApp.user_image = Image.FromFile(s[1]);
                                using (var bmpTemp = new Bitmap(s[1]))
                                {
                                    LANSharingApp.user_image = new Bitmap(bmpTemp);
                                }
                                //LANSharingApp.user_small_image = Image.FromFile(s[2]);
                                using (var bmpTemp = new Bitmap(s[2]))
                                {
                                    LANSharingApp.user_small_image = new Bitmap(bmpTemp);
                                }
                                LANSharingApp.image_flag            = 1;
                                LANSharingApp.user_image_path       = s[1];
                                LANSharingApp.user_small_image_path = s[2];
                                //custom user image is used
                                Console.WriteLine("Custom Image");
                            }
                            if (s[3].Equals("1"))
                            {
                                //sound on
                                LANSharingApp.sysSoundFlag = 1;
                                Console.WriteLine("Sound on");
                            }
                            else
                            {
                                //sound off
                                LANSharingApp.sysSoundFlag = 0;
                                Console.WriteLine("Sound Off");
                            }

                            int savePref = int.Parse(s[4]);
                            saveProfile = savePref;
                            Console.WriteLine("Save pref: " + saveProfile);

                            lock (lockerPathSave)
                            {
                                LANSharingApp.pathSave = s[5];
                            }

                            Console.WriteLine("Path pref: " + pathSave);
                        }
                        catch (Exception e)
                        {
                            // Let the user know what went wrong.
                            MessageFormError mfe = new MessageFormError("It's not possible to resume userPreferences: default settings are used");
                            mfe.ShowDialog();
                            Console.WriteLine("The userPreference file could not be read");
                            Console.WriteLine(e.Message);
                            LogFile(e.Message, e.ToString(), e.Source);
                            //default user image is used
                            UpdateUserPreference();
                            LANSharingApp.user_image            = global::LANSharing.Properties.Resources.user_80;
                            LANSharingApp.user_small_image      = global::LANSharing.Properties.Resources.user_32;
                            LANSharingApp.image_flag            = 0;
                            LANSharingApp.user_image_path       = "no";
                            LANSharingApp.user_small_image_path = "no";
                        }
                    }
                    else
                    {
                        //write default preference
                        WriteUserPreference();
                    }

                    // Console.WriteLine("ALready running: opening " + pathSend);
                    // Application.EnableVisualStyles();
                    // Application.SetCompatibleTextRenderingDefault(false);

                    //create userManagerUnit
                    umu = new UserManagerUnit();

                    //create a ManualResetEvent used from Server's Threads
                    // true -> server start work  ||  false -> server stop work
                    mre = new ManualResetEvent(true);

                    // create Server istance
                    //  active server
                    server       = new Server();
                    serverThread = new Thread(server.EntryPoint)
                    {
                        Name = "serverThread"
                    };
                    serverThread.IsBackground = true;
                    serverThread.Start();

                    //create new client
                    client = new Client();

                    // create gui istance
                    gui = new GUI(selected);

                    //run application
                    Application.Run(gui);
                }
            }
        }