Exemple #1
0
/**
	Destroy an ENetPeer structure.
**/
static void destroy_enetpeer( value p ) {
#ifdef ENET_DEBUG
	fprintf(stderr, "*** destroy_enetpeer\n");
	exit(0);
#endif
return;
	ENetPeer *peer;
	if(!val_is_abstract(p) || !val_is_kind(p, k_udprpeer))
		return;
	peer = (ENetPeer *)val_data(p);
	if(peer == NULL)
		return;

	// force immediate disconnect, if still connected.
	enet_peer_disconnect_now(peer, 0);

	// if the peer has an event still, destroy it
	//free_peer_event(peer);

	// clear the peer structure
	enet_peer_reset(peer);

	// peers never need be deallocated, they are part of an ENetHost
#ifdef ENET_DEBUG
	fprintf(stderr, "*** destroy_enetpeer done.\n");
#endif
	return;
}
Exemple #2
0
void ClientSystem::Connect()
{
    if ( mProgramState.mClientConnected )
    {
        L1( "Already connected, won't try it again!\n" );
        return; //Really wont try again
    }
    //        ENetAddress address2;
    //         address2.host = ENET_HOST_ANY;
    //         /* Bind the server to port 1234. */
    //         address2.port = 1236;
    mClient = 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 */,
                                 0 ,
                                 0 );
    if ( mClient == NULL )
    {
        L1( "An error occurred while trying to create an ENet client host.\n" );
        exit ( EXIT_FAILURE );
    }

    ENetAddress address;
    ENetEvent event;
    /* Connect to some.server.net:1234. */
    address.port = 1234;
    enet_address_set_host ( & address, core::ProgramState::Get().mServerIp.c_str() ); //core::ProgramState::Get().mServerIp.c_str());
    /* Initiate the connection, allocating the two channels 0 and 1. */
    mPeer = enet_host_connect ( mClient, & address, 2, 0 );
    if ( mPeer == NULL )
    {
        L1( "No available peers for initiating an ENet connection.\n" );
        exit ( EXIT_FAILURE );
    }
    bool connectSuccess = false;
    for( size_t i = 0; i < 500; ++i )
    {
        if ( enet_host_service ( mClient, & event, 10 ) > 0 &&
             event.type == ENET_EVENT_TYPE_CONNECT )
        {
            connectSuccess = true;
            break;
        }
    }
    enet_host_flush( mClient );
    if( !connectSuccess )
    {
        L1( "Connection timed out.\n" );
        enet_peer_reset ( mPeer );
        engine::Engine::Get().GetSystem<engine::WindowSystem>()->Close();
    }
    else
    {
        mProgramState.mClientConnected = true;
        std::auto_ptr<MyNameMessage> msg( new MyNameMessage );
        msg->mName = core::ProgramState::Get().mClientName;
        msg->mControlledLocalPlayerId = 1; // TODO: change when multiple local players are allowed
        mMessageHolder.AddOutgoingMessage( std::auto_ptr<Message>( msg.release() ) );
    }
}
Exemple #3
0
void
shmup_game_network_connect(shmup_game *g, int network_type, char *hostname)
{
	ENetEvent event;
	ENetAddress address;
	
	g->network_type = network_type;
	if (g->network_type == SERVER) {		
		address.host = ENET_HOST_ANY;
		address.port = 4000;		
		fprintf(stderr, "initializing server on port %d...\n", address.port);
		g->host = enet_host_create(&address, 4, 2, 0, 0);		
	} else {		
		fprintf(stderr, "initializing client...\n");
		enet_address_set_host(&address, hostname);
		address.port = 4000;
		g->host = enet_host_create(NULL, 1, 2, 0, 0);
		g->peer = enet_host_connect(g->host, &address, 2, 0);		
		if (g->peer == NULL) {
			fprintf (stderr, "No available peers for initiating an ENet connection.\n");
			exit (EXIT_FAILURE);
		}		
		if (enet_host_service (g->host, &event, 5000) > 0 &&
		    event.type == ENET_EVENT_TYPE_CONNECT) {
			printf("Connection to %s:4000 succeeded.\n", hostname);
			g->player[g->num_players].pos = v2(g->window_width/2, g->window_height/2);
			g->player[g->num_players].vel = v2zero;
			g->player[g->num_players].acc = v2zero;
			g->num_players++;
		} else {
			enet_peer_reset(g->peer);
			printf("Connection to %s:4000 failed.\n", hostname);
		}
	}
}
Exemple #4
0
	void CClienteENet::disconnect(CConexion* conexion)
	{
		ENetEvent event;
    
		enet_peer_disconnect (((CConexionENet*)conexion)->getENetPeer(),0);

		/* Allow up to 3 seconds for the disconnect to succeed
			and drop any packets received packets.     */
		while (enet_host_service (client, & event, 50) > 0)
		{
			switch (event.type)
			{
			case ENET_EVENT_TYPE_RECEIVE:
				enet_packet_destroy (event.packet);
				break;

			case ENET_EVENT_TYPE_DISCONNECT:
				if(DEBUG_CLIENT)
					fprintf (stdout, "Disconnection succeeded.");
					disconnectReceived(conexion);
				return;
			}
		}
	    
		/* We've arrived here, so the disconnect attempt didn't */
		/* succeed yet.  Force the connection down.             */
		enet_peer_reset (((CConexionENet*)conexion)->getENetPeer());
		disconnectReceived(conexion);

		if(DEBUG_CLIENT)
			fprintf(stdout, "Disconnection Forced");

		if(listaConexiones.empty())
			estado = INIT_NOT_CONNECTED;
	}
Exemple #5
0
void* ThreadSend() {
    while(1) {
        CCLOG("wait mutex to send");

        mutexMapPlay.lock();
//		pthread_mutex_lock(&mutexMapPlay);
        mapLayerToString(playLayer, packetBuffer);
//		strcpy(packetBuffer, "0000000030000003000000000000000000000010000000000\1");
        ENetPacket * packet = enet_packet_create (packetBuffer,
                              150,
                              ENET_PACKET_FLAG_RELIABLE);
        CCLOG(packet->data);
        /* Send the packet to the peer over channel id 0. */
        /* One could also broadcast the packet by */
        /* enet_host_broadcast (host, 0, packet); */
        enet_peer_send (peer, 0, packet);
        /* One could just use enet_host_service() instead. */
        enet_host_flush (server);
        CCLOG("finish send packet");
    }

    enet_peer_disconnect (peer, 0);
    enet_peer_reset (peer);
    CCLOG("exit thread send");
    return NULL;
}
Exemple #6
0
void CurlTest::recv(CCObject* pSender)
{
    CCLOG("waiting for connection");
    ENetEvent event;

    while (enet_host_service (server, & event, 10000) > 0) {
        if(event.type==ENET_EVENT_TYPE_CONNECT) {
            CCLOG("connected %x:%u.\n",
                  event.peer -> address.host,
                  event.peer -> address.port);
            char* temp;
            temp = "Client";
            event.peer -> data = temp;
            peer = event.peer;
            initStartGame();

            std::thread threadRecv(ThreadRecv);
            std::thread threadSend(ThreadSend);
            return;
            /*				pthread_t threadRecv;
            				pthread_t threadSend;
            				pthread_create(&threadRecv, NULL, &ThreadRecv, NULL);
            				pthread_create(&threadSend, NULL, &ThreadSend, NULL);	*/
        }
        if(event.type==ENET_EVENT_TYPE_DISCONNECT) {
            CCLOG("%s disconected.\n", event.peer -> data);
            enet_peer_reset (peer);
        }
    }
    pLabel-> setString("time out");
}
Exemple #7
0
/* The core API functions for a connection: 
  renet_connection_connect
  renet_connection_disconnect
  renet_connection_send_packet
  renet_connection_send_queued_packets
  renet_connection_update
  renet_connection_use_compression
  renet_connection_online
*/
VALUE renet_connection_connect(VALUE self, VALUE timeout)
{
  Connection* connection;
  Data_Get_Struct(self, Connection, connection);
  VALUE lock = rb_iv_get(self, "@lock");
  rb_mutex_lock(lock);

  VALUE rv = Qfalse;

  if (connection->online == 0)
  {
    connection->peer = enet_host_connect(connection->host, connection->address, connection->channels, 0);    
      
    if (connection->peer == NULL)
    {
      rb_raise(rb_eStandardError, "Cannot connect to remote host");
    }
    
    if (service(self, connection, NUM2UINT(timeout)) > 0 && connection->event->type == ENET_EVENT_TYPE_CONNECT)
    {
      connection->online = 1;
      renet_connection_execute_on_connection(self);
      rv = Qtrue;
    }
    else
    {
      enet_peer_reset(connection->peer);
    }
  }

  rb_mutex_unlock(lock);
  return rv;
}
Exemple #8
0
void NNetwork::Disconnect()
{
    if (!Host)
    {
        return;
    }
    enet_peer_disconnect(Host,0);
    ENetEvent Event;
    while (enet_host_service(Client,&Event,1000) > 0)
    {
        switch (Event.type)
        {
        case ENET_EVENT_TYPE_RECEIVE:
        {
            enet_packet_destroy(Event.packet);
            break;
        }
        case ENET_EVENT_TYPE_DISCONNECT:
        {
            GetGame()->GetLog()->Send("NETWORK",2,std::string("Successfully disconnected with ") + HostName + ".");
            Host = NULL;
            return;
        }
        default:
            GetGame()->GetLog()->Send("NET",1,"Certain events not implemented! FIXME");
            break;
        }
    }
    GetGame()->GetLog()->Send("NETWORK",1,"Server didn't respond to disconnect!");
    enet_peer_reset(Host);
    Host = NULL;
}
int NetworkManager::connexionToHost(std::string url,int port)
{
	enet_address_set_host (& address,url.c_str());
	address.port = port;
	peer = enet_host_connect (client, & address, 2, 0);
	if (peer == NULL)
	{
		return 1;
	}
	if (enet_host_service (client, &eventClient, 1000) > 0 && eventClient.type == ENET_EVENT_TYPE_CONNECT)
	{
		isConnectedflag = true;
		endThread = false;
		thread = boost::thread(&NetworkManager::threadConnexion, (void *) this);
		return 0;
	}
	else
	{
		/* Either the 5 seconds are up or a disconnect event was */
		/* received. Reset the peer in the event the 5 seconds   */
		/* had run out without any significant event.            */
		enet_peer_reset (peer);
		return 2 ;
	}
}
Exemple #10
0
void Network::InitializeClient(const char* ipAdress, const short port, const unsigned int maxDownstream, const unsigned int maxUpstream)
{
	std::cout << "Initializing client at port " << port << ".\n";

	_host = enet_host_create (NULL, 1, 2, maxDownstream, maxUpstream);

	if (_host == NULL)
		std::cout << "An error occurred while trying to create an ENet client host.\n";
	else
		std::cout << "Succesfully created ENet client host.\n";

	enet_address_set_host(&_address, ipAdress);
	_address.port = port;

	_peer = enet_host_connect(_host, &_address, 2, 0);

	if (_peer == NULL)
		std::cout << "No available peers for initiating an ENet connection.\n";

	// If connect send packages
	if (enet_host_service(_host, &_event, 1500) > 0 && _event.type == ENET_EVENT_TYPE_CONNECT)
	{
		_isConnected = true;
		printf("Connection to %s:%i succeeded.\n", ipAdress, _address.port);
		StartThreads();	
	}
	else
	{
		enet_peer_reset(_peer);
		printf("Connection to %s:%i failed.\n", ipAdress, _address.port);

	}
}
Exemple #11
0
NetworkPeer Network::Connect(const std::string& host, unsigned short port, NetworkCallback* callback)
{
    AutoLock<Mutex> lock(m_mutex);

    if(!m_host)
        return NetworkPeer(0);

    if(m_type != CLIENT)
    {
        std::cout << "Connect operation valid only if type is client" << std::endl;
        return NetworkPeer(0);
    }

    ENetAddress address;
    enet_address_set_host(&address, host.c_str());
    address.port = port;

    ENetPeer* peer = enet_host_connect((ENetHost*)m_host, &address, NETWORK_CHANNEL_COUNT, 0);    

    if(!peer)
    {
        std::cout << "Failed to connect to " << host << ":" << port << std::endl;
        return NetworkPeer(0);
    }

    m_networkCallback = callback;

    ENetEvent event;
    if(enet_host_service((ENetHost*)m_host, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
        return NetworkPeer(peer);

    enet_peer_reset(peer);
    std::cout << "Failed to connect to " << host << ":" << port << std::endl;
    return NetworkPeer(0);
}
Exemple #12
0
void CHostENet::Disconnect(CPeerENet* pPeer)
{
    ENetEvent event;

    enet_peer_disconnect (pPeer->GetENetPeer(),0);

    /* Allow up to 3 seconds for the disconnect to succeed and drop any packets received packets. */
    while (enet_host_service(m_Host, &event, 3000) > 0)
    {
        switch (event.type)
        {
        case ENET_EVENT_TYPE_RECEIVE:
            enet_packet_destroy (event.packet);
            break;
            
        case ENET_EVENT_TYPE_DISCONNECT:
            if (DEBUG_ENET)
                fprintf (stdout, "Disconnection succeeded.");
                DisconnectReceived(pPeer);
            return;
        }
    }

    /* We've arrived here, so the disconnect attempt didn't succeed yet.  Force the connection down.  */
    enet_peer_reset(pPeer->GetENetPeer());
    DisconnectReceived(pPeer);

    if (DEBUG_ENET)
        fprintf(stdout, "Disconnection Forced");

    if (m_PeerList.empty())
        m_Status = INIT_NOT_CONNECTED;
}
int NetworkManager::connexionToHost(std::string url,int port)
{
	/* Connect to some.server.net:1234. */
	    enet_address_set_host (& address,url.c_str());
	    address.port = port;

	    /* Initiate the connection, allocating the two channels 0 and 1. */
	    peer = enet_host_connect (client, & address, 2, 0);

	    if (peer == NULL)
	    {
	       return 1;
	    }

	    /* Wait up to 5 seconds for the connection attempt to succeed. */
	    if (enet_host_service (client, &eventClient, 5000) > 0 && eventClient.type == ENET_EVENT_TYPE_CONNECT)
	    {
	    	isConnectedflag = true;
            thread = boost::thread(&NetworkManager::threadConnexion, (void *) this);
	        return 0;
	    }
	    else
	    {
	        /* Either the 5 seconds are up or a disconnect event was */
	        /* received. Reset the peer in the event the 5 seconds   */
	        /* had run out without any significant event.            */
	        enet_peer_reset (peer);
	        return 2 ;
	    }
}
Exemple #14
0
bool udp_client::disconnect()
{
    if (!is_connected())
        return false;

    ENetEvent ev;
    enet_peer_disconnect(peer_, 0);

    {
        boost::lock_guard<boost::mutex> lock(host_mutex_);
        while (enet_host_service(host_, &ev, 3000)) {
            switch (ev.type) {
            case ENET_EVENT_TYPE_DISCONNECT:
                connected_ = false;
                log_msg("udp_client disconnected");
                return true;

            default:
                enet_packet_destroy(ev.packet);
            }
        }
    }

    log_msg("udp_client disconnect failed");
    enet_peer_reset(peer_);
    return false;
}
Exemple #15
0
void CC_Client::connect(const char *host=HOST, uint16_t port=PORT) {
    if (connected) {
        printf("Connected already.\n");
        return;
    }
    
    enet_address_set_host(&address, host);
    address.port = port;
    
    peer = enet_host_connect(client, &address, 2, 0);
    
    if (peer == NULL) {
        fprintf(stderr, "No available peers to connect\n");
        exit(EXIT_FAILURE);
    }
    
    if (enet_host_service(client, &evt, 5000) > 0
        && evt.type == ENET_EVENT_TYPE_CONNECT) {
        printf("Connected to server %s:%u\n", HOST, PORT);
        connected = true;
    } else {
        enet_peer_reset(peer);
        printf("Connection failed to %s:%u\n", HOST, PORT);
    }
    
    //~ conn_t_ret = pthread_create(&conn_t, NULL, CC_Client::loop, (void*)this);
}
Exemple #16
0
void Network::Update()
{
	ENetEvent netEvent;
	Event* fwEvent;

	while( enet_host_service( localHost, &netEvent, 0 ) > 0 )
	{
		switch( netEvent.type )
		{
			case ENET_EVENT_TYPE_CONNECT:
				fwEvent = new Event();
				fwEvent->Type = EVENT_NETWORK_CONNECTION_REQUEST;
				memcpy( &fwEvent->Data.Network.Traffic, &netEvent, sizeof(ENetEvent) );
				Framework::System->PushEvent( fwEvent );
				break;

			case ENET_EVENT_TYPE_RECEIVE:
				fwEvent = new Event();
				fwEvent->Type = EVENT_NETWORK_PACKET_RECEIVED;
				memcpy( &fwEvent->Data.Network.Traffic, &netEvent, sizeof(ENetEvent) );
				Framework::System->PushEvent( fwEvent );
				break;

			case ENET_EVENT_TYPE_DISCONNECT:
				enet_peer_reset( netEvent.peer );
				fwEvent = new Event();
				fwEvent->Type = EVENT_NETWORK_DISCONNECTED;
				memcpy( &fwEvent->Data.Network.Traffic, &netEvent, sizeof(ENetEvent) );
				Framework::System->PushEvent( fwEvent );
				break;
		}
	}
}
Exemple #17
0
Network::Network( std::string Server, int Port )
{
	localHost = enet_host_create (NULL /* create a client host */,
		1 /* only allow 1 outgoing connection */,
		1 /* allow up 2 channels to be used, 0 and 1 */,
		// 57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
		// 14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
		0 /* assume any amount of incoming bandwidth */,
		0 /* assume any amount of outgoing bandwidth */);
	if( localHost == 0 )
	{
		return;
	}
	enet_address_set_host( &serverAddress, Server.c_str() );
	serverAddress.port = Port;
	networkPeer = enet_host_connect( localHost, &serverAddress, 1, 0 );
	if( networkPeer == 0 )
	{
		enet_host_destroy( localHost );
		localHost = 0;
		return;
	}

	/* Wait up to 6 seconds for the connection attempt to succeed. */
	ENetEvent ev;
	if( enet_host_service( localHost, &ev, 6000 ) > 0 && ev.type == ENET_EVENT_TYPE_CONNECT )
	{
	} else {
		enet_peer_reset( networkPeer );
		networkPeer = 0;
		enet_host_destroy( localHost );
		localHost = 0;
		return;
	}
}
Exemple #18
0
/** Request a disconnection from a peer.
    @param peer peer to request a disconnection
    @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
    once the disconnection is complete.
*/
void
enet_peer_disconnect (ENetPeer * peer)
{
    ENetProtocol command;

    if (peer -> state == ENET_PEER_STATE_DISCONNECTING ||
        peer -> state == ENET_PEER_STATE_DISCONNECTED ||
        peer -> state == ENET_PEER_STATE_ZOMBIE)
      return;

    enet_peer_reset_queues (peer);

    command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
    command.header.channelID = 0xFF;
    command.header.flags = ENET_PROTOCOL_FLAG_UNSEQUENCED;
    command.header.commandLength = sizeof (ENetProtocolDisconnect);

    if (peer -> state == ENET_PEER_STATE_CONNECTED)
      command.header.flags = ENET_PROTOCOL_FLAG_ACKNOWLEDGE;

    enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);

    if (peer -> state == ENET_PEER_STATE_CONNECTED)
      peer -> state = ENET_PEER_STATE_DISCONNECTING;
    else
    {
        enet_host_flush (peer -> host);
        enet_peer_reset (peer);
    }
}
Exemple #19
0
/** Request a disconnection from a peer.
    @param peer peer to request a disconnection
    @param data data describing the disconnection
    @remarks An ENET_EVENT_DISCONNECT event will be generated by enet_host_service()
    once the disconnection is complete.
*/
void
enet_peer_disconnect (ENetPeer * peer, enet_uint32 data)
{
    ENetProtocol command;

    if (peer -> state == ENET_PEER_STATE_DISCONNECTING ||
        peer -> state == ENET_PEER_STATE_DISCONNECTED ||
        peer -> state == ENET_PEER_STATE_ACKNOWLEDGING_DISCONNECT ||
        peer -> state == ENET_PEER_STATE_ZOMBIE)
      return;

    enet_peer_reset_queues (peer);

    command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
    command.header.channelID = 0xFF;
    command.disconnect.data = ENET_HOST_TO_NET_32 (data);

    if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
      command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_ACKNOWLEDGE;
    else
      command.header.command |= ENET_PROTOCOL_COMMAND_FLAG_UNSEQUENCED;      
    
    enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);

    if (peer -> state == ENET_PEER_STATE_CONNECTED || peer -> state == ENET_PEER_STATE_DISCONNECT_LATER)
      peer -> state = ENET_PEER_STATE_DISCONNECTING;
    else
    {
        enet_host_flush (peer -> host);
        enet_peer_reset (peer);
    }
}
Exemple #20
0
int joynet_connect_client_to_server(JOYNET_CLIENT * cp, char * host, int port)
{
	ENetEvent event;
	
	if(enet_address_set_host(&cp->address, host) < 0)
	{
		return 0;
	}
	cp->address.port = port;
	cp->host = enet_host_create(NULL, 1, 0, 0, 0);
	if(!cp->host)
	{
		return 0;
	}
	cp->peer = enet_host_connect(cp->host, &cp->address, 4, 0);
	if(!cp->peer)
	{
		return 0;
	}
	if(enet_host_service(cp->host, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)
    {
		return 1;
    }
    else
    {
        enet_peer_reset(cp->peer);
    }
	return 0;
}
Exemple #21
0
 Client::~Client()
 {
     if(m_serverPeer != nullptr)
     {
         enet_peer_disconnect(m_serverPeer, 0);
         ENetEvent event;
         bool force = false;
         while(enet_host_service(m_client, &event, 3000))
         {
             switch(event.type)
             {
                 case ENET_EVENT_TYPE_DISCONNECT:
                     LOG(INFO) << "Disconnected successfully.";
                     force = false;
                     break;
                 case ENET_EVENT_TYPE_RECEIVE:
                     enet_packet_destroy(event.packet);
                     break;
                 default:
                     force = true;
                     break;
             }
         }
         if(force)
         {
             enet_peer_reset(m_serverPeer);
             m_serverPeer = nullptr;
         }
     }
     enet_host_destroy(m_client);
     LOG(INFO) << "Client destroyed.";
 }
void NetworkManager::disconnexion()
{
	if(peer != NULL)
	{
		if(isConnectedflag)
		{
			isConnectedflag = false;
		}
		enet_peer_disconnect (peer, 0);
	    /* Allow up to 3 seconds for the disconnect to succeed
	       and drop any packets received packets.
	     */
		while (enet_host_service (client, & eventClient, 3000) > 0)
		{
			switch (eventClient.type)
			{
			case ENET_EVENT_TYPE_RECEIVE:
				enet_packet_destroy (eventClient.packet);
				break;
			case ENET_EVENT_TYPE_DISCONNECT:
				puts ("Disconnection succeeded.");
				break;
			default:
				break;
			}
		}

		enet_peer_reset (peer);
	}

}
Exemple #23
0
void disconnect(bool async, bool cleanup)
{
    if(curpeer)
    {
        if(!discmillis)
        {
            enet_peer_disconnect(curpeer, DISC_NONE);
            enet_host_flush(clienthost);
            discmillis = totalmillis;
        }
        if(curpeer->state!=ENET_PEER_STATE_DISCONNECTED)
        {
            if(async) return;
            enet_peer_reset(curpeer);
        }
        curpeer = NULL;
        discmillis = 0;
        conoutf("disconnected");
        game::gamedisconnect(cleanup);
        setvar("mainmenu", 1);
    }
    if(!connpeer && clienthost)
    {
        enet_host_destroy(clienthost);
        clienthost = NULL;
    }
}
Exemple #24
0
/** Force an immediate disconnection from a peer.
    @param peer peer to disconnect
    @remarks No ENET_EVENT_DISCONNECT event will be generated. The foreign peer is not
    guarenteed to receive the disconnect notification, and is reset immediately upon
    return from this function.
*/
void
enet_peer_disconnect_now (ENetPeer * peer)
{
    ENetProtocol command;

    if (peer -> state != ENET_PEER_STATE_DISCONNECTED)
      return;

    if (peer -> state != ENET_PEER_STATE_ZOMBIE &&
        peer -> state != ENET_PEER_STATE_DISCONNECTING)
    {
        enet_peer_reset_queues (peer);

        command.header.command = ENET_PROTOCOL_COMMAND_DISCONNECT;
        command.header.channelID = 0xFF;
        command.header.flags = 0;
        command.header.commandLength = sizeof (ENetProtocolDisconnect);

        enet_peer_queue_outgoing_command (peer, & command, NULL, 0, 0);

        enet_host_flush (peer -> host);
    }

    enet_peer_reset (peer);
}
void NetPlayClient::Disconnect()
{
	ENetEvent netEvent;
	m_connecting = false;
	m_state = Failure;
	if (m_server)
		enet_peer_disconnect(m_server, 0);
	else
		return;

	while (enet_host_service(m_client, &netEvent, 3000) > 0)
	{
		switch (netEvent.type)
		{
		case ENET_EVENT_TYPE_RECEIVE:
			enet_packet_destroy(netEvent.packet);
			break;
		case ENET_EVENT_TYPE_DISCONNECT:
			m_server = nullptr;
			return;
		default:
			break;
		}
	}
	//didn't disconnect gracefully force disconnect
	enet_peer_reset(m_server);
	m_server = nullptr;
}
Exemple #26
0
void networkEngine::clientConnect()
{
    //gameEngine *gameE = gameEngine::Instance();
    boost::shared_ptr<gameEngine> gameE = gameEngine::Instance();

    if (!clientEstablishedConnection)
    {

        client = enet_host_create (NULL /* create a client host */,
                                   4 /* only allow 1 outgoing connection */,
                                   2 /* allow up to 2 channels to be used, 0 and 1*/,
                                   0 /* 56K modem with 56 Kbps downstream bandwidth */,
                                   0 /* 56K modem with 14 Kbps upstream bandwidth */);

        if (client == NULL)
        {
            logMsg("An error occurred while trying to create an ENet client host.");
            exit (EXIT_FAILURE);
        }

// Old Pre-GUI ipAddress input code
//    string ipAddress;
//    cout << "IP Address: " << endl;
//    cin >> ipAddress;

        /* Connect to some.server.net:1234. */
        enet_address_set_host (& serverAddress, ipAddress.c_str());
        serverAddress.port = 1234;

        /* Initiate the connection, allocating the two channels 0 and 1. */
        peer = enet_host_connect (client, & serverAddress, 2, 0);

        if (peer == NULL)
        {
            logMsg("No available peers for initiating an ENet connection.");
            exit (EXIT_FAILURE);
        }

        /* Wait up to 5 seconds for the connection attempt to succeed. */
        if (enet_host_service (client, & event, 5000) > 0 &&
                event.type == ENET_EVENT_TYPE_CONNECT)
        {
            logMsg("Connection to " +ipAddress +":1234 succeeded.");
            clientEstablishedConnection = true; // Tells other code that this instance is a network client
        }
        else
        {
            /* Either the 5 seconds are up or a disconnect event was */
            /* received. Reset the peer in the event the 5 seconds   */
            /* had run out without any significant event.            */
            enet_peer_reset (peer);

            logMsg("Connection to " +ipAddress +":1234 failed.");
        }
        gameE->setClientRunning(true);
        clientEstablishedConnection = true;
    }
}
Exemple #27
0
void ChopClient (unsigned int clientID) //do not confuse with killClient, this is a harsh disconnect
	{
		ClientMap::iterator iter = clients.find(clientID);
		if (iter != clients.end())
		{
			enet_peer_reset(iter->second); //disconnect now, regardless of what the client does
			clients.erase(iter);
		}
	}
Exemple #28
0
void Network::Disconnect()
{
	if( IsActive() && IsConnected() )
	{
		enet_peer_disconnect( networkPeer, 0 );
		enet_peer_reset( networkPeer );
		networkPeer = 0;
	}
}
 bool Client::_F_reset_connection()
 {
     if(_V_client_connection)
     {
         enet_peer_reset(_V_client_connection);
         return true;
     }
     return false;
 }
Exemple #30
0
void abortconnect()
{
    if(!connpeer) return;
    if(connpeer->state!=ENET_PEER_STATE_DISCONNECTED) enet_peer_reset(connpeer);
    connpeer = NULL;
    if(curpeer) return;
    enet_host_destroy(clienthost);
    clienthost = NULL;
}