void run() { while (receiving_thread != null) { TcpMessage m = receive_message(); Log.Main.Inform("Tcp message received: " + m.Name + "\r\n" + m.BodyAsText); UiApi.Message(MessageType.INFORM, "Tcp message received: " + m.Name + "\r\n" + m.BodyAsText); string reply = TcpMessage.Success; try { switch (m.Name) { case TcpMessage.FfmpegStart: MpegStream.Start(Service.UserSessionId, m.BodyAsText); break; case TcpMessage.FfmpegStop: MpegStream.Stop(); break; case TcpMessage.SslStart: if (stream is SslStream) { throw new Exception("SSL is already started."); } break; case TcpMessage.Poll: lock (errors) { if (errors.Count > 0) { reply = string.Join("\r\n", errors); errors.Clear(); } } break; default: throw new Exception("Unknown message: " + m.Name); } } catch (Exception e) { reply = e.Message; Log.Main.Error("Tcp message processing: ", e); } Log.Main.Inform("Tcp message sending: " + m.Name + "\r\n" + reply); TcpMessage m2 = new TcpMessage(m.Name, reply); send_message(m2); if (m.Name == TcpMessage.SslStart && reply == TcpMessage.Success) { startSsl(); } } }
protected override void OnStart(string[] args) { try { Log.Main.Inform("Starting..."); UiApi.OpenApi(); //try //{ // uint dwSessionId = WinApi.Wts.WTSGetActiveConsoleSessionId(); // MpegStream.Start(dwSessionId, "-f gdigrab -framerate 10 -f rtp_mpegts -srtp_out_suite AES_CM_128_HMAC_SHA1_80 -srtp_out_params aMg7BqN047lFN72szkezmPyN1qSMilYCXbqP/sCt srtp://127.0.0.1:5920"); //} //catch (Exception e) //{ // Console.WriteLine(e); //} uint sessionId = WinApi.Wts.WTSGetActiveConsoleSessionId(); if (sessionId == 0 || sessionId == 0xFFFFFFFF) { Log.Main.Inform("No console user is active."); } else { sessionChanged(sessionId, true); } } catch (Exception e) { Log.Main.Error(e); throw e; } }
protected override void OnStop() { try { Log.Main.Inform("Stopping..."); stopServingUser(); UiApi.CloseApi(); } catch (Exception e) { Log.Main.Error(e); throw e; } }
static void Main() { try { Log.Main.Inform("Version: " + AssemblyRoutines.GetAppVersion()); string user = ProcessRoutines.GetProcessUserName(); string m = "User: "******" (as administrator)"; } Log.Main.Inform(m); #if !test ServiceBase.Run(new Service()); #else MpegStream.Start(1, "-f gdigrab -framerate 10 -f rtp_mpegts -srtp_out_suite AES_CM_128_HMAC_SHA1_80 -srtp_out_params aMg7BqN047lFN72szkezmPyN1qSMilYCXbqP/sCt srtp://127.0.0.1:5920"); //Service.sessionChanged(1, true); //System.Threading.Thread.Sleep(1000000); //s.Stop //MpegStream.Start(1, ""); UserSessionApi.OpenApi(); UiApi.OpenApi(); ServiceControllerStatus scs = ServiceControllerStatus.Running; for (; ;) { System.Threading.Thread.Sleep(10000); UiApi.Message(MessageType.INFORM, "test"); scs = scs == ServiceControllerStatus.Running ? ServiceControllerStatus.Stopped : ServiceControllerStatus.Running; } #endif } catch (Exception e) { Log.Main.Error(e); } }
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); UiApi.Message(MessageType.ERROR, Log.GetExceptionMessage(e)); } Stop(); }); }
internal static void sessionChanged(uint sessionId, bool active) { try { if (sessionId == 0 || !active) { Log.Main.Inform("User logged off: " + currentUserName); stopServingUser(); currentUserSessionId = 0; currentUserName = null; return; } string userName = WindowsUserRoutines.GetUserNameBySessionId(sessionId); if (userName == currentUserName) { return; } Log.Main.Inform("User logged in: " + userName); stopServingUser(); currentUserSessionId = sessionId; currentUserName = userName; if (string.IsNullOrWhiteSpace(currentUserName)) { Log.Main.Error("Session's user name is empty."); return; } onNewUser_t = ThreadRoutines.StartTry( () => { 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); UiApi.Message(MessageType.WARNING, m); } 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); UiApi.Message(MessageType.ERROR, m); } 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("GETting: " + 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().ToUpper() != "OK") { throw new Exception("Response body: " + responseContent); } Log.Main.Inform("Response body: " + responseContent); }, (Exception e) => { Log.Main.Error(e); TcpServer.Stop(); UiApi.Message(MessageType.ERROR, Log.GetExceptionMessage(e)); }, () => { onNewUser_t = null; } ); } catch (Exception e) { Log.Main.Error(e); } }