Пример #1
0
        static void userLoggedOn()
        {
            string user_name = GetUserName();

            if (currentUserName == user_name)
            {
                return;
            }
            stop_userLoggedOn_t();
            currentUserName = user_name;
            userLoggedOn_t  = ThreadRoutines.StartTry(
                () =>
            {
                try
                {
                    //if (SysTray.This.IsOnlyTCP)
                    //{
                    //    Log.Main.Warning("TEST MODE: IsOnlyTCP");
                    //    IPAddress ip1;
                    //    if (!IPAddress.TryParse(Settings.General.TcpClientDefaultIp, out ip1))
                    //        throw new Exception("Server IP is not valid: " + Settings.General.TcpClientDefaultIp);
                    //    TcpServer.Start(Settings.General.TcpServerPort, ip1);
                    //    return;
                    //}

                    if (string.IsNullOrWhiteSpace(user_name))
                    {
                        Log.Main.Error("Session's user name is empty.");
                        return;
                    }
                    Log.Main.Inform("User logged in: " + user_name);

                    string service = Settings.General.GetServiceName();
                    IReadOnlyList <IZeroconfHost> zhs = ZeroconfResolver.ResolveAsync(service, TimeSpan.FromSeconds(3), 1, 10).Result;
                    if (zhs.Count < 1)
                    {
                        currentServerIp = Settings.General.TcpClientDefaultIp;
                        string m        = "Service '" + service + "' could not be resolved.\r\nUsing default ip: " + currentServerIp;
                        Log.Main.Warning(m);
                        InfoWindow.Create(m, null, "OK", null, Settings.View.ErrorSoundFile, System.Windows.Media.Brushes.WhiteSmoke, System.Windows.Media.Brushes.Yellow);
                    }
                    else if (zhs.Where(x => x.IPAddress != null).FirstOrDefault() == null)
                    {
                        currentServerIp = Settings.General.TcpClientDefaultIp;
                        string m        = "Resolution of service '" + service + "' has no IP defined.\r\nUsing default ip: " + currentServerIp;
                        Log.Main.Error(m);
                        InfoWindow.Create(m, null, "OK", null, Settings.View.ErrorSoundFile, System.Windows.Media.Brushes.WhiteSmoke, System.Windows.Media.Brushes.Red);
                    }
                    else
                    {
                        currentServerIp = zhs.Where(x => x.IPAddress != null).FirstOrDefault().IPAddress;
                        Log.Main.Inform("Service: " + service + " has been resolved to: " + currentServerIp);
                    }

                    IPAddress ip;
                    if (!IPAddress.TryParse(currentServerIp, out ip))
                    {
                        throw new Exception("Server IP is not valid: " + currentServerIp);
                    }
                    TcpServer.Start(Settings.General.TcpServerPort, ip);

                    string url = "http://" + currentServerIp + "/screenCapture/register?username="******"&ipaddress=" + TcpServer.LocalIp + "&port=" + TcpServer.LocalPort;
                    Log.Main.Inform("GETing: " + url);

                    HttpClient hc          = new HttpClient();
                    HttpResponseMessage rm = hc.GetAsync(url).Result;
                    if (!rm.IsSuccessStatusCode)
                    {
                        throw new Exception(rm.ReasonPhrase);
                    }
                    if (rm.Content == null)
                    {
                        throw new Exception("Response is empty");
                    }
                    string responseContent = rm.Content.ReadAsStringAsync().Result;
                    if (responseContent.Trim() != "OK")
                    {
                        throw new Exception("Response: " + responseContent);
                    }
                }
                catch (Exception e)
                {
                    Log.Main.Error(e);
                    InfoWindow.Create(Log.GetExceptionMessage(e), null, "OK", null, Settings.View.ErrorSoundFile, System.Windows.Media.Brushes.WhiteSmoke, System.Windows.Media.Brushes.Red);
                }
            },
                null,
                () =>
            {
                userLoggedOn_t = null;
            }
                );
        }
Пример #2
0
        public static InfoWindow Create(string title, string text, string image_url, string action_name, Action action, string sound_file = null, Brush box_brush = null, Brush button_brush = null)
        {
            InfoWindow w = null;

            if (text.Length > Settings.View.InfoToastMaxTextLength)
            {
                text = text.Remove(Settings.View.InfoToastMaxTextLength, text.Length - Settings.View.InfoToastMaxTextLength) + "<...>";
            }

            Action a = () =>
            {
                w = new InfoWindow(title, text, image_url, action_name, action);
                w.SetAppearance(box_brush, button_brush);
                WindowInteropHelper h = new WindowInteropHelper(w);
                h.EnsureHandle();
                w.Show();
                ThreadRoutines.StartTry(() =>
                {
                    Thread.Sleep(Settings.View.InfoToastLifeTimeInSecs * 1000);
                    w.Dispatcher.BeginInvoke((Action)(() => { w.Close(); }));
                });
                if (string.IsNullOrWhiteSpace(sound_file))
                {
                    sound_file = Settings.View.InfoSoundFile;
                }
                SoundPlayer sp = new SoundPlayer(sound_file);
                sp.Play();
            };

            lock (ws)
            {
                if (dispatcher == null)
                {//!!!the following code does not work in static constructor because creates a deadlock!!!
                    dispatcher_t = ThreadRoutines.StartTry(() =>
                    {
                        if (invisible_owner_w == null)
                        {//this window is used to hide notification windows from Alt+Tab panel
                            invisible_owner_w               = new Window();
                            invisible_owner_w.Width         = 0;
                            invisible_owner_w.Height        = 0;
                            invisible_owner_w.WindowStyle   = WindowStyle.ToolWindow;
                            invisible_owner_w.ShowInTaskbar = false;
                            invisible_owner_w.Show();
                            invisible_owner_w.Hide();
                        }

                        if (dispatcher == null)
                        {
                            //dispatcher = System.Windows.Threading.Dispatcher.CurrentDispatcher;
                            dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread);
                            System.Windows.Threading.Dispatcher.Run();
                        }
                    }, null, null, true, ApartmentState.STA);
                    if (!SleepRoutines.WaitForCondition(() => { return(dispatcher != null); }, 3000))
                    {
                        throw new Exception("Could not get dispatcher.");
                    }
                }
            }
            dispatcher.Invoke(a);
            return(w);
        }