Host::~Host() { for (auto& p : m_peers) enet_peer_disconnect_now((ENetPeer*) p.m_peer, 0); m_peers.clear(); if (m_object) { enet_host_flush((ENetHost*) m_object); enet_host_destroy((ENetHost*) m_object); m_object = nullptr; } }
Client::~Client() { enet_host_destroy(m_client); delete m_mainPlayer; // call this ONLY when linking with FreeImage as a static library #ifdef FREEIMAGE_LIB FreeImage_DeInitialise(); #endif SDL_DestroyWindow(m_window); SDL_Quit(); }
bool NNetwork::CreateClient() { if (Client) { enet_host_destroy(Client); } Client = enet_host_create(NULL,1,2,0,0); if (!Client) { GetGame()->GetLog()->Send("NETWORK",0,"Failed to create client!"); return 1; } return 0; }
void NetInputTerminate(NetInput *n) { enet_host_destroy(n->server); n->server = NULL; for (int i = 0; i < (int)n->peers.size; i++) { ENetPeer **p = CArrayGet(&n->peers, i); if (*p) { enet_peer_reset(*p); } } CArrayTerminate(&n->peers); }
static inline void destroy_host() { if (!host) { return; } if (server) { list_clear(peers); enet_peer_disconnect(server, 0); enet_peer_reset(server); } enet_host_destroy(host); user_id = 0; host = NULL; server = NULL; }
void Network::restart() { pthread_mutex_lock(&net_mutex); ENetEvent event; enet_peer_disconnect(server, 0); while (enet_host_service (client, &event, 3000) > 0) { if(event.type == ENET_EVENT_TYPE_RECEIVE) enet_packet_destroy (event.packet); else if(event.type == ENET_EVENT_TYPE_DISCONNECT) break; } enet_host_destroy(client); pthread_mutex_unlock(&net_mutex); start(); }
void main_loop() { static int counter = 0; #if EMSCRIPTEN counter++; #endif if (counter == 100) { printf("stop!\n"); #if EMSCRIPTEN emscripten_cancel_main_loop(); #endif return; } ENetEvent event; //printf("enet host?\n"); if (enet_host_service (host, & event, 0) == 0) return; printf("enet host, got event of type %d\n", event.type); switch (event.type) { case ENET_EVENT_TYPE_CONNECT: printf ("A new client connected from %x:%u.\n", event.peer -> address.host, event.peer -> address.port); /* Store any relevant client information here. */ event.peer -> data = "Client information"; send_msg(event.peer); break; case ENET_EVENT_TYPE_RECEIVE: printf ("A packet of length %u containing %s was received from %s on channel %u.\n", event.packet -> dataLength, event.packet -> data, event.peer -> data, event.channelID); /* Clean up the packet now that we're done using it. */ enet_packet_destroy (event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: printf ("%s disconected.\n", event.peer -> data); /* Reset the peer's client information. */ event.peer -> data = NULL; enet_host_destroy(host); break; default: printf("whaaa? %d\n", event.type); } }
Server::~Server() { MasterServerRequest msg; std::string packet; ENetEvent event; int hasEvent; bool pull = true; bool passed = false; unsigned int delay = 50; unsigned int timeout = 3000; unsigned int cuTime = 0; enet_host_destroy(_server); if (_masterAuthenticate) { _masterSocket.connect(_set.getCvar("sv_masterIP"), _set.getCvar("sv_masterPort"), 2); std::cout << "Shutting down server" << std::endl; while (!passed) { if ((hasEvent = _masterSocket.pullEvent(event, delay, pull)) >= 0) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: msg.set_content(MasterServerRequest::DELETESERVER); msg.set_port(_set.getCvar("sv_port")); msg.SerializeToString(&packet); _masterSocket.sendPacket(0, packet); enet_host_flush(_masterSocket.getHost()); passed = true; break; case ENET_EVENT_TYPE_NONE: cuTime += delay; if (cuTime >= timeout) passed = true; break; default: break; } } else break; } _masterSocket.disconnect(); } }
void CHostENet::End() { if (m_Status == INIT_AND_CONNECTED) DisconnectAll(); enet_host_destroy(m_Host); enet_deinitialize(); m_Status = NO_INIT; std::vector<CPeerENet*>::iterator it = m_PeerList.begin(); while (it != m_PeerList.end()) { delete (*it); ++it; } m_PeerList.clear(); }
NetManager::~NetManager() { if(mIsServer) { for(size_t i = 0; i < mClients.size(); i++) enet_peer_disconnect(mClients[i], 0); } else if(mServerPeer != NULL) { enet_peer_disconnect(mServerPeer, 0); } enet_host_destroy(mHost); enet_deinitialize(); }
void network_deinit() { g_debug("Stopping server"); tetris_plugin_action(PLUGIN_SHUTDOWN, -1, NULL, NULL); /* Disconnect each clients */ g_slist_foreach(network->clients, (GFunc) enet_peer_disconnect_now, 0); /* ENet's documentation doesn't say anything about how to properly delete an ENetPeer, and using free on them results in a segfault, so we just don't do anything */ g_slist_free(network->clients); enet_host_destroy(network->server); enet_deinitialize(); g_free(network); }
/** * Destroy the host and all connected peers. */ void host_destroy (void) { int i = 0; /* deallocate all peers */ for (i = 0; i < host->peerCount; i++) { if (host->peers[i].state == ENET_PEER_STATE_CONNECTED) { if (host->peerCount > 1) free(host->peers[i].data); enet_peer_disconnect_now(&host->peers[i], 0); } } /* deallocate the host */ enet_host_destroy(host); }
void network_close( ENetHost* socket ) { ENetEvent ev; if( socket == 0 ) return; for( unsigned int i = 0; i < socket->peerCount; i++ ) { ENetPeer* p = &socket->peers[i]; if( p->state != ENET_PEER_STATE_DISCONNECTED ) enet_peer_disconnect( p, 0 ); } while( enet_host_service( socket, &ev, 0 ) > 0 ) { } enet_host_destroy( socket ); }
void Room::Destroy() { room_impl->state = State::Closed; room_impl->room_thread->join(); room_impl->room_thread.reset(); if (room_impl->server) { enet_host_destroy(room_impl->server); } room_impl->room_information = {}; room_impl->server = nullptr; { std::lock_guard<std::mutex> lock(room_impl->member_mutex); room_impl->members.clear(); } room_impl->room_information.member_slots = 0; room_impl->room_information.name.clear(); }
void main_loop() { #ifdef __EMSCRIPTEN__ static int counter = 0; counter++; if (counter == 100) { printf("stop!\n"); emscripten_cancel_main_loop(); return; } #endif ENetEvent event; if (enet_host_service (host, & event, 0) == 0) return; switch (event.type) { case ENET_EVENT_TYPE_CONNECT: printf ("Connection succeeded!\n"); break; case ENET_EVENT_TYPE_RECEIVE: printf ("A packet of length %u containing %s was received from %s on channel %u.\n", event.packet -> dataLength, event.packet -> data, event.peer -> data, event.channelID); int result = strcmp("packetfoo", event.packet->data); #ifdef __EMSCRIPTEN__ REPORT_RESULT(result); #else exit(EXIT_SUCCESS); #endif /* Clean up the packet now that we're done using it. */ enet_packet_destroy (event.packet); break; case ENET_EVENT_TYPE_DISCONNECT: printf ("%s disconected.\n", event.peer -> data); /* Reset the peer's client information. */ event.peer -> data = NULL; enet_host_destroy(host); break; default: printf("whaaa? %d\n", event.type); } }
void PNetworkServer::shutdown() { if (m_state == NETWORK_CONNECTED || m_state == NETWORK_ERROR) { PMap<puint32, PNetworkPeer*>::iterator it = m_peers.begin(); PMap<puint32, PNetworkPeer*>::iterator ie = m_peers.end(); while (it != ie) { enet_peer_disconnect_now(it.value()->data()->peer, it.key()); PDELETE(it.value()); ++it; } enet_host_destroy(m_data->server); m_state = NETWORK_DISCONNECTED; } }
GameLabyrinth::~GameLabyrinth() { if (host != NULL) { for EACH(ClientVector, clients, client) { (*client)->disconenct(); delete *client; } clients.clear(); if (server.peer != NULL) { clientDisconnectFromServer(); } enet_host_destroy(host); enet_deinitialize(); log_msg("net", "Network deinitialized"); }
/* close a server, disconnecting all clients and freeing up memory used by them */ void joynet_close_server(JOYNET_SERVER * sp) { int i; for(i = 0; i < sp->max_clients; i++) { if(sp->client[i]->peer) { enet_peer_disconnect(sp->client[i]->peer, JOYNET_DISCONNECT_SERVER_CLOSED); } } enet_host_flush(sp->host); for(i = 0; i < sp->max_clients; i++) { joynet_destroy_client(sp->client[i]); } free(sp->client); enet_host_destroy(sp->host); }
int main (int argc, char ** argv) { ENetAddress address; ENetHost * host; ENetEvent event; ENetPeer *peer; if (argc < 2) { fprintf(stderr,"Usage: ./host port\n"); exit(1); } if (enet_initialize () != 0) { fprintf (stderr, "An error occurred while initializing ENet.\n"); exit(1); } address.host = ENET_HOST_ANY; address.port = atoi(argv[1]); host = enet_host_create( &address, 32, 2, 0 ); if (host == NULL) { fprintf (stderr, "error while trying to create an ENet host.\n"); exit (1); } fprintf(stderr,"Running"); while ( enet_host_service( host, &event, 1000 ) > 0 ) { fprintf(stderr,"."); } enet_host_destroy(host); atexit (enet_deinitialize); exit(0); // success }
void Server::onClose() { // number of milliseconds to wait for network events, corresponding to time between frames const unsigned int waitTime = 5 ; for(std::list<ENetPeer*>::iterator client = m_clients.begin() ; client != m_clients.end() ; ++client) { enet_peer_disconnect(*client,0) ; ENetEvent event ; // give a chance to notify clients while (enet_host_service(m_host,&event,waitTime) > 0) {} } if (m_host) { enet_host_destroy(m_host) ; m_host = NULL ; } }
bool NNetwork::CreateServer() { if (Server) { enet_host_destroy(Server); } Address.host = ENET_HOST_ANY; Address.port = GetGame()->GetConfig()->GetFloat("Port"); PlayerSlots = GetGame()->GetConfig()->GetFloat("PlayerSlots"); std::stringstream Message; Message << "Creating server on port " << Address.port << " with " << PlayerSlots << " player slots.\n"; GetGame()->GetLog()->Send("NETWORK",2,Message.str()); Server = enet_host_create(&Address,PlayerSlots,2,0,0); if (!Server) { GetGame()->GetLog()->Send("NETWORK",0,"Failed to create server!"); return 1; } return 0; }
Error NetworkedMultiplayerENet::create_client(const IP_Address& p_ip, int p_port, int p_in_bandwidth, int p_out_bandwidth){ ERR_FAIL_COND_V(active,ERR_ALREADY_IN_USE); host = enet_host_create (NULL /* create a client host */, 1 /* only allow 1 outgoing connection */, 2 /* allow up 2 channels to be used, 0 and 1 */, p_in_bandwidth /* 56K modem with 56 Kbps downstream bandwidth */, p_out_bandwidth /* 56K modem with 14 Kbps upstream bandwidth */); ERR_FAIL_COND_V(!host,ERR_CANT_CREATE); _setup_compressor(); ENetAddress address; address.host=p_ip.host; address.port=p_port; //enet_address_set_host (& address, "localhost"); //address.port = p_port; unique_id=_gen_unique_id(); /* Initiate the connection, allocating the two channels 0 and 1. */ ENetPeer *peer = enet_host_connect (host, & address, 2, unique_id); if (peer == NULL) { enet_host_destroy(host); ERR_FAIL_COND_V(!peer,ERR_CANT_CREATE); } //technically safe to ignore the peer or anything else. connection_status=CONNECTION_CONNECTING; active=true; server=false; refuse_connections=false; return OK; }
void ConnectionHandler::stopListen() { // - Disconnect all clients (close sockets) // TODO: probably there's a better way. ENetPeer *currentPeer; for (currentPeer = host->peers; currentPeer < &host->peers[host->peerCount]; ++currentPeer) { if (currentPeer->state == ENET_PEER_STATE_CONNECTED) { enet_peer_disconnect(currentPeer, 0); enet_host_flush(host); enet_peer_reset(currentPeer); } } enet_host_destroy(host); // FIXME: memory leak on NetComputers }
void* ThreadRecv() { ENetEvent event; /* Wait up to 1000 milliseconds for an event. */ while(1) { CCLOG("begin recv"); while (enet_host_service (server, & event, 1000000) > 0) { switch (event.type) { case ENET_EVENT_TYPE_CONNECT: CCLOG("A new client connected from %x:%u.\n", event.peer -> address.host, event.peer -> address.port); /* Store any relevant client information here. */ char temp[50]; strcat(temp, "Client information"); event.peer -> data = temp; //channel = 1; break; case ENET_EVENT_TYPE_RECEIVE: CCLOG("A packet of length %u containing %s was received on channel %u.\n", event.packet -> dataLength, event.packet -> data, event.channelID); updateMap(viewLayer, event.packet -> data); CCLOG("update finish"); enet_packet_destroy (event.packet); CCLOG("packet destroyed"); break; case ENET_EVENT_TYPE_DISCONNECT: CCLOG("%s disconected.\n", event.peer -> data); /* Reset the peer's client information. */ event.peer -> data = NULL; } } } enet_host_destroy(server); return NULL; }
void joynet_ping(const char * url, int port) { ENetAddress address; ENetHost * host; ENetPeer * peer; if(enet_address_set_host(&address, url) < 0) { return; } address.port = port; host = enet_host_create(NULL, 1, 0, 0, 0); if(!host) { return; } peer = enet_host_connect(host, &address, 4, 0); if(!peer) { return; } enet_host_destroy(host); }
bool NNetwork::PollEvent(ENetEvent* Event) { if (!Server && !Client) { return false; } if (Server && Client) { GetGame()->GetLog()->Send("NETWORK",1,"We have a server and client running on the same host! Assuming we're a server..."); enet_host_destroy(Client); Client = NULL; } if (Server) { int Check = enet_host_service(Server,Event,0); if (Check > 0) { return true; } if (Check < 0) { GetGame()->GetLog()->Send("NETWORK",0,"Failed to poll for events!"); } return false; } int Check = enet_host_service(Client,Event,0); if (Check > 0) { return true; } if (Check < 0) { GetGame()->GetLog()->Send("NETWORK",0,"Failed to poll for events!"); } return false; }
NetPlayServer::~NetPlayServer() { if (is_connected) { m_do_loop = false; m_thread.join(); enet_host_destroy(m_server); if (g_MainNetHost.get() == m_server) { g_MainNetHost.release(); } if (m_traversal_client) { g_TraversalClient->m_Client = nullptr; ReleaseTraversalClient(); } } #ifdef USE_UPNP UPnP::StopPortmapping(); #endif }
/** * Free client state. */ void quit_client_state() { int input_error; /* free enet structures */ if (client.client) enet_peer_disconnect(client.client, 0); if (client.host) enet_host_destroy(client.host); /* get thread status */ mutex_lock(&client.input_lock); input_error = client.input_error; mutex_unlock(&client.input_lock); /* cancel the input thread if needed */ if (!input_error) { mutex_lock(&client.input_lock); thread_cancel(client.input_thread); mutex_unlock(&client.input_lock); } /* join a canceled thread */ thread_join(client.input_thread); /* destroy the input lock */ mutex_destroy(&client.input_lock); mutex_destroy(&client.network_lock); /* free other input resources */ free(client.input_buffer); dlist_free(client.input_queue); dfree(client.server_host_addr); dfree(client.server_host_name); free_server_info(); }
void disconnect(int onlyclean, int async) { bool cleanup = onlyclean!=0; if(curpeer || connectedlocally) { if(curpeer) { if(!discmillis) { enet_peer_disconnect(curpeer, DISC_NONE); if(clienthost) enet_host_flush(clienthost); discmillis = totalmillis; } if(curpeer->state!=ENET_PEER_STATE_DISCONNECTED) { if(async) return; enet_peer_reset(curpeer); } curpeer = NULL; } discmillis = 0; conoutft(CON_MESG, "\frdisconnected"); cleanup = true; } if(!connpeer && clienthost) { enet_host_destroy(clienthost); clienthost = NULL; } if(cleanup) { client::gamedisconnect(onlyclean); localdisconnect(); } if(!onlyclean) localconnect(false); }
int main() { ENetHost *client; ENetAddress address; ENetEvent event; ENetPeer *peer; ENetPacket *packet; char string[1024]; pid_t pid; signal(SIGCHLD, sigchld_handler); /* create client */ client = enet_host_create(NULL, 1, #ifdef HAS_RECENT_ENET 2, #endif 0, 0); if (client == NULL) { fprintf(stderr, "Can't create client\n"); exit(1); } /* connect client */ enet_address_set_host(&address, "localhost"); address.port = 12345; peer = enet_host_connect(client, &address, 2 #ifdef HAS_RECENT_ENET , 0 #endif ); if (peer == NULL) { fprintf(stderr, "Can't create peer\n"); exit(1); } if (enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT) { printf("Connected\n"); } else { fprintf(stderr, "Connection failed\n"); exit(1); } /* fork */ pid = fork(); if (pid == 0) { /* read and send packets */ while (1) { if (fgets(string, 1024, stdin) == NULL) /* disconnect at EOF */ break; string[strlen(string)-1] = '\0'; /* drop the \n */ packet = enet_packet_create(string, strlen(string) + 1, ENET_PACKET_FLAG_RELIABLE); enet_peer_send(peer, 0, packet); enet_host_flush(client); } /* disconnect client */ enet_peer_disconnect(peer, 0); enet_host_destroy(client); } else { while (1) { enet_host_service(client, &event, 1000); if (event.type == ENET_EVENT_TYPE_RECEIVE) printf("> %s\n", event.packet->data); if (event.type == ENET_EVENT_TYPE_DISCONNECT) { printf("Exiting client\n"); exit(0); break; } } } return 0; }