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 "../../core/pool_type.hpp"
20#include "../../debug.h"
22#include "table/strings.h"
30template <
class Tsocket, PacketType Tfull_packet, PacketType Tban_packet>
41 Packet p(
nullptr, Tban_packet);
44 Debug(net, 2,
"[{}] Banned ip tried to join ({}), refused", Tsocket::GetName(), entry);
55 if (!Tsocket::AllowConnection()) {
58 Packet p(
nullptr, Tfull_packet);
79 struct sockaddr_storage sin{};
80 socklen_t sin_len =
sizeof(sin);
81 SOCKET s = accept(ls, (
struct sockaddr*)&sin, &sin_len);
82 if (s == INVALID_SOCKET)
return;
84 sin_len = FixAddrLenForEmscripten(sin);
94 if (!Tsocket::ValidateClient(s, address))
continue;
95 Tsocket::AcceptConnection(s, address);
105 fd_set read_fd, write_fd;
112 for (Tsocket *cs : Tsocket::Iterate()) {
113 FD_SET(cs->sock, &read_fd);
114 FD_SET(cs->sock, &write_fd);
119 FD_SET(s.first, &read_fd);
122 tv.tv_sec = tv.tv_usec = 0;
123 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
131 for (Tsocket *cs : Tsocket::Iterate()) {
132 cs->writable = !!FD_ISSET(cs->sock, &write_fd);
133 if (FD_ISSET(cs->sock, &read_fd)) {
134 cs->ReceivePackets();
153 address.Listen(SOCK_STREAM, &
sockets);
157 Debug(net, 0,
"Could not start network: could not create listening socket");
158 ShowNetworkError(STR_NETWORK_ERROR_SERVER_START);
169 closesocket(s.first);
172 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.