14 #ifndef NETWORK_CORE_TCP_LISTEN_H 15 #define NETWORK_CORE_TCP_LISTEN_H 18 #include "../network.h" 19 #include "../../core/pool_type.hpp" 20 #include "../../debug.h" 21 #include "table/strings.h" 29 template <
class Tsocket, PacketType Tfull_packet, PacketType Tban_packet>
42 struct sockaddr_storage sin;
43 memset(&sin, 0,
sizeof(sin));
44 socklen_t sin_len =
sizeof(sin);
45 SOCKET s = accept(ls, (
struct sockaddr*)&sin, &sin_len);
46 if (s == INVALID_SOCKET)
return;
63 DEBUG(net, 1,
"[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), entry.c_str());
65 if (send(s, (
const char*)p.
buffer, p.
size, 0) < 0) {
66 DEBUG(net, 0,
"send failed with error %d", GET_LAST_ERROR());
76 if (!Tsocket::AllowConnection()) {
82 if (send(s, (
const char*)p.
buffer, p.
size, 0) < 0) {
83 DEBUG(net, 0,
"send failed with error %d", GET_LAST_ERROR());
90 Tsocket::AcceptConnection(s, address);
100 fd_set read_fd, write_fd;
108 FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
109 FD_SET(cs->sock, &read_fd);
110 FD_SET(cs->sock, &write_fd);
114 for (
auto &s : sockets) {
115 FD_SET(s.second, &read_fd);
118 tv.tv_sec = tv.tv_usec = 0;
119 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
122 for (
auto &s : sockets) {
123 if (FD_ISSET(s.second, &read_fd))
AcceptClient(s.second);
127 FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
128 cs->writable = !!FD_ISSET(cs->sock, &write_fd);
129 if (FD_ISSET(cs->sock, &read_fd)) {
130 cs->ReceivePackets();
143 assert(sockets.size() == 0);
149 address.Listen(SOCK_STREAM, &sockets);
152 if (sockets.size() == 0) {
153 DEBUG(net, 0,
"[server] could not start network: could not create listening socket");
154 NetworkError(STR_NETWORK_ERROR_SERVER_START);
164 for (
auto &s : sockets) {
165 closesocket(s.second);
168 DEBUG(net, 1,
"[%s] closed listeners", Tsocket::GetName());
bool _networking
are we in networking mode?
Internal entity of a packet.
void GetBindAddresses(NetworkAddressList *addresses, uint16 port)
Get the addresses to bind to.
void PrepareToSend()
Writes the packet size from the raw packet from packet->size.
Template for TCP listeners.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
static SocketList sockets
List of sockets we listen on.
std::vector< NetworkAddress > NetworkAddressList
Type for a list of addresses.
static void AcceptClient(SOCKET ls)
Accepts clients from the sockets.
byte * buffer
The buffer of this packet, of basically variable length up to SEND_MTU.
static bool Receive()
Handle the receiving of packets.
const char * GetHostname()
Get the hostname; in case it wasn't given the IPv4 dotted representation is given.
PacketSize size
The size of the whole packet for received packets.
bool IsInNetmask(const char *netmask)
Checks whether this IP address is contained by the given netmask.
#define DEBUG(name, level,...)
Output a line of debugging information.
uint32 _frame_counter
The current frame.
static bool SetNonBlocking(SOCKET d)
Try to set the socket into non-blocking mode.
StringList _network_ban_list
The banned clients.
static void CloseListeners()
Close the sockets we're listening on.
static bool SetNoDelay(SOCKET d)
Try to set the socket to not delay sending.
Basic functions to receive and send TCP packets.
static bool Listen(uint16 port)
Listen on a particular port.