void run()
        {
            while (thread != null)
            {
                TcpMessage m = TcpMessage.Receive(stream);

                Log.Main.Inform("Tcp message received: " + m.Name + "\r\n" + m.BodyAsText);
                InfoWindow.Create("Tcp message received: " + m.Name + "\r\n" + m.BodyAsText, null, "OK", null);

                string reply = TcpMessage.Success;
                try
                {
                    switch (m.Name)
                    {
                    case TcpMessage.FfmpegStart:
                        uint sessionId = WinApi.Wts.WTSGetActiveConsoleSessionId();
                        MpegStream.Start(sessionId, m.BodyAsText);
                        break;

                    case TcpMessage.FfmpegStop:
                        MpegStream.Stop();
                        break;

                    case TcpMessage.SslStart:
                        if (stream is SslStream)
                        {
                            throw new Exception("SSL is already started.");
                        }
                        break;

                    default:
                        throw new Exception("Unknown message: " + m.Name);
                    }
                }
                catch (Exception e)
                {
                    reply = e.Message;
                }
                Log.Main.Inform("Tcp message sending: " + m.Name + "\r\n" + reply);
                m.Reply(stream, reply);
                if (m.Name == TcpMessage.SslStart && reply == TcpMessage.Success)
                {
                    startSsl();
                }
            }
        }
Пример #2
0
        //static Dictionary<ushort, TcpServer> servers = new Dictionary<ushort, TcpServer>();

        static public void Start(int local_port, IPAddress destination_ip)
        {
            if (!NetworkRoutines.IsNetworkAvailable())
            {
                throw new Exception("No network available.");
            }
            IPAddress ipAddress = NetworkRoutines.GetLocalIpForDestination(destination_ip);

            if (local_port == LocalPort && ipAddress.Equals(LocalIp))
            {
                return;
            }
            Stop();

            Log.Main.Inform("Starting TCP listener on " + local_port + " for " + destination_ip);

            //listeningSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            //listeningSocket.Bind(localEndPoint);
            ////listeningSocket.Listen(100);
            server = new TcpListener(ipAddress, local_port);
            server.Start();

            thread = ThreadRoutines.StartTry(run, (Exception e) =>
            {
                if (e is SocketException)
                {
                    Log.Main.Warning(e);
                }
                else
                {
                    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);
                }
                Stop();
            });
        }
Пример #3
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;
            }
                );
        }