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 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;
}
Beispiel #4
0
void servermsinit(const char *master, 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);
		enet_socket_bind(pongsock, &address);
		if (pongsock == ENET_SOCKET_NULL)
			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);
    }
}
Beispiel #6
0
/** 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;
}