void httpgetsend(ENetAddress &ad, char *hostname, char *req, char *ref, char *agent) { if (ad.host == ENET_HOST_ANY) { printf("looking up %s...\n", hostname); enet_address_set_host(&ad, hostname); if (ad.host == ENET_HOST_ANY) return; }; if (mssock != ENET_SOCKET_NULL) enet_socket_destroy(mssock); mssock = enet_socket_create(ENET_SOCKET_TYPE_STREAM); if (mssock == ENET_SOCKET_NULL) { printf("could not open socket\n"); return; }; if (enet_socket_connect(mssock, &ad) < 0) { printf("could not connect\n"); return; }; ENetBuffer buf; IString httpget; std::sprintf(httpget, "GET %s HTTP/1.0\nHost: %s\nReferer: %s\nUser-Agent: %s\n\n", req, hostname, ref, agent); buf.data = httpget; buf.dataLength = strlen((char *) buf.data); printf("sending request to %s...\n", hostname); enet_socket_send(mssock, NULL, &buf, 1); }
LANGameFinderAndAnnouncer::LANGameFinderAndAnnouncer() : serverPort(0), lastAnnounce(0) { announceSocket = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM); if(announceSocket == ENET_SOCKET_NULL) { throw std::runtime_error("LANGameFinderAndAnnouncer: Creating socket failed!"); } if(enet_socket_set_option(announceSocket, ENET_SOCKOPT_REUSEADDR, 1) < 0) { enet_socket_destroy(announceSocket); throw std::runtime_error("LANGameFinderAndAnnouncer: Setting socket option 'ENET_SOCKOPT_REUSEADDR' failed!"); } if(enet_socket_set_option(announceSocket, ENET_SOCKOPT_NONBLOCK, 1) < 0) { enet_socket_destroy(announceSocket); throw std::runtime_error("LANGameFinderAndAnnouncer: Setting socket option 'ENET_SOCKOPT_NONBLOCK' failed!"); } if(enet_socket_set_option(announceSocket, ENET_SOCKOPT_BROADCAST, 1) < 0) { enet_socket_destroy(announceSocket); throw std::runtime_error("LANGameFinderAndAnnouncer: Setting socket option 'ENET_SOCKOPT_BROADCAST' failed!"); } ENetAddress address; address.host = ENET_HOST_ANY; address.port = LANGAME_ANNOUNCER_PORT; if(enet_socket_bind(announceSocket, &address) < 0) { enet_socket_destroy(announceSocket); throw std::runtime_error("LANGameFinderAndAnnouncer: Binding socket to address failed!"); } }
bool initlogging(const char *identity, int facility_, int consolethres, int filethres, int syslogthres, bool logtimestamp) { facility = facility_ & 7; timestamp = logtimestamp; if(consolethres >= 0) consolethreshold = min(consolethres, (int)ACLOG_NUM); if(filethres >= 0) filethreshold = min(filethres, (int)ACLOG_NUM); if(syslogthres >= 0) syslogthreshold = min(syslogthres, (int)ACLOG_NUM); if(ident != identity) copystring(ident, identity); formatstring(ident_full)("ACR[%s]", identity); if(syslogthreshold < ACLOG_NUM) { #ifdef AC_USE_SYSLOG openlog(ident_full, LOG_NDELAY, facilities[facility]); #else if((logsock = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM)) == ENET_SOCKET_NULL || enet_address_set_host(&logdest, "localhost") < 0) syslogthreshold = ACLOG_NUM; #endif } static int lognum = 0; formatstring(filepath)("serverlog_%s_%s.part%d.txt", timestring(true), identity, ++lognum); if(fp) { fclose(fp); fp = NULL; } if(filethreshold < ACLOG_NUM) { fp = fopen(filepath, "w"); if(!fp) printf("failed to open \"%s\" for writing\n", filepath); } defformatstring(msg)("logging started: console(%s), file(%s", levelname[consolethreshold], levelname[fp ? filethreshold : ACLOG_NUM]); if(fp) concatformatstring(msg, ", \"%s\"", filepath); concatformatstring(msg, "), syslog(%s", levelname[syslogthreshold]); if(syslogthreshold < ACLOG_NUM) concatformatstring(msg, ", \"%s\", local%d", ident_full, facility); concatformatstring(msg, "), timestamp(%s)", timestamp ? "ENABLED" : "DISABLED"); enabled = consolethreshold < ACLOG_NUM || fp || syslogthreshold < ACLOG_NUM; if(enabled) printf("%s\n", msg); return enabled; }
/** Creates a host for communicating to peers. @param address the address at which other peers may connect to this host. If NULL, then no peers may connect to the host. @param peerCount the maximum number of peers that should be allocated for the host. @param incomingBandwidth downstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth. @param outgoingBandwidth upstream bandwidth of the host in bytes/second; if 0, ENet will assume unlimited bandwidth. @returns the host on success and NULL on failure @remarks ENet will strategically drop packets on specific sides of a connection between hosts to ensure the host's bandwidth is not overwhelmed. The bandwidth parameters also determine the window size of a connection which limits the amount of reliable packets that may be in transit at any given time. */ ENetHost * enet_host_create (const ENetAddress * address, size_t peerCount, enet_uint32 incomingBandwidth, enet_uint32 outgoingBandwidth) { ENetHost * host = (ENetHost *) enet_malloc (sizeof (ENetHost)); ENetPeer * currentPeer; if (peerCount > ENET_PROTOCOL_MAXIMUM_PEER_ID) return NULL; host -> peers = (ENetPeer *) enet_malloc (peerCount * sizeof (ENetPeer)); memset (host -> peers, 0, peerCount * sizeof (ENetPeer)); host -> socket = enet_socket_create (ENET_SOCKET_TYPE_DATAGRAM, address); if (host -> socket == ENET_SOCKET_NULL) { enet_free (host -> peers); enet_free (host); return NULL; } if (address != NULL) host -> address = * address; host -> incomingBandwidth = incomingBandwidth; host -> outgoingBandwidth = outgoingBandwidth; host -> bandwidthThrottleEpoch = 0; host -> recalculateBandwidthLimits = 0; host -> mtu = ENET_HOST_DEFAULT_MTU; host -> peerCount = peerCount; host -> lastServicedPeer = host -> peers; host -> commandCount = 0; host -> bufferCount = 0; host -> receivedAddress.host = ENET_HOST_ANY; host -> receivedAddress.port = 0; host -> receivedDataLength = 0; for (currentPeer = host -> peers; currentPeer < & host -> peers [host -> peerCount]; ++ currentPeer) { currentPeer -> host = host; currentPeer -> incomingPeerID = currentPeer - host -> peers; currentPeer -> data = NULL; enet_list_clear (& currentPeer -> acknowledgements); enet_list_clear (& currentPeer -> sentReliableCommands); enet_list_clear (& currentPeer -> sentUnreliableCommands); enet_list_clear (& currentPeer -> outgoingReliableCommands); enet_list_clear (& currentPeer -> outgoingUnreliableCommands); enet_peer_reset (currentPeer); } return host; }
bool BroadcastHost::init(unsigned short port) { // Create raw socket // FIXME: This needs a really recent version of enet afaik. ENetAddress bcaddr; bcaddr.host = ENET_HOST_ANY; bcaddr.port = port; bcastsocket = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM); if (bcastsocket == -1) std::cout << "No socket." << std::endl; if (enet_socket_set_option(bcastsocket, ENET_SOCKOPT_REUSEADDR, 1)) std::cout << "enet_socket_set_option1" << std::endl; if (enet_socket_bind(bcastsocket, &bcaddr)) std::cout << "enet_socket_bind" << std::endl; if (enet_socket_set_option(bcastsocket, ENET_SOCKOPT_NONBLOCK, 1)) std::cout << "enet_socket_set_option2" << std::endl; std::cout << "Started broadcast host (" << port << ")." << std::endl; return true; }
bool Network::initialize(Client & client) { if (enet_initialize() != 0) return false; m_client = enet_host_create(nullptr, 1, 1, 0, 0); if (!m_client) return false; if (enet_host_compress_with_range_coder(m_client) < 0) return false; m_socket = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM); if (m_socket < 0) return false; if (enet_socket_set_option(m_socket, ENET_SOCKOPT_NONBLOCK, 1) < 0) return false; if (enet_socket_set_option(m_socket, ENET_SOCKOPT_BROADCAST, 1) < 0) return false; return true; }
bool setuppingsocket(ENetAddress *address) { if(pingsocket != ENET_SOCKET_NULL) return true; pingsocket = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM); if(pingsocket == ENET_SOCKET_NULL) return false; if(address && enet_socket_bind(pingsocket, address) < 0) return false; enet_socket_set_option(pingsocket, ENET_SOCKOPT_NONBLOCK, 1); return true; }
void servermsinit(const char *master, const char *sdesc, bool listen) { const char *mid = strstr(master, "/"); if (!mid) mid = master; strcpy_s(masterpath, mid); strn0cpy(masterbase, master, mid-master+1); strcpy_s(serverdesc, sdesc); if (listen) { ENetAddress address = { ENET_HOST_ANY, CUBE_SERVINFO_PORT }; pongsock = enet_socket_create(ENET_SOCKET_TYPE_DATAGRAM, &address); if (pongsock == ENET_SOCKET_NULL) sys::fatal("could not create server info socket\n"); } }
void setupmaster() { if(masterserver) { conoutf("loading master (%s:%d)..", *masterip ? masterip : "*", masterport); ENetAddress address = { ENET_HOST_ANY, enet_uint16(masterport) }; if(*masterip && enet_address_set_host(&address, masterip) < 0) fatal("failed to resolve master address: %s", masterip); if((mastersocket = enet_socket_create(ENET_SOCKET_TYPE_STREAM)) == ENET_SOCKET_NULL) fatal("failed to create master server socket"); if(enet_socket_set_option(mastersocket, ENET_SOCKOPT_REUSEADDR, 1) < 0) fatal("failed to set master server socket option"); if(enet_socket_bind(mastersocket, &address) < 0) fatal("failed to bind master server socket"); if(enet_socket_listen(mastersocket, -1) < 0) fatal("failed to listen on master server socket"); if(enet_socket_set_option(mastersocket, ENET_SOCKOPT_NONBLOCK, 1) < 0) fatal("failed to make master server socket non-blocking"); if(!setuppingsocket(&address)) fatal("failed to create ping socket"); starttime = clocktime; conoutf("master server started on %s:[%d]", *masterip ? masterip : "localhost", masterport); } }