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);
22 /// wrap given SDLNet TCP socket
23 /// for use with Accept()
24 explicit Socket(TCPsocket sock);
28 Socket(const Socket &) = delete;
29 Socket &operator =(const Socket &) = delete;
31 Socket(Socket &&) noexcept;
32 Socket &operator =(Socket &&) noexcept;
34 explicit operator bool() const noexcept { return sock; }
36 bool operator ==(const Socket &other) const noexcept {
37 return sock == other.sock;
39 bool operator <(const Socket &other) const noexcept {
40 return sock < other.sock;
44 /// create a socket for an incoming connection
45 /// @return an empty socket if there are none
46 Socket Accept() noexcept;
47 /// check if there is data available to read
48 bool Ready() const noexcept;
49 /// receive data into given buffer
50 /// @return number of bytes read, at most max_len
51 /// non-blocking if Ready() is true
52 std::size_t Recv(void *buf, std::size_t max_len);
53 /// send data from given buffer, at most max_len bytes
54 /// @return number of bytes written
55 /// may be less than len as soon as I get to
56 /// making it non-blocking
57 std::size_t Send(const void *buf, std::size_t max_len);
59 int AddTo(SDLNet_SocketSet);
60 int RemoveFrom(SDLNet_SocketSet);
70 virtual ~IOHandler() = default;
72 void Close() noexcept { closed = true; }
73 bool Closed() const noexcept { return closed; }
75 virtual void OnCreate(Socket &) { }
76 virtual void OnRemove(Socket &) noexcept { }
78 virtual void OnSend(Socket &) { };
79 virtual void OnRecv(Socket &) { };
80 virtual void OnError(Socket &) noexcept { Close(); }
91 using ConnectionSet = std::list<std::pair<Socket, IOHandler *>>;
94 explicit Pool(int max_conn = 32, std::size_t buf_siz = 1500);
97 Pool(const Pool &) = delete;
98 Pool &operator =(const Pool &) = delete;
101 void AddConnection(Socket, IOHandler *);
103 bool Check(unsigned long timeout);
107 int FreeSlots() const noexcept { return max_conn - use_conn; }
108 int OccupiedSlots() const noexcept { return use_conn; }
109 int TotalSlots() const noexcept { return max_conn; }
110 /// reallocate the pool to accomodate at least new_max sockets
111 void Resize(int new_max);
114 SDLNet_SocketSet set;
116 ConnectionSet connections;