Commit 9ed23474 authored by hyperxor's avatar hyperxor

Remove friend relationship in Connection class

parent 54e2bc98
...@@ -27,13 +27,10 @@ ...@@ -27,13 +27,10 @@
namespace Pistache { namespace Pistache {
namespace Http { namespace Http {
class ConnectionPool;
class Transport; class Transport;
struct Connection : public std::enable_shared_from_this<Connection> { struct Connection : public std::enable_shared_from_this<Connection> {
friend class ConnectionPool;
using OnDone = std::function<void()>; using OnDone = std::function<void()>;
Connection(); Connection();
...@@ -58,6 +55,8 @@ struct Connection : public std::enable_shared_from_this<Connection> { ...@@ -58,6 +55,8 @@ struct Connection : public std::enable_shared_from_this<Connection> {
void connect(const Address &addr); void connect(const Address &addr);
void close(); void close();
bool isIdle() const; bool isIdle() const;
bool tryUse();
void setAsIdle();
bool isConnected() const; bool isConnected() const;
bool hasTransport() const; bool hasTransport() const;
void associateTransport(const std::shared_ptr<Transport> &transport); void associateTransport(const std::shared_ptr<Transport> &transport);
......
...@@ -425,6 +425,16 @@ bool Connection::isIdle() const { ...@@ -425,6 +425,16 @@ bool Connection::isIdle() const {
Connection::State::Idle; Connection::State::Idle;
} }
bool Connection::tryUse() {
auto curState = static_cast<uint32_t>(Connection::State::Idle);
auto newState = static_cast<uint32_t>(Connection::State::Used);
return state_.compare_exchange_strong(curState, newState);
}
void Connection::setAsIdle() {
state_.store(static_cast<uint32_t>(Connection::State::Idle));
}
bool Connection::isConnected() const { bool Connection::isConnected() const {
return connectionState_.load() == Connected; return connectionState_.load() == Connected;
} }
...@@ -583,10 +593,7 @@ ConnectionPool::pickConnection(const std::string &domain) { ...@@ -583,10 +593,7 @@ ConnectionPool::pickConnection(const std::string &domain) {
} }
for (auto &conn : pool) { for (auto &conn : pool) {
auto &state = conn->state_; if (conn->tryUse()) {
auto curState = static_cast<uint32_t>(Connection::State::Idle);
auto newState = static_cast<uint32_t>(Connection::State::Used);
if (state.compare_exchange_strong(curState, newState)) {
return conn; return conn;
} }
} }
...@@ -596,7 +603,7 @@ ConnectionPool::pickConnection(const std::string &domain) { ...@@ -596,7 +603,7 @@ ConnectionPool::pickConnection(const std::string &domain) {
void ConnectionPool::releaseConnection( void ConnectionPool::releaseConnection(
const std::shared_ptr<Connection> &connection) { const std::shared_ptr<Connection> &connection) {
connection->state_.store(static_cast<uint32_t>(Connection::State::Idle)); connection->setAsIdle();
} }
size_t ConnectionPool::usedConnections(const std::string &domain) const { size_t ConnectionPool::usedConnections(const std::string &domain) const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment