| 138 | } |
| 139 | |
| 140 | void onClientConnection(const TcpConnectionPtr& conn) |
| 141 | { |
| 142 | LOG_TRACE << "Client " << conn->peerAddress().toIpPort() << " -> " |
| 143 | << conn->localAddress().toIpPort() << " is " |
| 144 | << (conn->connected() ? "UP" : "DOWN"); |
| 145 | if (conn->connected()) |
| 146 | { |
| 147 | int id = -1; |
| 148 | { |
| 149 | MutexLockGuard lock(mutex_); |
| 150 | if (!availIds_.empty()) |
| 151 | { |
| 152 | id = availIds_.front(); |
| 153 | availIds_.pop(); |
| 154 | clientConns_[id] = conn; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if (id <= 0) |
| 159 | { |
| 160 | conn->shutdown(); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | conn->setContext(id); |
| 165 | char buf[256]; |
| 166 | snprintf(buf, sizeof(buf), "CONN %d FROM %s IS UP\r\n", id, |
| 167 | conn->peerAddress().toIpPort().c_str()); |
| 168 | sendBackendString(0, buf); |
| 169 | } |
| 170 | } |
| 171 | else |
| 172 | { |
| 173 | if (!conn->getContext().empty()) |
| 174 | { |
| 175 | int id = boost::any_cast<int>(conn->getContext()); |
| 176 | assert(id > 0 && id <= kMaxConns); |
| 177 | char buf[256]; |
| 178 | snprintf(buf, sizeof(buf), "CONN %d FROM %s IS DOWN\r\n", |
| 179 | id, conn->peerAddress().toIpPort().c_str()); |
| 180 | sendBackendString(0, buf); |
| 181 | |
| 182 | MutexLockGuard lock(mutex_); |
| 183 | if (backendConn_) |
| 184 | { |
| 185 | availIds_.push(id); |
| 186 | clientConns_.erase(id); |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | assert(availIds_.empty()); |
| 191 | assert(clientConns_.empty()); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | void onClientMessage(const TcpConnectionPtr& conn, Buffer* buf, Timestamp) |