private void EmitFiles(NetworkStream stream) { EmitBytesDone = false; EmitBytes.Clear(); ThreadPool.QueueUserWorkItem((x) => EmitFilesRead(clip_data as FileBuffer)); while (!EmitBytesDone) { while (EmitBytes.Count > 0) { EmitBytes.Dequeue(stream); } } }
void ThreadEmitter() { while (true) { Thread.Sleep(50); if (!clip_dirty) { continue; } clip_dirty = false; Log(NotificationType.Sending); for (int i = 0; i < clients.Count; i++) { var tcp = clients[i]; var ok = true; if (config.Solo && soloIndex != i) { tcp.Log = "..."; continue; } try { var task = (tcp.ConnectAsync(tcp.Name, config.Port)); tcp.Log = (ok = task.Wait(1000)) ? "Sent" : "Failed"; if (ok) { var stream = tcp.GetStream(); BinFormatter.Serialize(stream, clip_data); if (clip_data.Type == DataType.Files) { // The story doesn't end there.. // Send the file buffer EmitFiles(stream); } stream.Close(); EmitBytes.Clean(); } } catch (Exception ex) { ok = false; tcp.Log = "Failed: " + ex.DeepMessage(); } // Close and set the new one tcp.Close(); clients[i] = new NetClient(tcp.Name) { Log = tcp.Log }; } Log(NotificationType.Sent, clip_data); } }