|
| 1 | +// example.ThreadSafe_Socket.cpp — one SocketClient instance per thread |
| 2 | +#include <cstdlib> |
| 3 | +#include <iostream> |
| 4 | +#include <string> |
| 5 | +#include <thread> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +#include "deathbycaptcha/deathbycaptcha.hpp" |
| 9 | + |
| 10 | +namespace { |
| 11 | +std::string getenv_or_empty(const char* key) { |
| 12 | + const char* v = std::getenv(key); |
| 13 | + return v ? std::string(v) : std::string{}; |
| 14 | +} |
| 15 | + |
| 16 | +void run_socket_worker(const std::string& username, |
| 17 | + const std::string& password, |
| 18 | + const std::string& image_path, |
| 19 | + bool decode_mode, |
| 20 | + int worker_id) { |
| 21 | + // Each worker owns its own client instance. |
| 22 | + dbc::SocketClient client(username, password); |
| 23 | + client.is_verbose = false; |
| 24 | + |
| 25 | + try { |
| 26 | + if (!decode_mode) { |
| 27 | + std::cout << "[SOCKET worker " << worker_id |
| 28 | + << "] balance: " << client.get_balance() << " US cents\n"; |
| 29 | + } else { |
| 30 | + auto result = client.decode(image_path, 120); |
| 31 | + if (result && result->text.has_value()) { |
| 32 | + std::cout << "[SOCKET worker " << worker_id << "] solved captcha " |
| 33 | + << result->captcha << ": " << *result->text << '\n'; |
| 34 | + } else { |
| 35 | + std::cout << "[SOCKET worker " << worker_id |
| 36 | + << "] timeout/no result\n"; |
| 37 | + } |
| 38 | + } |
| 39 | + } catch (const std::exception& e) { |
| 40 | + std::cerr << "[SOCKET worker " << worker_id << "] error: " << e.what() |
| 41 | + << '\n'; |
| 42 | + } |
| 43 | +} |
| 44 | +} // namespace |
| 45 | + |
| 46 | +int main() { |
| 47 | + const std::string username = getenv_or_empty("DBC_USERNAME"); |
| 48 | + const std::string password = getenv_or_empty("DBC_PASSWORD"); |
| 49 | + const std::string image_path = getenv_or_empty("DBC_IMAGE_PATH"); |
| 50 | + const std::string threads_env = getenv_or_empty("DBC_THREADS"); |
| 51 | + |
| 52 | + if (username.empty() || password.empty()) { |
| 53 | + std::cerr << "Set DBC_USERNAME and DBC_PASSWORD before running.\n"; |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | + int threads_count = 2; |
| 58 | + if (!threads_env.empty()) { |
| 59 | + try { |
| 60 | + threads_count = std::stoi(threads_env); |
| 61 | + } catch (...) { |
| 62 | + std::cerr << "Invalid DBC_THREADS value. Use a positive integer.\n"; |
| 63 | + return 1; |
| 64 | + } |
| 65 | + } |
| 66 | + if (threads_count <= 0) { |
| 67 | + std::cerr << "DBC_THREADS must be greater than zero.\n"; |
| 68 | + return 1; |
| 69 | + } |
| 70 | + |
| 71 | + const bool decode_mode = !image_path.empty(); |
| 72 | + |
| 73 | + // Main thread sanity check in normal usage. |
| 74 | + try { |
| 75 | + dbc::SocketClient sanity_client(username, password); |
| 76 | + sanity_client.is_verbose = false; |
| 77 | + std::cout << "[SOCKET main] initial balance: " |
| 78 | + << sanity_client.get_balance() << " US cents\n"; |
| 79 | + } catch (const std::exception& e) { |
| 80 | + std::cerr << "[SOCKET main] error: " << e.what() << '\n'; |
| 81 | + return 1; |
| 82 | + } |
| 83 | + |
| 84 | + std::vector<std::thread> workers; |
| 85 | + workers.reserve(static_cast<std::size_t>(threads_count)); |
| 86 | + |
| 87 | + for (int i = 0; i < threads_count; ++i) { |
| 88 | + workers.emplace_back(run_socket_worker, |
| 89 | + username, |
| 90 | + password, |
| 91 | + image_path, |
| 92 | + decode_mode, |
| 93 | + i + 1); |
| 94 | + } |
| 95 | + |
| 96 | + for (auto& t : workers) t.join(); |
| 97 | + return 0; |
| 98 | +} |
0 commit comments