public static byte[] GetClipboardBytes(ClipboardPacket p)
        {
            byte[] res = new byte[8 * 1024];
            int    i   = 0;

            res[i++] = (byte)p.type;
            for (int j = sizeof(long); j > 0; j--)
            {
                res[i++] = (byte)(p.totalLength >> (8 * ((sizeof(long) - j))));
            }
            byte[] s = Encoding.Unicode.GetBytes(p.name);
            for (int j = 0; j < 256; j++)
            {
                res[i++] = (j < s.Length) ? s[j] : (byte)0;
            }
            for (int j = sizeof(int); j > 0; j--)
            {
                res[i++] = (byte)(p.length >> (8 * ((sizeof(int) - j))));
            }
            for (int j = 0; j < p.length; j++)
            {
                res[i++] = p.data[j];
            }
            return(res);
        }
        public static void SendBitmap(System.Windows.Media.Imaging.BitmapSource b)
        {
            ClipboardPacket packet = new ClipboardPacket();

            packet.type = BITMAP_TYPE;
            MemoryStream ms = new MemoryStream();

            System.Windows.Media.Imaging.BmpBitmapEncoder encoder = new System.Windows.Media.Imaging.BmpBitmapEncoder();

            encoder.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(b));
            encoder.Save(ms);

            byte[] getBitmapData = ms.ToArray();
            packet.length      = BUFFER_SIZE;
            packet.totalLength = ms.Length;
            packet.name        = String.Empty;
            long bytesToSend = ms.Length;

            while (bytesToSend > 0)
            {
                if (bytesToSend < BUFFER_SIZE)
                {
                    packet.length = (int)bytesToSend;
                }
                packet.data = new byte[BUFFER_SIZE];
                Array.Copy(getBitmapData, ms.Length - bytesToSend, packet.data, 0, packet.length);
                bytesToSend -= packet.length;
                byte[] toSend = Serialization.GetClipboardBytes(packet);

                Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
            }
            ms.Close();
        }
        public static void SendPathDropList(string path)
        {
            ClipboardPacket packet = new ClipboardPacket();

            packet.type        = SETDROPLIST_TYPE;
            packet.data        = System.Text.Encoding.Unicode.GetBytes(path);
            packet.length      = packet.data.Length;
            packet.totalLength = 0;
            packet.name        = String.Empty;

            byte[] toSend = Serialization.GetClipboardBytes(packet);
            Console.WriteLine("Text: sending path" + path);
            Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
        }
        public static void SendNewFolder(string folder, ref string rootDir)
        {
            ClipboardPacket packet = new ClipboardPacket();

            packet.type        = DIRECTORY_TYPE;
            packet.name        = folder.Substring(rootDir.Length);
            packet.length      = 0;
            packet.totalLength = 0;

            byte[] toSend = Serialization.GetClipboardBytes(packet);
            Console.WriteLine("Folder: sending {0} bytes to server...", toSend.Length);

            Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
        }
        public static void SendUpdateRequest()
        {
            /** Richiedo un aggiornamento della mia clipboard. Il server mi invierà il contenuto della sua clipboard **/
            ClipboardPacket packet = new ClipboardPacket();

            packet.data        = new byte[0];
            packet.length      = packet.data.Length;
            packet.totalLength = 0;
            packet.name        = String.Empty;
            packet.type        = UPDATE_TYPE;

            byte[] toSend = Serialization.GetClipboardBytes(packet);
            Console.WriteLine("Sending update packet");
            Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
        }
        public static void SendText(string text)
        {
            ClipboardPacket packet = new ClipboardPacket();

            packet.type        = TEXT_TYPE;
            packet.data        = System.Text.Encoding.Unicode.GetBytes(text);
            packet.length      = packet.data.Length;
            packet.totalLength = 0;
            packet.name        = String.Empty;

            byte[] toSend = Serialization.GetClipboardBytes(packet);
            Console.WriteLine("Text: sending {0} bytes to server...", toSend.Length);

            Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
        }
        public static ClipboardPacket FromClipboardBytes(byte[] arr)
        {
            ClipboardPacket p = new ClipboardPacket();
            int             i = 0;

            p.type        = arr[i++];
            p.totalLength = BitConverter.ToInt64(arr, i);
            i            += 8;
            p.name        = Encoding.Unicode.GetString(arr, i, 256).TrimEnd('\0'); //remove all string terminator characters
            i            += 256;
            p.length      = BitConverter.ToInt32(arr, i);
            i            += 4;
            p.data        = new byte[p.length];
            for (int j = 0; j < p.length; j++, i++)
            {
                p.data[j] = arr[i];
            }
            return(p);
        }
        public static void SendFile(string fileToSend, ref string rootDir)
        {
            int  readBytes   = 0;
            long bytesToSend = new FileInfo(fileToSend).Length;

            ClipboardPacket packet = new ClipboardPacket();

            packet.type = FILE_TYPE;
            String filename = fileToSend.Substring(rootDir.Length);

            packet.name        = filename;
            packet.totalLength = bytesToSend;

            FileStream fileStream = new FileStream(fileToSend, FileMode.Open);

            byte[] buffer = new byte[BUFFER_SIZE];
            try
            {
                do
                {
                    readBytes    = fileStream.Read(buffer, 0, BUFFER_SIZE);
                    bytesToSend -= readBytes;

                    packet.length = readBytes;
                    packet.data   = new byte[BUFFER_SIZE];
                    Array.Copy(buffer, packet.data, BUFFER_SIZE);

                    byte[] toSend = Serialization.GetClipboardBytes(packet);
                    Utility.SendBytes(socket, toSend, toSend.Length, SocketFlags.None);
                }while (bytesToSend > 0);
                Console.WriteLine("trasferimento terminato");
            }
            catch (Exception)
            {
                Console.WriteLine("Errore nel trasferimento");
            }
            finally
            {
                fileStream.Close();
            }
        }
예제 #9
0
        private static void listenClipboard()
        {
            try
            {
                listener.Listen(0);
                client = listener.Accept();
            }
            catch (Exception e)
            {
                listener.Close();
                return;
            }

            byte[] buffer = new byte[BYTES_PER_TRANSFER];

            while (true)
            {
                try
                {
                    ReceivePacket(ref client, ref buffer);

                    ClipboardPacket packet = Serialization.FromClipboardBytes(buffer);

                    switch ((ClipboardPacketType)packet.type)
                    {
                    case ClipboardPacketType.TEXT:
                        Clipboard.SetText(Encoding.Unicode.GetString(packet.data));
                        break;

                    case ClipboardPacketType.DIRECTORY:
                        Clipboard.Clear();
                        string dir = TEMP_DIR + packet.name;
                        Directory.CreateDirectory(dir);
                        break;

                    case ClipboardPacketType.FILE:
                        string file = TEMP_DIR + packet.name;

                        lock (objLock)
                        {
                            while (currentDownload != null && !packet.name.Equals(currentFileName))
                            {
                                Monitor.Wait(objLock);
                            }
                            if (currentDownload == null)
                            {
                                currentFileName = packet.name;
                                currentDownload = new AsynchFileReceiver(file, packet.totalLength);
                                Console.WriteLine("START " + file);
                                currentDownload.Start();
                            }
                            currentDownload.newFragmentAvailable(ref packet.data);
                        }
                        break;

                    case ClipboardPacketType.BITMAP:
                        lock (objLock)
                        {
                            if (bitmapStream == null)
                            {
                                bitmapStream = new MemoryStream((int)packet.totalLength);
                            }
                            bitmapStream.Write(packet.data, 0, packet.data.Length);
                            if (bitmapStream.Position == packet.totalLength)
                            {
                                var bitmap = new BitmapImage();
                                bitmap.BeginInit();
                                bitmap.StreamSource = bitmapStream;
                                bitmap.CacheOption  = BitmapCacheOption.OnLoad;
                                bitmap.EndInit();
                                bitmap.Freeze();
                                Clipboard.SetImage(bitmap);
                                bitmapStream.Dispose();
                                bitmapStream = null;
                            }
                        }
                        break;

                    case ClipboardPacketType.UPDATE:
                        SendClipboardNotice(Clipboard.GetDataObject());
                        break;

                    case ClipboardPacketType.SET_DROPLIST:
                        string           fileNames  = Encoding.Unicode.GetString(packet.data);
                        string[]         filesArray = fileNames.Split('|');
                        StringCollection sc         = new StringCollection();
                        foreach (string s in filesArray)
                        {
                            sc.Add(TEMP_DIR + s);
                        }
                        try
                        {
                            Thread t = new Thread(new ThreadStart(() =>
                            {
                                Clipboard.SetFileDropList(sc);
                            }
                                                                  ));
                            t.SetApartmentState(ApartmentState.STA);
                            t.Start();
                        }
                        catch (Exception e)
                        {
                        }
                        break;
                    }
                }
                catch (SocketException)
                {
                    break;
                }
                catch (ObjectDisposedException)
                {
                    break;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    break;
                }
            }
            client.Close();
            listener.Close();
        }