static internal void downloadCompleted(AsynchFileReceiver download) { lock (objLock) { currentFileName = null; if (download == currentDownload) { currentDownload = null; } if (download == bitmapDownload) { bitmapDownload = null; } Monitor.PulseAll(objLock); } }
private static void listenClipboard() { try { listener.Listen(0); client = listener.Accept(); } catch (Exception) { listener.Close(); return; } byte[] rawPacket = new byte[ClipboardConstants.PacketLength]; while (true) { try { if (!Utility.ReceiveBytes(client, rawPacket, rawPacket.Length, SocketFlags.None)) { break; } ClipboardPacket packet = Serialization.FromClipboardBytes(rawPacket); ClipboardPacketType packetType = (ClipboardPacketType)packet.type; switch (packetType) { case ClipboardPacketType.TEXT: Clipboard.SetText(System.Text.Encoding.Unicode.GetString(packet.payload)); break; case ClipboardPacketType.DIRECTORY: Clipboard.Clear(); string dir = TemporaryDirectory + packet.name; Directory.CreateDirectory(dir); break; case ClipboardPacketType.FILE: string file = TemporaryDirectory + 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.payload); } break; case ClipboardPacketType.BITMAP: lock (objLock) { if (bitmapStream == null) { bitmapStream = new MemoryStream((int)packet.totalLength); } bitmapStream.Write(packet.payload, 0, packet.payload.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: ClipboardTrasfer.SendClipboardNotice(Clipboard.GetDataObject()); break; case ClipboardPacketType.SET_DROPLIST: string fileNames = Encoding.Unicode.GetString(packet.payload); string[] filesArray = fileNames.Split('|'); StringCollection sc = new StringCollection(); foreach (string s in filesArray) { sc.Add(TemporaryDirectory + s); } try { Thread t = new Thread(new ThreadStart(() => { Clipboard.SetFileDropList(sc); })); t.SetApartmentState(ApartmentState.STA); t.Start(); } catch (Exception) { } break; } } catch (SocketException se) { if (NetworkErrorOccured != null) { NetworkErrorOccured(se); } //break; } catch (ObjectDisposedException) { break; } catch (Exception ex) { Console.WriteLine(ex.Message); break; } } client.Close(); listener.Close(); }