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!"); } }
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); }
void cleanupserver() { if(serverhost) enet_host_destroy(serverhost); serverhost = NULL; if(pongsock != ENET_SOCKET_NULL) enet_socket_destroy(pongsock); if(lansock != ENET_SOCKET_NULL) enet_socket_destroy(lansock); pongsock = lansock = ENET_SOCKET_NULL; }
bool BroadcastHost::shutdown() { enet_socket_destroy(bcastsocket); if (reply) reply->drop(); reply = 0; return true; }
void Network::finalize(Client & client) { if(m_server) enet_peer_disconnect_now(m_server, 0); if(m_client) enet_host_destroy(m_client); enet_socket_destroy(m_socket); enet_deinitialize(); }
void httpgetrecieve(ENetBuffer &buf) { if (mssock==ENET_SOCKET_NULL) return; enet_uint32 events = ENET_SOCKET_WAIT_RECEIVE; if (enet_socket_wait(mssock, &events, 0) >= 0 && events) { int len = enet_socket_receive(mssock, NULL, &buf, 1); if (len<=0) { enet_socket_destroy(mssock); mssock = ENET_SOCKET_NULL; return; } buf.data = ((char *)buf.data)+len; ((char*)buf.data)[0] = 0; buf.dataLength -= len; } }
/** Destroys the host and all resources associated with it. @param host pointer to the host to destroy */ void enet_host_destroy (ENetHost * host) { ENetPeer * currentPeer; enet_socket_destroy (host -> socket); for (currentPeer = host -> peers; currentPeer < & host -> peers [host -> peerCount]; ++ currentPeer) { enet_peer_reset (currentPeer); } enet_free (host -> peers); enet_free (host); }
LANGameFinderAndAnnouncer::~LANGameFinderAndAnnouncer() { if(serverPort > 0) { stopAnnounce(); } enet_socket_destroy(announceSocket); }
/** Forcefully disconnects a peer. @param peer peer to forcefully disconnect @remarks The foreign host represented by the peer is not notified of the disconnection and will timeout on its connection to the local host. */ void enet_peer_reset (ENetPeer * peer) { if (peer -> ownSocket != ENET_SOCKET_NULL) { enet_socket_destroy (peer -> ownSocket); peer -> ownSocket = ENET_SOCKET_NULL; } size_t i; ENetHost * host = peer -> host; for (i = 0; i < host -> peerCount - host -> idlePeers; ++ i) if (host -> busyPeersList [i] == peer -> incomingPeerID) { host -> idlePeersList [host -> idlePeers ++] = peer -> incomingPeerID; host -> busyPeersList [i] = host -> busyPeersList [host -> peerCount - host -> idlePeers]; break; } if (peer -> host -> connectingPeerTimeout && peer -> connectingPeers) { enet_free(peer -> connectingPeers); peer -> connectingPeers = NULL; peer -> host -> connectsWindow -= ENET_PROTOCOL_TOTAL_SESSIONS * (peer -> connectingPeersTimeMask + 1); } enet_peer_on_disconnect (peer); peer -> outgoingPeerID = ENET_PROTOCOL_MAXIMUM_PEER_ID; peer -> connectID = 0; peer -> state = ENET_PEER_STATE_DISCONNECTED; peer -> incomingBandwidth = 0; peer -> outgoingBandwidth = 0; peer -> incomingBandwidthThrottleEpoch = 0; peer -> outgoingBandwidthThrottleEpoch = 0; peer -> incomingDataTotal = 0; peer -> outgoingDataTotal = 0; peer -> lastSendTime = 0; peer -> lastReceiveTime = 0; peer -> nextTimeout = 0; peer -> earliestTimeout = 0; peer -> packetLossEpoch = 0; peer -> packetsSent = 0; peer -> packetsLost = 0; peer -> packetLoss = 0; peer -> packetLossVariance = 0; peer -> packetThrottle = ENET_PEER_DEFAULT_PACKET_THROTTLE; peer -> packetThrottleLimit = ENET_PEER_PACKET_THROTTLE_SCALE; peer -> packetThrottleCounter = 0; peer -> packetThrottleEpoch = 0; peer -> packetThrottleAcceleration = ENET_PEER_PACKET_THROTTLE_ACCELERATION; peer -> packetThrottleDeceleration = ENET_PEER_PACKET_THROTTLE_DECELERATION; peer -> packetThrottleInterval = ENET_PEER_PACKET_THROTTLE_INTERVAL; peer -> pingInterval = ENET_PEER_PING_INTERVAL; peer -> timeoutLimit = ENET_PEER_TIMEOUT_LIMIT; peer -> timeoutMinimum = ENET_PEER_TIMEOUT_MINIMUM; peer -> timeoutMaximum = ENET_PEER_TIMEOUT_MAXIMUM; peer -> lastRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME; peer -> lowestRoundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME; peer -> lastRoundTripTimeVariance = 0; peer -> highestRoundTripTimeVariance = 0; peer -> roundTripTime = ENET_PEER_DEFAULT_ROUND_TRIP_TIME; peer -> roundTripTimeVariance = 0; peer -> mtu = peer -> host -> mtu; peer -> reliableDataInTransit = 0; peer -> outgoingReliableSequenceNumber = 0; peer -> windowSize = ENET_PROTOCOL_MAXIMUM_WINDOW_SIZE; peer -> incomingUnsequencedGroup = 0; peer -> outgoingUnsequencedGroup = 0; peer -> eventData = 0; peer -> totalWaitingData = 0; memset (& peer -> localAddress, 0, sizeof (ENetAddress)); memset (peer -> unsequencedWindow, 0, sizeof (peer -> unsequencedWindow)); enet_peer_reset_queues (peer); }
/** 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; }