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 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; }
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); } }
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; }
component* menu_listen_create(scene *s) { listen_menu_data *local = malloc(sizeof(listen_menu_data)); s->gs->role = ROLE_SERVER; local->s = s; // Form address (host) ENetAddress address; address.host = ENET_HOST_ANY; address.port = settings_get()->net.net_listen_port; // Set up host local->host = enet_host_create(&address, 1, 2, 0, 0); if(local->host == NULL) { DEBUG("Failed to initialize ENet server"); free(local); return NULL; } enet_socket_set_option(local->host->socket, ENET_SOCKOPT_REUSEADDR, 1); // Text config text_settings tconf; text_defaults(&tconf); tconf.font = FONT_BIG; tconf.halign = TEXT_CENTER; tconf.cforeground = color_create(0, 121, 0, 255); // Create the menu component* menu = menu_create(11); menu_attach(menu, label_create(&tconf, "START SERVER")); menu_attach(menu, filler_create()); menu_attach(menu, label_create(&tconf, "Waiting ...")); menu_attach(menu, filler_create()); local->cancel_button = textbutton_create(&tconf, "CANCEL", COM_ENABLED, menu_listen_cancel, s); menu_attach(menu, local->cancel_button); menu_set_userdata(menu, local); menu_set_free_cb(menu, menu_listen_free); menu_set_tick_cb(menu, menu_listen_tick); return menu; }
/** 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); if (host -> socket == ENET_SOCKET_NULL || (address != NULL && enet_socket_bind (host -> socket, address) < 0)) { if (host -> socket != ENET_SOCKET_NULL) enet_socket_destroy (host -> socket); enet_free (host -> peers); enet_free (host); return NULL; } enet_socket_set_option (host -> socket, ENET_SOCKOPT_NONBLOCK, 1); enet_socket_set_option (host -> socket, ENET_SOCKOPT_BROADCAST, 1); enet_socket_set_option (host -> socket, ENET_SOCKOPT_RCVBUF, ENET_HOST_RECEIVE_BUFFER_SIZE); enet_socket_set_option (host -> socket, ENET_SOCKOPT_SNDBUF, ENET_HOST_SEND_BUFFER_SIZE); 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; }