12 #ifndef NETWORK_CORE_TCP_LISTEN_H
13 #define NETWORK_CORE_TCP_LISTEN_H
16 #include "../network.h"
17 #include "../../core/pool_type.hpp"
18 #include "../../debug.h"
19 #include "table/strings.h"
27 template <
class Tsocket, PacketType Tfull_packet, PacketType Tban_packet>
38 Packet p(
nullptr, Tban_packet);
41 Debug(net, 2,
"[{}] Banned ip tried to join ({}), refused", Tsocket::GetName(), entry);
52 if (!Tsocket::AllowConnection()) {
55 Packet p(
nullptr, Tfull_packet);
76 struct sockaddr_storage sin;
77 memset(&sin, 0,
sizeof(sin));
78 socklen_t sin_len =
sizeof(sin);
79 SOCKET s = accept(ls, (
struct sockaddr*)&sin, &sin_len);
80 if (s == INVALID_SOCKET)
return;
82 sin_len = FixAddrLenForEmscripten(sin);
92 if (!Tsocket::ValidateClient(s, address))
continue;
93 Tsocket::AcceptConnection(s, address);
103 fd_set read_fd, write_fd;
110 for (Tsocket *cs : Tsocket::Iterate()) {
111 FD_SET(cs->sock, &read_fd);
112 FD_SET(cs->sock, &write_fd);
117 FD_SET(s.first, &read_fd);
120 tv.tv_sec = tv.tv_usec = 0;
121 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
129 for (Tsocket *cs : Tsocket::Iterate()) {
130 cs->writable = !!FD_ISSET(cs->sock, &write_fd);
131 if (FD_ISSET(cs->sock, &read_fd)) {
132 cs->ReceivePackets();
151 address.Listen(SOCK_STREAM, &
sockets);
155 Debug(net, 0,
"Could not start network: could not create listening socket");
156 ShowNetworkError(STR_NETWORK_ERROR_SERVER_START);
167 closesocket(s.first);
170 Debug(net, 5,
"[{}] Closed listeners", Tsocket::GetName());
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
std::map< SOCKET, NetworkAddress > SocketList
Type for a mapping between address and socket.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
bool IsInNetmask(const std::string &netmask)
Checks whether this IP address is contained by the given netmask.
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
static NetworkError GetLast()
Get the last network error.
Template for TCP listeners.
static bool Receive()
Handle the receiving of packets.
static void AcceptClient(SOCKET ls)
Accepts clients from the sockets.
static SocketList sockets
List of sockets we listen on.
static bool Listen(uint16_t port)
Listen on a particular port.
static void CloseListeners()
Close the sockets we're listening on.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
uint32_t _frame_counter
The current frame.
bool _networking
are we in networking mode?
StringList _network_ban_list
The banned clients.
void GetBindAddresses(NetworkAddressList *addresses, uint16_t port)
Get the addresses to bind to.
bool SetNonBlocking([[maybe_unused]] SOCKET d)
Try to set the socket into non-blocking mode.
bool SetNoDelay([[maybe_unused]] SOCKET d)
Try to set the socket to not delay sending.
Internal entity of a packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
ssize_t TransferOut(F transfer_function, D destination, Args &&... args)
Transfer data from the packet to the given function.
Basic functions to receive and send TCP packets.