12#ifndef NETWORK_CORE_TCP_LISTEN_H
13#define NETWORK_CORE_TCP_LISTEN_H
16#include "../network.h"
17#include "../network_func.h"
18#include "../network_internal.h"
19#include "../../debug.h"
21#include "table/strings.h"
29template <
class Tsocket, PacketType Tfull_packet, PacketType Tban_packet>
40 Packet p(
nullptr, Tban_packet);
43 Debug(net, 2,
"[{}] Banned ip tried to join ({}), refused", Tsocket::GetName(), entry);
54 if (!Tsocket::AllowConnection()) {
57 Packet p(
nullptr, Tfull_packet);
78 struct sockaddr_storage sin{};
79 socklen_t sin_len =
sizeof(sin);
80 SOCKET s = accept(ls, (
struct sockaddr*)&sin, &sin_len);
81 if (s == INVALID_SOCKET)
return;
83 sin_len = FixAddrLenForEmscripten(sin);
93 if (!Tsocket::ValidateClient(s, address))
continue;
94 Tsocket::AcceptConnection(s, address);
104 fd_set read_fd, write_fd;
111 for (Tsocket *cs : Tsocket::Iterate()) {
112 FD_SET(cs->sock, &read_fd);
113 FD_SET(cs->sock, &write_fd);
118 FD_SET(s.first, &read_fd);
121 tv.tv_sec = tv.tv_usec = 0;
122 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
130 for (Tsocket *cs : Tsocket::Iterate()) {
131 cs->writable = !!FD_ISSET(cs->sock, &write_fd);
132 if (FD_ISSET(cs->sock, &read_fd)) {
133 cs->ReceivePackets();
152 address.Listen(SOCK_STREAM, &
sockets);
156 Debug(net, 0,
"Could not start network: could not create listening socket");
157 ShowNetworkError(STR_NETWORK_ERROR_SERVER_START);
168 closesocket(s.first);
171 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...
const std::string & GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
bool IsInNetmask(std::string_view netmask)
Checks whether this IP address is contained by the given netmask.
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,...)
Output 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 SetNoDelay(SOCKET d)
Try to set the socket to not delay sending.
bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
Internal entity of a packet.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
ssize_t TransferOut(F transfer_function)
Transfer data from the packet to the given function.
IPv4 addresses should be 4 bytes.
Basic functions to receive and send TCP packets.