Esempio n. 1
0
        void ServerBase::update(void)
        {
            if (!mIsRunning)
                return;

            ENetEvent event;
            while (enet_host_service(mInternalHost, &event, 0) > 0)
            {
                switch(event.type)
                {
                    case ENET_EVENT_TYPE_CONNECT:
                    {
                        IncomingClientBase *client = new Kiaro::Network::IncomingClientBase(event.peer, this);
                        event.peer->data = client;

                        mConnectedClientSet.insert(mConnectedClientSet.end(), (size_t)client);
                        onClientConnected(client);

                        break;
                    }

                    case ENET_EVENT_TYPE_DISCONNECT:
                    {
                        Kiaro::Network::IncomingClientBase *disconnected = (Kiaro::Network::IncomingClientBase*)event.peer->data;
                        onClientDisconnected(disconnected);

                        mConnectedClientSet.erase((size_t)disconnected);
                        delete disconnected;

                        break;
                    }

                    case ENET_EVENT_TYPE_RECEIVE:
                    {
                        if (!event.peer->data)
                        {
                            enet_packet_destroy(event.packet);
                            throw std::runtime_error("ServerBase: Invalid ENet peer data on packet receive!");
                        }

                        Kiaro::Network::IncomingClientBase *sender = (Kiaro::Network::IncomingClientBase*)event.peer->data;
                        Kiaro::Support::BitStream incomingStream(event.packet->data, event.packet->dataLength, event.packet->dataLength);

                        onReceivePacket(incomingStream, sender);
                        enet_packet_destroy(event.packet);

                        break;
                    }

                    case ENET_EVENT_TYPE_NONE:
                        break;
                }
            }
        }
Esempio n. 2
0
// ----------------------------------------------------------------------------
ENetPacket* Crypto::encryptSend(BareNetworkString& ns, bool reliable)
{
    // 4 bytes counter and 4 bytes tag
    ENetPacket* p = enet_packet_create(NULL, ns.m_buffer.size() + 8,
        (reliable ? ENET_PACKET_FLAG_RELIABLE :
        (ENET_PACKET_FLAG_UNSEQUENCED | ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT))
        );
    if (p == NULL)
        return NULL;

    std::array<uint8_t, 12> iv = {};
    std::unique_lock<std::mutex> ul(m_crypto_mutex);

    uint32_t val = ++m_packet_counter;
    if (NetworkConfig::get()->isClient())
        memcpy(iv.data(), &val, 4);
    else
        memcpy(iv.data() + 4, &val, 4);

    uint8_t* packet_start = p->data + 8;

    if (EVP_EncryptInit_ex(m_encrypt, NULL, NULL, NULL, iv.data()) != 1)
    {
        enet_packet_destroy(p);
        return NULL;
    }

    int elen;
    if (EVP_EncryptUpdate(m_encrypt, packet_start, &elen, ns.m_buffer.data(),
        (int)ns.m_buffer.size()) != 1)
    {
        enet_packet_destroy(p);
        return NULL;
    }
    if (EVP_EncryptFinal_ex(m_encrypt, unused_16_blocks.data(), &elen) != 1)
    {
        enet_packet_destroy(p);
        return NULL;
    }
    if (EVP_CIPHER_CTX_ctrl(m_encrypt, EVP_CTRL_GCM_GET_TAG, 4, p->data + 4)
        != 1)
    {
        enet_packet_destroy(p);
        return NULL;
    }
    ul.unlock();

    memcpy(p->data, &val, 4);
    return p;
}   // encryptSend
Esempio n. 3
0
void Purity::Server::handleEvents()
{
    ENetEvent event;
    NetworkAction action;

    while (enet_host_service(mHost, &event, 0) > 0)
    {
        switch (event.type)
        {
        case ENET_EVENT_TYPE_CONNECT:
            std::cout << "New peer!" << std::endl;
            break;

        case ENET_EVENT_TYPE_DISCONNECT:
            std::cout << "Peer disconnected!" << std::endl;
            break;
        case ENET_EVENT_TYPE_RECEIVE:
            memcpy(&action, event.packet->data, event.packet->dataLength);

            std::cout << action.objectName << " is performing " << action.actionName << std::endl;

            mReceivedActionQueue->push(action);

            enet_packet_destroy(event.packet);

            break;

        default:
            std::cout << "Default!\n";
        }
    }
}
void NetworkLayerENet::listen(ENetHost* host, ENetEvent& last_host_event, NetworkEventHandler& listener)
{
    while (enet_host_service(host, &last_host_event, 0) > 0)
    {
        switch (last_host_event.type)
        {
            case ENET_EVENT_TYPE_CONNECT:
            {
                NetPeerENet* peer = new NetPeerENet(last_host_event.peer);
                listener.onConnect(peer);
            }
            break;

            case ENET_EVENT_TYPE_RECEIVE:
            {
                NetPeerENet peer(last_host_event.peer);
                listener.onReceive(peer, last_host_event.packet->data, last_host_event.packet->dataLength);
                enet_packet_destroy(last_host_event.packet);
            }
            break;

            case ENET_EVENT_TYPE_DISCONNECT:
            {
                NetPeerENet peer(last_host_event.peer);
                listener.onDisconnect(peer);
            }
            break;

            default:
            break;
        }
    }
}
Esempio n. 5
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;
}
Esempio n. 6
0
static void
enet_peer_remove_incoming_commands (ENetList * queue, ENetListIterator startCommand, ENetListIterator endCommand)
{
    ENetListIterator currentCommand;    
    
    for (currentCommand = startCommand; currentCommand != endCommand; )
    {
       ENetIncomingCommand * incomingCommand = (ENetIncomingCommand *) currentCommand;

       currentCommand = enet_list_next (currentCommand);

       enet_list_remove (& incomingCommand -> incomingCommandList);
 
       if (incomingCommand -> packet != NULL)
       {
          -- incomingCommand -> packet -> referenceCount;

          if (incomingCommand -> packet -> referenceCount == 0)
            enet_packet_destroy (incomingCommand -> packet);
       }

       if (incomingCommand -> fragments != NULL)
         enet_free (incomingCommand -> fragments);

       enet_free (incomingCommand);
    }
}
Esempio n. 7
0
    bool Client::update()
    {
        bool dataChanged = false;
        ENetEvent event;

        while(enet_host_service(m_client, &event, 2))
        {
            switch(event.type)
            {
                case ENET_EVENT_TYPE_CONNECT:
                    LOG(INFO) << "Client connected successfully to " << event.peer->address.host
                              << ":" << event.peer->address.port << ".";
                    m_connected = true;
                    dataChanged = true;
                    break;
                case ENET_EVENT_TYPE_DISCONNECT:
                    event.peer->data = nullptr;
                    LOG(INFO) << "Client disconnected.";
                    m_serverPeer = nullptr;
                    m_connected = false;
                    dataChanged = true;
                    break;
                case ENET_EVENT_TYPE_NONE:
                    //Nothing happened
                    break;
                case ENET_EVENT_TYPE_RECEIVE:
                    dataChanged = receivePacket(event.packet, event.peer);
                    enet_packet_destroy(event.packet);
                    break;
            }
        }
        return dataChanged;
    }
Esempio n. 8
0
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;
}
Esempio n. 9
0
void udp_server::poll (uint16_t milliseconds)
{
    ENetEvent ev;
    auto result (enet_host_service(sv_, &ev, milliseconds));

    if (result < 0)
        throw std::runtime_error((format("network error %1%") % -result).str());

    switch (ev.type)
    {
        case ENET_EVENT_TYPE_CONNECT:
            on_connect(ev.peer);
            break;

        case ENET_EVENT_TYPE_RECEIVE:
            on_receive(ev.peer, packet(ev.packet->data, ev.packet->dataLength));
            enet_packet_destroy(ev.packet);
            break;

        case ENET_EVENT_TYPE_DISCONNECT:
            on_disconnect(ev.peer);
            break;

        case ENET_EVENT_TYPE_NONE:
            break;
    }
}
Esempio n. 10
0
void net_controller_free(controller *ctrl) {
    wtf *data = ctrl->data;
    ENetEvent event;
    if (!data->disconnected) {
        DEBUG("closing connection");
        enet_peer_disconnect(data->peer, 0);

        while (enet_host_service(data->host, &event, 3000) > 0) {
            switch (event.type) {
                case ENET_EVENT_TYPE_RECEIVE:
                    enet_packet_destroy(event.packet);
                    break;
                case ENET_EVENT_TYPE_DISCONNECT:
                    DEBUG("got disconnect notice");
                    // peer has acknowledged the disconnect
                    goto done;
                    break;
                default:
                    break;
            }
        }
    }
done:
    if(data->host) {
        enet_host_destroy(data->host);
        data->host = NULL;
    }
    if(ctrl->data) {
        free(ctrl->data);
        ctrl->data = NULL;
    }
}
Esempio n. 11
0
void Server::updateNet() {
	ENetEvent event;
	while (enet_host_service(host, &event, 0) > 0) {
		switch (event.type) {
		case ENET_EVENT_TYPE_CONNECT:
			handleConnect(event.peer);
			break;
		case ENET_EVENT_TYPE_DISCONNECT:
			handleDisconnect(event.peer, (DisconnectReason) event.data);
			break;
		case ENET_EVENT_TYPE_RECEIVE:
			handlePacket(
				event.packet->data,
				event.packet->dataLength,
				event.channelID,
				event.peer
				);
			enet_packet_destroy(event.packet);
			break;
		default:
			LOG_WARNING(logger) << "Received unknown ENetEvent type << event.type";
			break;
		}
	}
}
Esempio n. 12
0
	bool NetworkHost::update()
	{
		if (!host)
			return false;
		
		ENetEvent event;
		while (enet_host_service(host, &event, 0) > 0)
		{
			switch (event.type)
			{
			case ENET_EVENT_TYPE_CONNECT:
			{
				NetworkConnection *newconn = new NetworkConnection(event.peer);
				event.peer->data = newconn;
				newconnections.push(newconn);
				break;
			}
			case ENET_EVENT_TYPE_RECEIVE:
			{
				// Push received data into connection
				Buffer *buffer = new Buffer(event.packet->data, event.packet->dataLength, true);
				NetworkConnection* con = ((NetworkConnection*) event.peer->data);
				con->injectData((BufferPointer) buffer);
				enet_packet_destroy(event.packet);
				break;
			}
			case ENET_EVENT_TYPE_DISCONNECT:
				((NetworkConnection*) event.peer->data)->close();
				break;
			default:
				break;
			}
		}
		return true;
	}
Esempio n. 13
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;
}
Esempio n. 14
0
void* NetworkManager::threadConnexion(void* arguments)
{
	NetworkManager * networkManager = static_cast<NetworkManager *>(arguments) ;

	while ((enet_host_service (networkManager->client,&networkManager->eventClient, 10) >= 0 ) && (networkManager->endThread == false )  )
	{
		switch (networkManager->eventClient.type)
		{
		case ENET_EVENT_TYPE_CONNECT:
			networkManager->callback((XSILIUM_ALL * 1000) + ID_CONNEXION);
			break;
		case ENET_EVENT_TYPE_RECEIVE:
		{
			MessagePacket * message = new MessagePacket();

			std::istringstream archive_stream(std::string((char*)networkManager->eventClient.packet->data));
			boost::archive::text_iarchive archive(archive_stream);
			archive >> message;

			networkManager->callback(message->getOpcode(),message);
			break;
		}

		case ENET_EVENT_TYPE_DISCONNECT:
			networkManager->callback((XSILIUM_ALL * 1000 ) + ID_DECONEXION);
			networkManager->endThread = true;
			break;
		default:
			break;
		}
        enet_packet_destroy (networkManager->eventClient.packet);
	}
	return NULL;
}
Esempio n. 15
0
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);
	}

}
Esempio n. 16
0
    void Host::process()
    {
        // check for data
        ENetEvent event;
        while (enet_host_service(mClient, &event, 0) > 0)
        {
            switch (event.type)
            {
            case ENET_EVENT_TYPE_CONNECT:
                {
                    mConnected = true;
                } break;

            case ENET_EVENT_TYPE_RECEIVE:
                {
                    Packet *packet = new Packet((char*)event.packet->data,
                                                event.packet->dataLength);
                    mPackets.push_back(packet);
                    enet_packet_destroy (event.packet);
                } break;

            case ENET_EVENT_TYPE_DISCONNECT:
                {
                    mConnected = false;
                } break;
            }
        }
    }
Esempio n. 17
0
Event::~Event()
{
	if( Type == EVENT_FORM_INTERACTION )
	{
		if( Data.Forms.AdditionalData != nullptr )
		{
			delete Data.Forms.AdditionalData;
		}
	}

#ifdef NETWORK_SUPPORT
	if( Type == EVENT_NETWORK_PACKET_RECEIVED )
	{
		enet_packet_destroy( Data.Network.Traffic.packet );
	}
#endif

#ifdef DOWNLOAD_SUPPORT
	if( Type == EVENT_DOWNLOAD_COMPLETE || Type == EVENT_DOWNLOAD_PROGRESS )
	{
		if( URL != 0 )
		{
			delete URL;
		}
		if( Contents != 0 )
		{
			delete Contents;
		}
	}
#endif

}
Esempio n. 18
0
int main (int argc, char * const argv[]) {
    
    if(enet_initialize() != 0)
	{
		printf("An error occurred while initializing ENet.\n");
	}
	
	ENetAddress address;
	ENetHost* server;
	
	address.host = ENET_HOST_ANY;
	address.port = 9050;
	
	// up to 32 clients with unlimited bandwidth
	server = enet_host_create(&address, 32, 0, 0);
	if(server == NULL)
	{
		printf("An error occurred while trying to create an ENet server host.\n");
	}
	
	int clients = 0;
	
	// server main loop
	while(true)
	{
		ENetEvent event;
		while(enet_host_service(server, &event, 1000) > 0)
		{
			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);
					
					clients++;
					
					break;
					
				case ENET_EVENT_TYPE_RECEIVE:
					printf ("A packet of length %u containing %s was received.\n",
							event.packet->dataLength,
							event.packet->data);
					
					ENetPacket* packet = enet_packet_create(event.packet->data, event.packet->dataLength, ENET_PACKET_FLAG_RELIABLE);
					enet_host_broadcast(server, 0, packet);
					enet_host_flush(server);
					
					enet_packet_destroy(event.packet);
					break;
					
				case ENET_EVENT_TYPE_DISCONNECT:
					printf("Client disconnected.\n");
					event.peer->data = NULL;
			}
		}
	}
	
	return 0;
}
Esempio n. 19
0
Event::~Event()
{

#ifdef NETWORK_SUPPORT
	if( Type == EVENT_NETWORK_PACKET_RECEIVED )
	{
		enet_packet_destroy( Data.Network.Traffic.packet );
	}
#endif

#ifdef DOWNLOAD_SUPPORT
	if( Type == EVENT_DOWNLOAD_COMPLETE || Type == EVENT_DOWNLOAD_PROGRESS )
	{
		if( Data.Download.URL != nullptr )
		{
			delete Data.Download.URL;
		}
		if( Data.Download.Contents != nullptr )
		{
			delete Data.Download.Contents;
		}
	}
#endif

}
Esempio n. 20
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;
	}
Esempio n. 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.";
 }
Esempio n. 22
0
void network_handle_packet(int wait_time)
{
  ENetEvent event;
  gchar *command, *args;

  enet_host_service(network->server, &event, wait_time);
  switch (event.type) {
  case ENET_EVENT_TYPE_CONNECT:
    event.peer->data = (void *) tetris_id_new();
    tetris_plugin_action(PLUGIN_CONNECT, (int) event.peer->data,
                         NULL, NULL);
    network_add_client(event.peer);
    break;
  case ENET_EVENT_TYPE_RECEIVE:
    tetris_extract_command((const gchar *) event.packet->data,
                           event.packet->dataLength,
                           &command, &args);
    tetris_plugin_action(PLUGIN_RECV, (int) event.peer->data,
                         command, args);
    g_free(command);
    g_free(args);
    enet_packet_destroy(event.packet);
    break;
  case ENET_EVENT_TYPE_DISCONNECT:
    tetris_plugin_action(PLUGIN_DISCONNECT, (int) event.peer->data,
                         NULL, NULL);
    network_remove_client(event.peer);
    tetris_id_free((int) event.peer->data);
    event.peer->data = NULL;
    break;
  case ENET_EVENT_TYPE_NONE:
    break;
  }
}
Esempio n. 23
0
void udp_client::poll(unsigned int milliseconds)
{
    ENetEvent ev;
    int result;
    {
        boost::lock_guard<boost::mutex> lock(host_mutex_);
        result = enet_host_service(host_, &ev, milliseconds);
    }

    if (result < 0)
        throw std::runtime_error(
            (format("network error %1%") % -result).str());

    switch (ev.type) {
    case ENET_EVENT_TYPE_CONNECT:
        on_connect();
        break;

    case ENET_EVENT_TYPE_RECEIVE:
        receive(packet(ev.packet->data, ev.packet->dataLength));
        break;

    case ENET_EVENT_TYPE_DISCONNECT:
        on_disconnect();
        break;

    case ENET_EVENT_TYPE_NONE:
        break;
    }

    if (ev.packet != nullptr)
        enet_packet_destroy(ev.packet);
}
Esempio n. 24
0
/**
	Destroy an allocated ENetEvent struct
**/
static void destroy_enetevent( value e ) {
	ENetEvent *event;
	if( !val_is_abstract(e) || !val_is_kind(e,k_udprevent) )
		return;
	event = (ENetEvent *)val_data(e);
	if(e == NULL)
		return;

	// enet_packet_destroy frees the packet itself.
#ifdef ENET_DEBUG
printf("*** destroy_enetevent freeing packet\n");
#endif
	if(event->packet != NULL)
		enet_packet_destroy (event->packet);
	//if(event->type == ENET_EVENT_TYPE_DISCONNECT
		//&& event->peer->data != NULL)
			//enet_free(event->peer -> data);
#ifdef ENET_DEBUG
//printf("*** destroy_enetevent freeing event\n");
#endif
	enet_free(event);
#ifdef ENET_DEBUG
//printf("*** destroy_enetevent done.\n");
#endif
	return;
}
Esempio n. 25
0
 bool Client::disconnect_from_server()
 {
     if(_V_status_connected and _V_client_connection)
     {
         enet_peer_disconnect(_V_client_connection,0);
         while(enet_host_service(_V_client_host,&_V_event,_V_time)>0)
         {
             switch (_V_event.type)
             {
                 case ENET_EVENT_TYPE_RECEIVE:
                 {
                     enet_packet_destroy(_V_event.packet);
                     break;
                 }
                 case ENET_EVENT_TYPE_DISCONNECT:
                     return true;
                 default:
                     break;
             }
         }
         _V_client_connection=nullptr;
         return true;
     }
     return false;
 }
Esempio n. 26
0
File: main.c Progetto: turnage/Charl
/**
 *  The primary loop and logic. Listen for packets and process them accordingly.
 */
void run (void)
{
        printf("Server running normally...\n");

        ENetEvent *ev = NULL;
        client *cli = NULL;
        u64 nullpacks = 0;
        connected = 1;

        do {
                ev = host_listen(500);

                if (!ev) {
                        nullpacks++;
                } else if (ev->type == ENET_EVENT_TYPE_CONNECT) {
                        ev->peer->data = client_new();
                        cli = ev->peer->data;
                        cli->peer = ev->peer;
                        fprintf(stderr, "Client connected.\n");
                } else if (ev->type == ENET_EVENT_TYPE_RECEIVE) {
                        fprintf(stderr, "Packet received.\n");
                        handle_event(ev);
                        enet_packet_destroy(ev->packet);
                } else if (ev->type == ENET_EVENT_TYPE_DISCONNECT) {
                        cli = ev->peer->data;
                        host_list(cli->channel);
                        client_destroy(cli);
                        fprintf(stderr, "Client left.\n");
                } else {
                        fprintf(stderr, "Unrecognized event.\n");
                }

        } while (connected);
}
Esempio n. 27
0
void sendmap(char *mapname)
{
    if(*mapname) save_world(mapname);
    changemap(mapname);
    mapname = getclientmap();
    int mapsize;
    uchar *mapdata = readmap(mapname, &mapsize); 
    if(!mapdata) return;
    ENetPacket *packet = enet_packet_create(NULL, MAXTRANS + mapsize, ENET_PACKET_FLAG_RELIABLE);
    uchar *start = packet->data;
    uchar *p = start+2;
    putint(p, SV_SENDMAP);
    sendstring(mapname, p);
    putint(p, mapsize);
    if(65535 - (p - start) < mapsize)
    {
        conoutf("map %s is too large to send", mapname);
        free(mapdata);
        enet_packet_destroy(packet);
        return;
    };
    memcpy(p, mapdata, mapsize);
    p += mapsize;
    free(mapdata); 
    *(ushort *)start = ENET_HOST_TO_NET_16(p-start);
    enet_packet_resize(packet, p-start);
    sendpackettoserv(packet);
    conoutf("sending map %s to server...", mapname);
    sprintf_sd(msg)("[map %s uploaded to server, \"getmap\" to receive it]", mapname);
    toserver(msg);
}
Esempio n. 28
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;
}
Esempio n. 29
0
    void RemoteInterface::update() {
        ENetEvent event;
        while(host && enet_host_service(host, &event, 0)>0) {
            switch(event.type)
            {
                case ENET_EVENT_TYPE_CONNECT:
                    std::cout << "[DEBUG] RemoteInterface::update - connected to server" << std::endl;

                    break;

                case ENET_EVENT_TYPE_RECEIVE:
                {
                    packetbuf p(event.packet);
                    int chan = event.channelID;
                    while(p.remaining()) {
                        receive( -1, chan, p);
                    }
                    enet_packet_destroy(event.packet);
                    break;
                }
                case ENET_EVENT_TYPE_DISCONNECT:
                    std::cout << "[DEBUG] RemoteInterface::update - ENET_EVENT_TYPE_DISCONNECT" << std::endl;
                    disconnect();
                    break;

                default:
                    break;
            }
        }
    }
Esempio n. 30
0
void Room::RoomImpl::HandleWifiPacket(const ENetEvent* event) {
    Packet in_packet;
    in_packet.Append(event->packet->data, event->packet->dataLength);
    in_packet.IgnoreBytes(sizeof(u8));         // Message type
    in_packet.IgnoreBytes(sizeof(u8));         // WifiPacket Type
    in_packet.IgnoreBytes(sizeof(u8));         // WifiPacket Channel
    in_packet.IgnoreBytes(sizeof(MacAddress)); // WifiPacket Transmitter Address
    MacAddress destination_address;
    in_packet >> destination_address;

    Packet out_packet;
    out_packet.Append(event->packet->data, event->packet->dataLength);
    ENetPacket* enet_packet = enet_packet_create(out_packet.GetData(), out_packet.GetDataSize(),
                                                 ENET_PACKET_FLAG_RELIABLE);

    if (destination_address == BroadcastMac) { // Send the data to everyone except the sender
        std::lock_guard<std::mutex> lock(member_mutex);
        bool sent_packet = false;
        for (const auto& member : members) {
            if (member.peer != event->peer) {
                sent_packet = true;
                enet_peer_send(member.peer, 0, enet_packet);
            }
        }

        if (!sent_packet) {
            enet_packet_destroy(enet_packet);
        }
    } else { // Send the data only to the destination client
        std::lock_guard<std::mutex> lock(member_mutex);
        auto member = std::find_if(members.begin(), members.end(),
                                   [destination_address](const Member& member) -> bool {
                                       return member.mac_address == destination_address;
                                   });
        if (member != members.end()) {
            enet_peer_send(member->peer, 0, enet_packet);
        } else {
            LOG_ERROR(Network,
                      "Attempting to send to unknown MAC address: "
                      "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
                      destination_address[0], destination_address[1], destination_address[2],
                      destination_address[3], destination_address[4], destination_address[5]);
            enet_packet_destroy(enet_packet);
        }
    }
    enet_host_flush(server);
}