summaryrefslogtreecommitdiff
path: root/louloulibs/network/tcp_server_socket.hpp
blob: a54d3bf3c048ebafbc573725b278ce12c59119f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once

#include <network/socket_handler.hpp>
#include <network/resolver.hpp>
#include <network/poller.hpp>

#include <string>

#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/ip.h>

#include <logger/logger.hpp>

#include <cstring>
#include <cassert>

using namespace std::string_literals;

template <typename RemoteSocketType>
class TcpSocketServer: public SocketHandler
{
 public:
  TcpSocketServer(std::shared_ptr<Poller> poller, const uint16_t port):
      SocketHandler(poller, -1)
  {
    if ((this->socket = ::socket(AF_INET6, SOCK_STREAM, 0)) == -1)
      throw std::runtime_error("Could not create socket: "s + std::strerror(errno));

    int opt = 1;
    if (::setsockopt(this->socket, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) == -1)
      throw std::runtime_error("Failed to set socket option: "s + std::strerror(errno));

    struct sockaddr_in6 addr{};
    addr.sin6_family = AF_INET6;
    addr.sin6_port = htons(port);
    addr.sin6_addr = IN6ADDR_ANY_INIT;
    if ((::bind(this->socket, (const struct sockaddr*)&addr, sizeof(addr))) == -1)
      { // If we can’t listen on this port, we just give up, but this is not fatal.
        log_warning("Failed to bind on port ", std::to_string(port), ": ", std::strerror(errno));
        return;
      }

    if ((::listen(this->socket, 10)) == -1)
      throw std::runtime_error("listen() failed");

    this->accept();
  }
  ~TcpSocketServer() = default;

  void on_recv() override
  {
    // Accept a RemoteSocketType
    int socket = ::accept(this->socket, nullptr, nullptr);

    auto client = std::make_unique<RemoteSocketType>(poller, socket, *this);
    this->poller->add_socket_handler(client.get());
    this->sockets.push_back(std::move(client));
  }

 protected:
  std::vector<std::unique_ptr<RemoteSocketType>> sockets;

 private:
  void accept()
  {
    this->poller->add_socket_handler(this);
  }
  void on_send() override
  {
    assert(false);
  }
  void connect() override
  {
    assert(false);
  }
  bool is_connected() const override
  {
    return true;
  }
  bool is_connecting() const override
  {
    return false;
  }
};