Esempio n. 1
0
        public bool OnReceiveAsyncStream(SocketAsyncEventArgs arg)
        {
            if (arg.BytesTransferred > 4 && clientStream.pmIn.NewRead() == arg.BytesTransferred)
            {
                switch (clientStream.pmIn.ReadCommand())
                {
                case Command.REMOTE_DESKTOP_CONNECT_STREAM_SUCCESS:
                {
                    MainWindow.loader.ChangeText("Creating input stream...");
                    System.Net.IPAddress address = clientStream.ClientAddress;

                    clientInput        = new SOCKET.Client();
                    clientInput.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
                    {
                        clientInput.socket.Connect(address, Settings.s.remoteDesktopPort);
                        clientInput.StartReceiveAsync(OnReceiveAsyncInput);

                        clientInput.socket.SendTimeout    = 5000;
                        clientInput.socket.ReceiveTimeout = 5000;

                        clientInput.pmOut.Write(Command.REMOTE_DESKTOP_CONNECT_INPUT);

                        clientInput.pmOut.End();

                        if (clientInput.SendSafe())
                        {
                            //cw.WriteLine(Remote.Language.Find("ClientConnecting", this, "Connecting to {0}"), Address.Text);
                            return(true);
                        }
                        else
                        {
                            //cw.WriteLine(clientInput.lastSendException.Message);
                            clientInput.Close();
                            clientInput = null;
                            return(false);
                        }

                        //cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully conencted to {0}"), Address.Text);
                    }
                    catch (System.Net.Sockets.SocketException ee)
                    {
                        //cw.WriteLine(ee.Message);
                        clientInput.Close();
                        clientInput = null;
                    }
                }
                break;

                default:
                    break;
                }
            }
            return(false);
        }
Esempio n. 2
0
        // Todo: Blocking call if failed to connect for about 10-15 sec
        private void Connect_Click(object sender, RoutedEventArgs e)
        {
            /*if (client != null)
             * {
             *      cw.WriteLine(Remote.Language.Find("ClientNotNull", this, "A connection is already made!"));
             *      return;
             * }*/

            System.Net.IPAddress address;
            if (System.Net.IPAddress.TryParse(Address.Text, out address))
            {
                SOCKET.Client client = new SOCKET.Client();
                client.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
                {
                    client.socket.Connect(address, Settings.s.remoteDesktopPort);
                    client.StartReceiveAsync(OnReceiveAsync);

                    client.socket.SendTimeout    = 5000;
                    client.socket.ReceiveTimeout = 5000;

                    client.pmOut.Write(Command.CONNECT);
                    client.pmOut.Write(Command.USER_NAME);
                    client.pmOut.Write(Settings.s.name);
                    client.pmOut.End();

                    if (client.SendSafe())
                    {
                        cw.WriteLine(Remote.Language.Find("ClientConnecting", this, "Connecting to {0}"), Address.Text);
                        return;
                    }
                    else
                    {
                        cw.WriteLine(client.lastSendException.Message);
                        client.Close();
                        client = null;
                    }

                    //cw.WriteLine(Remote.Language.Find("ClientConnected", this, "Succesfully conencted to {0}"), Address.Text);
                }
                catch (System.Net.Sockets.SocketException ee)
                {
                    cw.WriteLine(ee.Message);
                    client.Close();
                    client = null;
                }
            }
        }
Esempio n. 3
0
        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);
        }