Skip to content

Commit

Permalink
DEVOPS-10175 smartptr connection pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
ipalenov committed Jun 5, 2024
1 parent 10bb619 commit c20cf4f
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions src/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Connection::Connection(const AMQP::Address& address, int timeout): timeout(timeo

Connection::~Connection() {
delete pimpl;
pimpl = nullptr;
}

void Connection::connect() {
Expand Down
16 changes: 10 additions & 6 deletions src/linux/ConnectionImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ ConnectionImpl::ConnectionImpl(const AMQP::Address& address) :
sslInited = true;
}
eventLoop = event_base_new();
handler = new TCPHandler(eventLoop);
connection = new AMQP::TcpConnection(handler, address);
handler.reset(new TCPHandler(eventLoop));
connection.reset(new AMQP::TcpConnection(handler.get(), address));
thread = std::thread(ConnectionImpl::loopThread, this);
}

Expand All @@ -25,16 +25,20 @@ ConnectionImpl::~ConnectionImpl() {
}
event_base_loopbreak(eventLoop);
thread.join();
delete connection;
delete handler;
connection.reset(nullptr);
handler.reset(nullptr);

event_base_free(eventLoop);
}

void ConnectionImpl::loopThread(ConnectionImpl* thiz) {
event_base* loop = thiz->eventLoop;
while(!thiz->connection->closed()) {
event_base_loop(loop, EVLOOP_NONBLOCK);
try{
event_base_loop(loop, EVLOOP_NONBLOCK);
}catch(std::exception& ex){
Biterp::Logging::error("Channel loop error: " + std::string(ex.what()));
}
}
}

Expand All @@ -50,7 +54,7 @@ void ConnectionImpl::openChannel(std::unique_ptr<AMQP::TcpChannel>& channel) {
std::condition_variable cv;
bool ready = false;

channel.reset(new AMQP::TcpChannel(connection));
channel.reset(new AMQP::TcpChannel(connection.get()));
channel->onReady([&]() {
std::unique_lock<std::mutex> lock(m);
ready = true;
Expand Down
4 changes: 2 additions & 2 deletions src/linux/ConnectionImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ class ConnectionImpl{

private:
event_base* eventLoop;
TCPHandler* handler;
AMQP::TcpConnection* connection;
std::unique_ptr<TCPHandler> handler;
std::unique_ptr<AMQP::TcpConnection> connection;

std::unique_ptr<AMQP::TcpChannel> trChannel;
std::thread thread;
Expand Down
8 changes: 4 additions & 4 deletions src/windows/ConnectionImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ ConnectionImpl::ConnectionImpl(const AMQP::Address& address) :
handler(address.hostname(), address.port(), address.secure()),
trChannel(nullptr)
{
connection = new AMQP::Connection(&handler, address.login(), address.vhost());
handler.setConnection(connection);
connection.reset(new AMQP::Connection(&handler, address.login(), address.vhost()));
handler.setConnection(connection.get());
thread = std::thread(SimplePocoHandler::loopThread, &handler);
}

Expand All @@ -16,7 +16,7 @@ ConnectionImpl::~ConnectionImpl() {
connection->close();
}
thread.join();
delete connection;
connection.reset(nullptr);
}

void ConnectionImpl::openChannel(std::unique_ptr<AMQP::Channel>& channel) {
Expand All @@ -29,7 +29,7 @@ void ConnectionImpl::openChannel(std::unique_ptr<AMQP::Channel>& channel) {
std::mutex m;
std::condition_variable cv;
bool ready = false;
channel.reset(new AMQP::Channel(connection));
channel.reset(new AMQP::Channel(connection.get()));
channel->onReady([&]() {
std::unique_lock<std::mutex> lock(m);
ready = true;
Expand Down
2 changes: 1 addition & 1 deletion src/windows/ConnectionImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ConnectionImpl{

private:
SimplePocoHandler handler;
AMQP::Connection* connection;
std::unique_ptr<AMQP::Connection> connection;
std::unique_ptr<AMQP::Channel> trChannel;
std::unique_ptr<AMQP::Channel> rcChannel;
std::thread thread;
Expand Down

0 comments on commit c20cf4f

Please sign in to comment.