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" 31 template <
class Tsocket, PacketType Tfull_packet, PacketType Tban_packet>
44 struct sockaddr_storage sin;
45 memset(&sin, 0,
sizeof(sin));
46 socklen_t sin_len =
sizeof(sin);
47 SOCKET s = accept(ls, (
struct sockaddr*)&sin, &sin_len);
48 if (s == INVALID_SOCKET)
return;
65 DEBUG(net, 1,
"[%s] Banned ip tried to join (%s), refused", Tsocket::GetName(), *iter);
67 if (send(s, (
const char*)p.
buffer, p.
size, 0) < 0) {
68 DEBUG(net, 0,
"send failed with error %d", GET_LAST_ERROR());
78 if (!Tsocket::AllowConnection()) {
84 if (send(s, (
const char*)p.
buffer, p.
size, 0) < 0) {
85 DEBUG(net, 0,
"send failed with error %d", GET_LAST_ERROR());
92 Tsocket::AcceptConnection(s, address);
102 fd_set read_fd, write_fd;
110 FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
111 FD_SET(cs->sock, &read_fd);
112 FD_SET(cs->sock, &write_fd);
117 FD_SET(s->second, &read_fd);
120 tv.tv_sec = tv.tv_usec = 0;
121 #if !defined(__MORPHOS__) && !defined(__AMIGA__) 122 if (select(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv) < 0)
return false;
124 if (WaitSelect(FD_SETSIZE, &read_fd, &write_fd, NULL, &tv, NULL) < 0)
return false;
129 if (FD_ISSET(s->second, &read_fd))
AcceptClient(s->second);
133 FOR_ALL_ITEMS_FROM(Tsocket, idx, cs, 0) {
134 cs->writable = !!FD_ISSET(cs->sock, &write_fd);
135 if (FD_ISSET(cs->sock, &read_fd)) {
136 cs->ReceivePackets();
149 assert(sockets.
Length() == 0);
154 for (
NetworkAddress *address = addresses.Begin(); address != addresses.End(); address++) {
155 address->Listen(SOCK_STREAM, &sockets);
158 if (sockets.
Length() == 0) {
159 DEBUG(net, 0,
"[server] could not start network: could not create listening socket");
160 NetworkError(STR_NETWORK_ERROR_SERVER_START);
171 closesocket(s->second);
174 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.
void Clear()
Remove all items from the list.
Wrapper for (un)resolved network addresses; there's no reason to transform a numeric IP to a string a...
const T * Begin() const
Get the pointer to the first item (const)
const T * End() const
Get the pointer behind the last valid item (const)
static SocketList sockets
List of sockets we listen on.
static void AcceptClient(SOCKET ls)
Accepts clients from the sockets.
uint Length() const
Get the number of items in the list.
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.
#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.
bool IsInNetmask(char *netmask)
Checks whether this IP address is contained by the given netmask.
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.