Пример #1
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);
        }
Пример #2
0
        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!");
        }