Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
camargo2019 committed Sep 10, 2024
2 parents a36c576 + 03d7533 commit cb11884
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 13 additions & 1 deletion core/socket/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@

CoreSocket::CoreSocket(boost::asio::io_context& io_context, std::string ip, short port, Cache& cache_)
: acceptor_(io_context, boost::asio::ip::tcp::endpoint(boost::asio::ip::make_address(ip), port)),
cache_(cache_), client_(0) {
cache_(cache_), client_(0), socket_(io_context) {
accept();
ping();
}

void CoreSocket::accept() {
Expand All @@ -47,3 +48,14 @@ void CoreSocket::accept() {
);
}

void CoreSocket::ping() {
std::thread([this](){
while (true) {
if (socket_.is_open()) {
boost::asio::write(socket_, boost::asio::buffer(std::string("PING\r\n")));
}

std::this_thread::sleep_for(std::chrono::seconds(5));
}
}).detach();
}
4 changes: 3 additions & 1 deletion core/socket/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@

private:
void accept();
void ping();

boost::asio::ip::tcp::acceptor acceptor_;
Cache& cache_;
std::atomic<int> client_;
boost::asio::ip::tcp::socket socket_;
boost::asio::ip::tcp::acceptor acceptor_;
ConfigConnect ConfigConn = Config::getConfigConnect();
};

Expand Down

0 comments on commit cb11884

Please sign in to comment.