예제 #1
0
 static internal void downloadCompleted(AsynchFileReceiver download)
 {
     lock (objLock)
     {
         currentFileName = null;
         if (download == currentDownload)
         {
             currentDownload = null;
         }
         if (download == bitmapDownload)
         {
             bitmapDownload = null;
         }
         Monitor.PulseAll(objLock);
     }
 }
예제 #2
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();
        }