Commit aa532768 authored by louiz’'s avatar louiz’

Keep a "connected" state in the SocketHandler class

parent 483e1702
...@@ -50,7 +50,6 @@ IrcClient* Bridge::get_irc_client(const std::string& hostname, const std::string ...@@ -50,7 +50,6 @@ IrcClient* Bridge::get_irc_client(const std::string& hostname, const std::string
this->irc_clients.emplace(hostname, std::make_shared<IrcClient>(hostname, username, this)); this->irc_clients.emplace(hostname, std::make_shared<IrcClient>(hostname, username, this));
std::shared_ptr<IrcClient> irc = this->irc_clients.at(hostname); std::shared_ptr<IrcClient> irc = this->irc_clients.at(hostname);
this->poller->add_socket_handler(irc); this->poller->add_socket_handler(irc);
irc->start();
return irc.get(); return irc.get();
} }
} }
......
...@@ -128,6 +128,8 @@ void IrcClient::send_quit_command() ...@@ -128,6 +128,8 @@ void IrcClient::send_quit_command()
void IrcClient::send_join_command(const std::string& chan_name) void IrcClient::send_join_command(const std::string& chan_name)
{ {
if (!this->connected)
this->start();
if (this->welcomed == false) if (this->welcomed == false)
this->channels_to_join.push_back(chan_name); this->channels_to_join.push_back(chan_name);
else else
......
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
#include <iostream> #include <iostream>
SocketHandler::SocketHandler(): SocketHandler::SocketHandler():
poller(nullptr) poller(nullptr),
connected(false)
{ {
if ((this->socket = ::socket(AF_INET, SOCK_STREAM, 0)) == -1) if ((this->socket = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
throw std::runtime_error("Could not create socket"); throw std::runtime_error("Could not create socket");
...@@ -46,6 +47,7 @@ bool SocketHandler::connect(const std::string& address, const std::string& port) ...@@ -46,6 +47,7 @@ bool SocketHandler::connect(const std::string& address, const std::string& port)
if (::connect(this->socket, rp->ai_addr, rp->ai_addrlen) == 0) if (::connect(this->socket, rp->ai_addr, rp->ai_addrlen) == 0)
{ {
log_info("Connection success."); log_info("Connection success.");
this->connected = true;
this->on_connected(); this->on_connected();
return true; return true;
} }
...@@ -99,8 +101,12 @@ void SocketHandler::on_send() ...@@ -99,8 +101,12 @@ void SocketHandler::on_send()
void SocketHandler::close() void SocketHandler::close()
{ {
this->connected = false;
this->poller->remove_socket_handler(this->get_socket()); this->poller->remove_socket_handler(this->get_socket());
::close(this->socket); ::close(this->socket);
// recreate the socket for a potential future usage
if ((this->socket = ::socket(AF_INET, SOCK_STREAM, 0)) == -1)
throw std::runtime_error("Could not create socket");
} }
socket_t SocketHandler::get_socket() const socket_t SocketHandler::get_socket() const
...@@ -116,3 +122,8 @@ void SocketHandler::send_data(std::string&& data) ...@@ -116,3 +122,8 @@ void SocketHandler::send_data(std::string&& data)
this->poller->watch_send_events(this); this->poller->watch_send_events(this);
} }
} }
bool SocketHandler::is_connected() const
{
return this->connected;
}
...@@ -61,6 +61,7 @@ public: ...@@ -61,6 +61,7 @@ public:
* should be truncated, only the unused data should be left untouched. * should be truncated, only the unused data should be left untouched.
*/ */
virtual void parse_in_buffer() = 0; virtual void parse_in_buffer() = 0;
bool is_connected() const;
protected: protected:
socket_t socket; socket_t socket;
...@@ -84,6 +85,7 @@ protected: ...@@ -84,6 +85,7 @@ protected:
* (actually it is sharing our ownership with a Bridge). * (actually it is sharing our ownership with a Bridge).
*/ */
Poller* poller; Poller* poller;
bool connected;
private: private:
SocketHandler(const SocketHandler&) = delete; SocketHandler(const SocketHandler&) = delete;
......
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