Program Listing for File Network.hpp

Return to documentation for file (gui/Network/Network.hpp)

#pragma once

#include <arpa/inet.h>
#include <sys/poll.h>
#include <sys/socket.h>

#include "API/API.hpp"

#include <array>
#include <memory>
#include <thread>

class Network {
private:
  int _port;
  const std::string _hostname;
  sockaddr_in serverAddr;
  int _fdServer;
  std::shared_ptr<API> _api = nullptr;
  std::thread _networkThread;
  std::array<int, 2> _pipeFdExit;
  std::array<pollfd, 3> _pollInFd;
  std::array<pollfd, 1> _pollOutFd;

  void RunNetworkInternal();

  void ServerHandshake();

public:
  Network(int port, std::string hostname, std::shared_ptr<API> &data);
  ~Network();

  void Run();

  void SendMessage(const std::string &msg);

  [[nodiscard]] std::string ReceiveMessage() const;

  void RequestStop();
};