public void runCBListenerFaster()
        {
            this.cbListener = new Thread(() =>
            {
                try
                {
                    Thread.CurrentThread.IsBackground = true;



                    while (true)
                    {
                        //ho spostato qui queste tre righe per evitare il prob che ogni volta va tutto a puttane
                        Console.Write("Waiting for ClipBoard connection... ");
                        TcpClient acceptedClient = this.cbSocketServer.AcceptTcpClient();
                        Console.WriteLine("Clipboard is Connected!");

                        Thread.Sleep(100);
                        try
                        {
                            Console.WriteLine("Aspettando un messaggio dalla clipboard");
                            NetworkStream stream = acceptedClient.GetStream();
                            byte[] buffer        = receiveAllData(stream);

                            Object received = AmbrUtils.ByteArrayToObject(buffer);
                            Console.WriteLine("FINE RICEZIONE\t Tipo: " + received.GetType() + " Dimensione : " + buffer.Length + " bytes");
                            SetClipBoard(received);
                            Console.WriteLine("CBLISTENER : clipboard settata");
                        }
                        catch (IndexOutOfRangeException cbex)
                        {
                            //eccezione generata quando chiudo il client dalla clipboard
                            //bool b = this.isConnected;
                            Console.WriteLine("Index Out Of Range  generata in cb: [{0}]", cbex.Message);
                            //this.isConnected = false;
                            //closeOnException();
                            return;
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("ECCEZIONE GENERATA IN RICEZIONE CB : [{0}]", ex.Message);
                            return;
                            //closeOnException();
                            //return;
                            // restartServer();
                        }
                    }
                }

                catch (Exception e)
                {
                    Console.WriteLine("Eccezione generica in cblistener " + e.Message);
                    return;
                }
            });
            this.cbListener.Start();
        }
Beispiel #2
0
        internal bool authenticateWithPassword()
        {
            byte[] b = AmbrUtils.ObjectToByteArray(this.password);
            Console.WriteLine("Mandato password : [" + this.password + "]");

            //UdpSender.Send(b, b.Length);
            server.Client.Send(b, b.Length, 0);

            //byte[] receivedResponse = UdpSender.Receive(ref remoteIPEndPoint);
            byte[] receivedResponse = new byte[AmbrUtils.ObjectToByteArray(new Boolean()).Length];
            server.Client.Receive(receivedResponse);
            Boolean result = (Boolean)AmbrUtils.ByteArrayToObject(receivedResponse);

            return(result);
        }
        public void setData()
        {
            Type t = this.content.GetType();

            try
            {
                if (t == typeof(String))
                {
                    //setta il file di testo nella clipboard
                    Clipboard.SetText((String)this.content);
                }
                else if (t == typeof(ZipArchive))
                {
                    //extraction  already been done
                    Clipboard.Clear();
                    System.Collections.Specialized.StringCollection files = getFileNames(AmbrUtils.ZIP_EXTRACTED_FOLDER + @"/CBFILES/"); //add all files to list
                    foreach (DirectoryInfo dir in new DirectoryInfo(AmbrUtils.ZIP_EXTRACTED_FOLDER + @"/CBFILES/").GetDirectories())
                    {
                        files.Add(dir.FullName);
                    }
                    if (files != null && files.Count > 0)
                    {
                        Clipboard.SetFileDropList(files);
                    }
                }
                else if (t == typeof(BitmapImage))
                {
                    Clipboard.SetImage((BitmapImage)content);
                }
                else if (t == typeof(Stream))
                {
                    Clipboard.SetAudio((Stream)content);
                }
                else
                {
                    Console.WriteLine("Non sono riuscito ad identificare il tipo");
                }
                int millis = 3000;
                AmbrUtils.showPopUpMEssage("La clipboard è stata aggiornata!\n(Questa finestra si chiuderà in " + ((int)millis / 1000) + " secondi", millis);

                Console.WriteLine("La clipboard è stata settata");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        public void sendClipBoardFaster(TcpClient client)
        {
            byte[] content     = new byte[0]; //byte array that will contain the clipboard
            byte[] sizeInBytes = new byte[4]; //byte array that will contain the size

            if (Clipboard.ContainsText())
            {
                content = AmbrUtils.ObjectToByteArray(Clipboard.GetText());
            }
            else if (Clipboard.ContainsFileDropList())
            {
                //Creates a new, blank zip file to work with - the file will be
                //finalized when the using
                if (Directory.Exists(AmbrUtils.CB_FILES_DIRECTORY_PATH))     //delete folder if exists
                {
                    DirectoryInfo di = new DirectoryInfo(AmbrUtils.CB_FILES_DIRECTORY_PATH);
                    di.Attributes &= ~FileAttributes.ReadOnly;
                    //di.Attributes = FileAttributes.Normal;
                    Directory.Delete(AmbrUtils.CB_FILES_DIRECTORY_PATH, true);
                }
                if (File.Exists(AmbrUtils.ZIP_FILE_NAME_AND_PATH))
                {
                    FileInfo di = new FileInfo(AmbrUtils.ZIP_FILE_NAME_AND_PATH);
                    //di.Attributes &= ~FileAttributes.ReadOnly;
                    di.Attributes = FileAttributes.Normal;
                    File.Delete(AmbrUtils.ZIP_FILE_NAME_AND_PATH);
                }
                DirectoryInfo newDirInfo = Directory.CreateDirectory(AmbrUtils.CB_FILES_DIRECTORY_PATH);
                newDirInfo.Attributes &= ~FileAttributes.ReadOnly;

                foreach (String filepath in Clipboard.GetFileDropList())
                {
                    FileAttributes attr = File.GetAttributes(filepath);
                    if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        DirectoryInfo diSource = new DirectoryInfo(filepath);
                        System.IO.Directory.CreateDirectory(AmbrUtils.CB_FILES_DIRECTORY_PATH + diSource.Name);
                        DirectoryInfo diDst = new DirectoryInfo(AmbrUtils.CB_FILES_DIRECTORY_PATH + diSource.Name);
                        AmbrUtils.CopyFilesRecursively(diSource, diDst);
                    }
                    else       //it's a file
                    {
                        String dstFilePath = AmbrUtils.CB_FILES_DIRECTORY_PATH + Path.GetFileName(filepath);
                        System.IO.File.Copy(filepath, dstFilePath);
                    }
                }
                ZipFile.CreateFromDirectory(AmbrUtils.CB_FILES_DIRECTORY_PATH, AmbrUtils.ZIP_FILE_NAME_AND_PATH, CompressionLevel.Fastest, true);
                FileInfo info = new FileInfo(AmbrUtils.ZIP_FILE_NAME_AND_PATH);
                Console.WriteLine("Dimensione del file zip : " + info.Length + " bytes");
                if (info.Length > 1024 * 1024 * 200)     //limite a 200 mega
                {
                    MessageBoxResult result = MessageBox.Show("Non è possibile mandare file per più di 200 mega ( attualmente " + info.Length + " bytes )");
                    Console.WriteLine("Can't send more than 200 Mega Bytes");
                    return;
                }
                content = File.ReadAllBytes(AmbrUtils.ZIP_FILE_NAME_AND_PATH);
            }
            else if (Clipboard.ContainsImage())
            {
                //content = imageToByteArray(Clipboard.GetImage());
                content = AmbrUtils.bitmapSourceToByteArray(Clipboard.GetImage());
            }
            else if (Clipboard.ContainsAudio())
            {
                content = AmbrUtils.audioSourceToByteArray(Clipboard.GetAudioStream());
            }
            else
            {
                Console.WriteLine("Nothing to send");
                return;
            }

            NetworkStream ns  = client.GetStream();
            Int32         len = content.Length;

            sizeInBytes = BitConverter.GetBytes(len); //convert size of content into byte array
            Console.WriteLine("Mando size: " + len);
            ns.Write(sizeInBytes, 0, 4);              //write
            Console.WriteLine("Mando buffer...");
            ns.Write(content, 0, content.Length);
            ns.Flush();
            Console.WriteLine("Mandato!");
        }