1 #ifndef BLANK_NET_TCP_HPP_
2 #define BLANK_NET_TCP_HPP_
13 /// all failing functions throw NetError
17 /// create an empty socket that is not connected to anything
19 /// create TCP socket bound to given port
20 explicit Socket(unsigned short port);
21 /// connect to given host:port
22 Socket(const std::string &host, unsigned short port);
24 /// wrap given SDLNet TCP socket
25 /// for use with Accept()
26 explicit Socket(TCPsocket sock);
30 Socket(const Socket &) = delete;
31 Socket &operator =(const Socket &) = delete;
33 Socket(Socket &&) noexcept;
34 Socket &operator =(Socket &&) noexcept;
36 explicit operator bool() const noexcept { return sock; }
38 bool operator ==(const Socket &other) const noexcept {
39 return sock == other.sock;
41 bool operator <(const Socket &other) const noexcept {
42 return sock < other.sock;
46 /// create a socket for an incoming connection
47 /// @return an empty socket if there are none
48 Socket Accept() noexcept;
49 /// check if there is data available to read
50 bool Ready() const noexcept;
51 /// receive data into given buffer
52 /// @return number of bytes read, at most max_len
53 /// non-blocking if Ready() is true
54 std::size_t Recv(void *buf, std::size_t max_len);
55 /// send data from given buffer, at most max_len bytes
56 /// @return number of bytes written
57 /// may be less than len as soon as I get to
58 /// making it non-blocking
59 std::size_t Send(const void *buf, std::size_t max_len);
61 int AddTo(SDLNet_SocketSet);
62 int RemoveFrom(SDLNet_SocketSet);
72 virtual ~IOHandler() = default;
74 void Close() noexcept { closed = true; }
75 bool Closed() const noexcept { return closed; }
77 virtual void OnCreate(Socket &) { }
78 virtual void OnRemove(Socket &) noexcept { }
80 virtual void OnSend(Socket &) { };
81 virtual void OnRecv(Socket &) { };
82 virtual void OnError(Socket &) noexcept { Close(); }
93 using ConnectionSet = std::list<std::pair<Socket, IOHandler *>>;
96 explicit Pool(int max_conn = 32, std::size_t buf_siz = 1500);
99 Pool(const Pool &) = delete;
100 Pool &operator =(const Pool &) = delete;
103 void AddConnection(Socket, IOHandler *);
105 bool Check(unsigned long timeout);
109 int FreeSlots() const noexcept { return max_conn - use_conn; }
110 int OccupiedSlots() const noexcept { return use_conn; }
111 int TotalSlots() const noexcept { return max_conn; }
112 /// reallocate the pool to accomodate at least new_max sockets
113 void Resize(int new_max);
116 SDLNet_SocketSet set;
117 ConnectionSet connections;