static void Send(string path) { if (File.Exists(path)) { SendSingleFile(path, ""); } else { if (!Directory.Exists(path)) { logger.Log(LogLevel.Error, "Invalid file/directory path."); return; } string directory = Path.GetDirectoryName(path); string[] filePaths = Directory.GetFiles(path, "*", SearchOption.AllDirectories); bool[] results = new bool[filePaths.Length]; int maxConnections = Constants.MaxConnections; // threadsafe copy Semaphore mutex = new Semaphore(maxConnections, maxConnections); int index = 0; foreach (string filePath in filePaths) { mutex.WaitOne(); int i = index; // copy to avoid race condition DebugTools.ThreadpoolStarter("file send thread", () => { results[i] = SendSingleFile(filePath, directory); mutex.Release(); }); index++; } // sync threads for (int i = 0; i < maxConnections; i++) { mutex.WaitOne(); } // check if any result was false int fails = 0; foreach (bool sendResult in results) { fails += sendResult ? 0 : 1; } if (fails > 0) { logger.Log(LogLevel.Error, "Failed to send {0} out of {1} files!", fails, filePaths.Length); } else { logger.Log(LogLevel.Priority, "All files sent successfully."); } } }
protected virtual void HandleAcceptedSocket(Socket acceptedSocket) { Logger.Log(LogLevel.Information, "Accepted connection from {0} on port {1}", acceptedSocket.RemoteEndPoint, Port); DebugTools.ThreadpoolStarter("Socket init routine", () => RaiseClientConnectedEvent(acceptedSocket)); }
protected override void StartParallelLoops() { base.StartParallelLoops(); DebugTools.ThreadpoolStarter("Reduce loop", ReduceLoop); }