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>
40 struct sockaddr_storage sin;
41 memset(&sin, 0,
sizeof(sin));
42 socklen_t sin_len =
sizeof(sin);
43 SOCKET s = accept(ls, (
struct sockaddr*)&sin, &sin_len);
44 if (s == INVALID_SOCKET)
return;
61 DEBUG(net, 1,
"[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), entry.c_str());
63 if (send(s, (
const char*)p.
buffer, p.
size, 0) < 0) {
64 DEBUG(net, 0,
"send failed with error %d", GET_LAST_ERROR());
74 if (!Tsocket::AllowConnection()) {
80 if (send(s, (
const char*)p.
buffer, p.
size, 0) < 0) {
81 DEBUG(net, 0,
"send failed with error %d", GET_LAST_ERROR());
88 Tsocket::AcceptConnection(s, address);
98 fd_set read_fd, write_fd;
105 for (Tsocket *cs : Tsocket::Iterate()) {
106 FD_SET(cs->sock, &read_fd);
107 FD_SET(cs->sock, &write_fd);
111 for (
auto &s : sockets) {
112 FD_SET(s.second, &read_fd);
115 tv.tv_sec = tv.tv_usec = 0;
116 if (select(FD_SETSIZE, &read_fd, &write_fd,
nullptr, &tv) < 0)
return false;
119 for (
auto &s : sockets) {
120 if (FD_ISSET(s.second, &read_fd))
AcceptClient(s.second);
124 for (Tsocket *cs : Tsocket::Iterate()) {
125 cs->writable = !!FD_ISSET(cs->sock, &write_fd);
126 if (FD_ISSET(cs->sock, &read_fd)) {
127 cs->ReceivePackets();
140 assert(sockets.size() == 0);
146 address.Listen(SOCK_STREAM, &sockets);
149 if (sockets.size() == 0) {
150 DEBUG(net, 0,
"[server] could not start network: could not create listening socket");
151 NetworkError(STR_NETWORK_ERROR_SERVER_START);
161 for (
auto &s : sockets) {
162 closesocket(s.second);
165 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.