| 43 | } |
| 44 | |
| 45 | void Connection::writePacket(const Packet& packet) { |
| 46 | while (true) { |
| 47 | { |
| 48 | lock_guard<std::recursive_mutex> guard(connectionMutex); |
| 49 | if (shuttingDown) { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | bool success = write(packet); |
| 54 | if (success) { |
| 55 | return; |
| 56 | } |
| 57 | bool hasConnection; |
| 58 | { |
| 59 | lock_guard<std::recursive_mutex> guard(connectionMutex); |
| 60 | hasConnection = (socketFd != -1); |
| 61 | } |
| 62 | |
| 63 | // Yield the processor |
| 64 | if (hasConnection) { |
| 65 | // Have a connection, sleep for 1ms |
| 66 | std::this_thread::sleep_for(std::chrono::microseconds(1000)); |
| 67 | } else { |
| 68 | // No connection, sleep for 100ms |
| 69 | std::this_thread::sleep_for(std::chrono::microseconds(100 * 1000)); |
| 70 | } |
| 71 | LOG_EVERY_N(1000, INFO) << "Waiting to write..."; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void Connection::closeSocket() { |
| 76 | lock_guard<std::recursive_mutex> guard(connectionMutex); |