private static void InitProtoClient() { if (ProtoClient.IsConnected()) { // Proto client already initialized return; } try { LOG.Info("Initializing Proto Client"); ProtoClient.Disconnect(); ProtoClient.Init(SettingsManager.HyperionServerIp, SettingsManager.HyperionServerPort, SettingsManager.HyperionMessagePriority); // Double checking since sometimes exceptions are not thrown even if connection fails if (ProtoClient.IsConnected()) { LOG.Info("Proto Client initialized"); NotificationUtils.Info($"Connected to Hyperion server on {SettingsManager.HyperionServerIp}:{SettingsManager.HyperionServerPort}!"); } else { throw new Exception(GetProtoInitFailedMsg()); } } catch (Exception ex) { throw new Exception(GetProtoInitFailedMsg(), ex); } }
public static void Init(bool reInit = false, bool forceOn = false) { if (!_initLock) { _initLock = true; // Stop current capture first on reinit if (reInit) { _captureEnabled = false; Thread.Sleep(500 + Settings.CaptureInterval); if (_protoClient != null) { ProtoClient.Disconnect(); Thread.Sleep(500); } } _protoClient = new ProtoClient(); ProtoClient.Init(Settings.HyperionServerIp, Settings.HyperionServerPort, Settings.HyperionMessagePriority); if (Settings.CaptureOnStartup || forceOn) { if (ProtoClient.IsConnected()) { Notifications.Info($"Connected to Hyperion server on {Settings.HyperionServerIp}!"); ToggleCapture("ON"); } } if (Settings.ApiEnabled) { _apiServer = new ApiServer(); _apiServer.StartServer("localhost", Settings.ApiPort.ToString()); } else { _apiServer?.StopServer(); } _initLock = false; } }
private void PowerModeChanged(object sender, PowerModeChangedEventArgs powerMode) { // On resume restart capture instance after grace period in case that was resume if (powerMode.Mode == PowerModes.Resume) { if (_captureEnabled) { _captureEnabled = false; Thread.Sleep(2500); ToggleCapture("ON"); } Thread.Sleep(1500); _protoClient = new ProtoClient(); ProtoClient.Init(Settings.HyperionServerIp, Settings.HyperionServerPort, Settings.HyperionMessagePriority); } }
private void StartCapture() { try { while (CaptureEnabled) { if (!_protoClient.IsConnected()) { // Reconnect every 2.5s _protoClient.Init(HyperionServerIp, HyperionServerPort, HyperionMessagePriority); Thread.Sleep(2500); continue; } Surface s = _d.CaptureScreen(HyperionWidth, HyperionHeight); DataStream ds = Surface.ToStream(s, ImageFileFormat.Bmp); byte[] buffer = new byte[ds.Length]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = ds.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } byte[] pixelData = ms.ToArray(); _protoClient.SendImage(pixelData); Thread.Sleep(CaptureInterval); } ds.Dispose(); s.Dispose(); } } catch (Exception ex) { if (NotificationLevel == NotifcationLevels.Info || NotificationLevel == NotifcationLevels.Error) { Notifications.Error("Failed to take screenshot." + ex.Message); } } }
public Form1() { InitializeComponent(); // Create a simple tray menu with only one item. var trayMenu = new ContextMenu(); trayMenu.MenuItems.Add("Exit", OnExit); TrayIcon = new NotifyIcon { Text = @"Hyperion Screen Capture (Not Connected)" }; TrayIcon.DoubleClick += TrayIcon_DoubleClick; TrayIcon.Icon = Resources.Hyperion_disabled; // Add menu to tray icon and show it. TrayIcon.ContextMenu = trayMenu; TrayIcon.Visible = true; _d = new DxScreenCapture(MonitorIndex); _protoClient = new ProtoClient(); _protoClient.Init(HyperionServerIp, HyperionServerPort, HyperionMessagePriority); if (_protoClient.IsConnected()) { if (NotificationLevel == NotifcationLevels.Info) { Notifications.Info(string.Format("Connected to Hyperion server on {0}!", HyperionServerIp)); } CaptureEnabled = true; Thread t = new Thread(StartCapture) { IsBackground = true }; t.Start(); } TrayIcon.Icon = Resources.Hyperion_enabled; TrayIcon.Text = @"Hyperion Screen Capture (Enabled)"; }
private static void StartCapture() { try { _d = new DxScreenCapture(Settings.MonitorIndex); while (_captureEnabled) { if (!ProtoClient.IsConnected()) { // Reconnect every 5s (default) ProtoClient.Init(Settings.HyperionServerIp, Settings.HyperionServerPort, Settings.HyperionMessagePriority); Thread.Sleep(Settings.ReconnectInterval); continue; } var s = _d.CaptureScreen(Settings.HyperionWidth, Settings.HyperionHeight, _d.MonitorIndex); var dr = s.LockRectangle(LockFlags.None); var ds = dr.Data; var x = RemoveAlpha(ds); s.UnlockRectangle(); s.Dispose(); ds.Dispose(); ProtoClient.SendImageToServer(x); // Add small delay to reduce cpu usage (200FPS max) Thread.Sleep(Settings.CaptureInterval); } _d = null; } catch (Exception ex) { _captureEnabled = false; Notifications.Error("Error occured during capture: " + ex.Message); } }