ClientServerProject/project/client.h
2021-02-11 02:26:38 -05:00

33 lines
1.0 KiB
C++

#pragma once
#include "../libraries/event.h"
#include "networked.h"
#include <thread>
class client : public networked
{
auto accept_message() -> void //add const later
{
while(is_valid()) /* Accept all messages */
{
auto message = receive_message();
if (message.empty() && is_valid()) //connection is closed or broken.
{
disconnect();
break;
}
message_received.execute(message);
}
}
public:
event<std::string&> message_received {};
explicit client() : networked(net::socket(AF_INET, SOCK_STREAM, 0)){}
auto connect(const std::string& ip, const port port) -> std::thread
{
const auto address = net::getaddrinfo(ip, std::to_string(port));
auto remote_address = net::getaddrinfo(ip, std::to_string(port));
if (net::connect(socket, remote_address) < 0)
return std::thread();
auto thread = std::thread(&client::accept_message, this);
return thread;
}
};