private void Application_Startup(object sender, StartupEventArgs e) { RemoteGuiLoader.MainWindow loader = new RemoteGuiLoader.MainWindow(); Action <string> action = loader.ChangeText; loader.Show(); loader.ChangeText("Loading..."); //loader.Dispatcher.Invoke(new Action(() => loader.ChangeText(a)), System.Windows.Threading.DispatcherPriority.Render); Remote.Startup.Load(loader.ChangeText); loader.ChangeText("Loading Window"); if (e.Args.Length == 0) //nincs parancs, normális indítás { //Settings.Load(); //No longer needed MainWindow mw = new MainWindow(); loader.Close(); mw.Show(); } else if (e.Args.Length == 2) { switch (e.Args[0].ToLower()) { case "-remotedesktop": { Options o = new Options(OptionsTypes.RemoteDesktop); o.ShowDialog(); RemoteDesktop rd = new RemoteDesktop(); System.Net.IPAddress address; if (System.Net.IPAddress.TryParse(e.Args[1], out address)) { rd.clientStream = new SOCKET.Client(); rd.clientStream.socket = new System.Net.Sockets.Socket(new System.Net.IPEndPoint(address, 6546).AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); try { rd.clientStream.socket.Connect(address, 6546); } catch (System.Net.Sockets.SocketException ee) { Environment.Exit(1); //Console.WriteLine(e); } } PacketManager pmOut = rd.clientStream.pmOut; pmOut.New(); pmOut.Set(Command.REMOTE_DESKTOP); pmOut.bw.Write(UserControls.RemoteDesktopOptions.screenCaptureMethods[o.rd.ScreenCaptureMethodComboBox.SelectedIndex]); pmOut.bw.Write(UserControls.RemoteDesktopOptions.frameBufferings[o.rd.BufferingComboBox.SelectedIndex]); pmOut.bw.Write(UserControls.RemoteDesktopOptions.desktopCompositions[o.rd.CompositionComboBox.SelectedIndex]); pmOut.bw.Write(UserControls.RemoteDesktopOptions.pixelFormats[o.rd.FormatComboBox.SelectedIndex]); pmOut.bw.Write(UserControls.RemoteDesktopOptions.framesPerSeconds[o.rd.FPSComboBox.SelectedIndex]); pmOut.bw.Write(UserControls.RemoteDesktopOptions.compressions[o.rd.CompressionComboBox.SelectedIndex]); pmOut.bw.Write(((System.Windows.Controls.ComboBoxItem)o.rd.CodecComboBox.SelectedItem).Content.ToString()); pmOut.bw.Write(UserControls.RemoteDesktopOptions.LZ4BlockSizes[o.rd.LZ4BlockSizeComboBox.SelectedIndex]); bool?b = rd.ShowDialog(); } break; default: break; } } }
private bool OnReceiveAsync(System.Net.Sockets.SocketAsyncEventArgs arg) { // Get the client object from arg SOCKET.Client client = (SOCKET.Client)arg.UserToken; // Notes: First 4 bytes are the size of the message. // Notes: It's impossible to send only 4 bytes, as that would mean we only send the message size, which doesn't make sense , so this must be always bigger than 4 bytes if (arg.BytesTransferred > 4 && client.pmIn.NewRead() == arg.BytesTransferred) { lastCommand = client.pmIn.ReadCommand(); switch (lastCommand) { // Connection attempt case Command.CONNECT: #region CONNECT { cw.WriteLine(Remote.Language.Find("IncomingConnection", this, "Incoming connection attempt from: {0}"), client.ClientAddress); Command c = client.pmIn.ReadCommand(); if (c == Command.USER_NAME) { Dispatcher.Invoke(new Action(() => { foreach (var cd in clients) { if (cd.client == client) { cd.name = client.pmIn.ReadString(); cd.lName.Content = cd.name; connectedComputersItems.Add(cd.sp); ConnectedComputers.ItemsSource = connectedComputersItems; break; } } }), DispatcherPriority.Render); client.pmOut.New(); client.pmOut.Write(Command.CONNECT_SUCCESS); client.pmOut.Write(Command.USER_NAME); client.pmOut.Write(Settings.s.name); client.pmOut.End(); if (client.SendSafe()) { cw.WriteLine(Remote.Language.Find("ClientConnectSuccess", this, "Client from {0} successfully connected!"), client.ClientAddress); return(true); } else { cw.WriteLine(client.lastSendException.Message); client.Close(); client = null; } } } break; #endregion // Success! case Command.CONNECT_SUCCESS: #region CONNECT_SUCCES { cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully connected to {0}"), client.ClientAddress); Command c = client.pmIn.ReadCommand(); if (c == Command.USER_NAME) { Dispatcher.Invoke(new Action(() => { ClientData cd = new ClientData(); cd.client = client; Label l = new Label(); cd.name = client.pmIn.ReadString(); cd.lName = l; l.Content = cd.name; StackPanel sp = new StackPanel(); sp.Children.Add(l); //connectedComputersBinding.Add(sp, cd); clients[sp] = cd; connectedComputersItems.Add(sp); ConnectedComputers.ItemsSource = connectedComputersItems; }), DispatcherPriority.Render); } return(true); } #endregion case Command.REMOTE_DESKTOP_REQUEST: #region REMOTE_DESKTOP_REQUEST { ClientData cd = clients[(SOCKET.Client)arg.UserToken]; cd.remoteDesktopRequest = true; Dispatcher.Invoke(new Action(() => { ConnectedComputers_SelectionChanged(null, null); }), DispatcherPriority.Render); cw.WriteLine(Remote.Language.Find("RemoteDesktopRequestIncoming", this, "A Remote Desktop request was received from {0} / {1}"), cd.name, cd.client.ClientAddress); return(true); } #endregion case Command.REMOTE_DESKTOP_CAPTURE_INFO_REQUEST: #region REMOTE_DESKTOP_CAPTURE_INFO_REQUEST { Dispatcher.Invoke(new Action(() => { this.WindowState = System.Windows.WindowState.Minimized; loader.Show(); }), DispatcherPriority.Render); loader.ChangeText("Sending streaming settings..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; cd.client.pmOut.New(); cd.client.pmOut.Write(Command.REMOTE_DESKTOP_CAPTURE_INFO); ProtoBuf.Serializer.SerializeWithLengthPrefix(cd.client.pmOut.Stream, Settings.s.remoteDesktopSettings, ProtoBuf.PrefixStyle.Base128); cd.client.pmOut.End(); if (cd.client.SendSafe()) { return(true); } else { } } break; #endregion case Command.REMOTE_DESKTOP_CAPTURE_INFO: #region REMOTE_DESKTOP_CAPTURE_INFO { loader.ChangeText("Creating host..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; Settings.RemoteDesktopSettings s = ProtoBuf.Serializer.DeserializeWithLengthPrefix <Settings.RemoteDesktopSettings>(cd.client.pmIn.Stream, ProtoBuf.PrefixStyle.Base128); host = new RemoteDesktopHost(s); cd.client.pmOut.New(); cd.client.pmOut.Write(Command.REMOTE_DESKTOP_HOST_READY); cd.client.pmOut.Write(SystemParameters.PrimaryScreenWidth); cd.client.pmOut.Write(SystemParameters.PrimaryScreenHeight); cd.client.pmOut.End(); if (cd.client.SendSafe()) { return(true); } else { } } break; #endregion case Command.REMOTE_DESKTOP_HOST_READY: #region REMOTE_DESKTOP_HOST_READY { loader.ChangeText("Creating streaming connection..."); bool whatToReturn = false; RemoteDesktop rd; Dispatcher.Invoke(new Action(() => { rd = new RemoteDesktop(); rd.x = (int)client.pmIn.ReadDouble(); rd.y = (int)client.pmIn.ReadDouble(); System.Net.IPAddress address = client.ClientAddress; rd.clientStream = new SOCKET.Client(); rd.clientStream.socket = new System.Net.Sockets.Socket(new System.Net.IPEndPoint(address, Settings.s.remoteDesktopPort).AddressFamily, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp); try { rd.clientStream.socket.Connect(address, Settings.s.remoteDesktopPort); rd.clientStream.StartReceiveAsync(rd.OnReceiveAsyncStream); rd.clientStream.socket.SendTimeout = 5000; rd.clientStream.socket.ReceiveTimeout = 5000; rd.clientStream.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_STREAM); rd.clientStream.pmOut.End(); if (rd.clientStream.SendSafe()) { //cw.WriteLine(Remote.Language.Find("ClientConnecting", this, "Connecting to {0}"), Address.Text); whatToReturn = true; } else { cw.WriteLine(rd.clientStream.lastSendException.Message); rd.clientStream.Close(); rd.clientStream = null; whatToReturn = false; } //cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully conencted to {0}"), Address.Text); } catch (System.Net.Sockets.SocketException ee) { cw.WriteLine(ee.Message); rd.clientStream.Close(); rd.clientStream = null; } }), DispatcherPriority.Render); if (whatToReturn) { return(true); } else { return(false); } } #endregion case Command.REMOTE_DESKTOP_CONNECT_STREAM: #region REMOTE_DESKTOP_CONNECT_STREAM { loader.ChangeText("Streaming connection created..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; Dispatcher.Invoke(new Action(() => { cd.sp.Children.Remove(cd.lName); clients.Remove(cd); host.clientStream = cd.client; host.clientStream.onReceiveAsync = host.OnReceiveAsync; }), DispatcherPriority.Render); host.clientStream.pmOut.New(); host.clientStream.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_STREAM_SUCCESS); host.clientStream.pmOut.End(); if (host.clientStream.SendSafe()) { return(true); } else { } // send success } break; #endregion case Command.REMOTE_DESKTOP_CONNECT_INPUT: #region REMOTE_DESKTOP_CONNECT_INPUT { loader.ChangeText("Input stream created..."); ClientData cd = clients[(SOCKET.Client)arg.UserToken]; Dispatcher.Invoke(new Action(() => { cd.sp.Children.Remove(cd.lName); clients.Remove(cd); host.clientInput = cd.client; host.clientInput.onReceiveAsync = host.OnReceiveAsyncInput; }), DispatcherPriority.Render); host.clientInput.pmOut.New(); host.clientInput.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_INPUT_SUCCESS); host.clientInput.pmOut.End(); if (host.clientInput.SendSafe()) { return(true); } else { } // send success } break; #endregion default: break; } } //Client disconnected, clean up. else if (arg.BytesTransferred == 0) { switch (lastCommand) { // Error? EMPTY_COMMAND = 0 case Command.EMPTY_COMMAND: break; // Restore MainWindow upon client fail case Command.REMOTE_DESKTOP_CAPTURE_INFO_REQUEST: { Dispatcher.Invoke(new Action(() => { loader.Close(); loader = new RemoteGuiLoader.MainWindow(); WindowState = System.Windows.WindowState.Normal; }), DispatcherPriority.Render); cw.WriteLine(Remote.Language.Find("RemoteDekstopCaptureInfoRequestClientDisconnected", this, "The client disconnected during Remote Desktop initialization")); } break; default: break; } foreach (var item in connectedComputersItems) { } //cw.WriteLine("End"); } return(false); }