当前位置: 首页>>代码示例>>C++>>正文


C++ enet_address_set_host函数代码示例

本文整理汇总了C++中enet_address_set_host函数的典型用法代码示例。如果您正苦于以下问题:C++ enet_address_set_host函数的具体用法?C++ enet_address_set_host怎么用?C++ enet_address_set_host使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了enet_address_set_host函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: CHECK

Peer* ClientHost::Connect(
  std::string server_ip,
  uint16_t port,
  size_t channel_count
) {
  CHECK(_state == STATE_INITIALIZED);

  ENetAddress address;
  if (enet_address_set_host(&address, server_ip.c_str()) != 0) {
    // THROW_ERROR("Unable to set enet host address!");
    return NULL;
  }
  address.port = port;  // XXX: type cast.

  ENetPeer* enet_peer = enet_host_connect(_host, &address, channel_count, 0);
  if (enet_peer == NULL) {
    // THROW_ERROR("Enet host is unable to connect!");
    return NULL;
  }

  Peer* peer = Host::_GetPeer(enet_peer);
  CHECK(peer != NULL);

  return peer;
}
开发者ID:xairy,项目名称:enet-plus,代码行数:25,代码来源:client_host.cpp

示例2: initEnet

static int initEnet(const char *host_ip, const int host_port, ServerData *data)
{
   ENetAddress address;
   assert(data);

   if (enet_initialize() != 0) {
      fprintf (stderr, "An error occurred while initializing ENet.\n");
      return RETURN_FAIL;
   }

   address.host = ENET_HOST_ANY;
   address.port = host_port;

   if (host_ip)
      enet_address_set_host(&address, host_ip);

   data->server = enet_host_create(&address,
         32    /* max clients */,
         2     /* max channels */,
         0     /* download bandwidth */,
         0     /* upload bandwidth */);

   if (!data->server) {
      fprintf (stderr,
            "An error occurred while trying to create an ENet server host.\n");
      return RETURN_FAIL;
   }

   /* enable compression */
   data->server->checksum = enet_crc32;
   enet_host_compress_with_range_coder(data->server);

   return RETURN_OK;
}
开发者ID:Cloudef,项目名称:srv.birth,代码行数:34,代码来源:main.c

示例3: printf

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);
}
开发者ID:noko3,项目名称:cc-client,代码行数:27,代码来源:client.cpp

示例4: ENSURE

bool CNetClientSession::Connect(u16 port, const CStr& server)
{
	ENSURE(!m_Host);
	ENSURE(!m_Server);

	// Create ENet host
	ENetHost* host = enet_host_create(NULL, 1, CHANNEL_COUNT, 0, 0);
	if (!host)
		return false;

	// Bind to specified host
	ENetAddress addr;
	addr.port = port;
	if (enet_address_set_host(&addr, server.c_str()) < 0)
		return false;

	// Initiate connection to server
	ENetPeer* peer = enet_host_connect(host, &addr, CHANNEL_COUNT, 0);
	if (!peer)
		return false;

	m_Host = host;
	m_Server = peer;

	m_Stats = new CNetStatsTable(m_Server);
	if (CProfileViewer::IsInitialised())
		g_ProfileViewer.AddRootTable(m_Stats);

	return true;
}
开发者ID:Gavinator98,项目名称:0ad,代码行数:30,代码来源:NetSession.cpp

示例5: renet_connection_initialize

VALUE renet_connection_initialize(VALUE self, VALUE host, VALUE port, VALUE channels, VALUE download, VALUE upload)
{
  rb_funcall(mENet, rb_intern("initialize"), 0);
  Connection* connection;

  /* Now that we're releasing the GIL while waiting for enet_host_service 
     we need a lock to prevent potential segfaults if another thread tries
     to do an operation on same connection  */
  VALUE lock = rb_mutex_new();
  rb_iv_set(self, "@lock", lock); 
  rb_mutex_lock(lock);

  Data_Get_Struct(self, Connection, connection);
  Check_Type(host, T_STRING);
  if (enet_address_set_host(connection->address, StringValuePtr(host)) != 0)
  {
    rb_raise(rb_eStandardError, "Cannot set address");
  }
  connection->address->port = NUM2UINT(port);
  connection->channels = NUM2UINT(channels);
  connection->online = 0;
  connection->host = enet_host_create(NULL, 1, connection->channels, NUM2UINT(download), NUM2UINT(upload));
  if (connection->host == NULL)
  {
    rb_raise(rb_eStandardError, "Cannot create host");
  }
  rb_iv_set(self, "@total_sent_data", INT2FIX(0)); 
  rb_iv_set(self, "@total_received_data", INT2FIX(0));
  rb_iv_set(self, "@total_sent_packets", INT2FIX(0));
  rb_iv_set(self, "@total_received_packets", INT2FIX(0));

  rb_mutex_unlock(lock);
  return self;
}
开发者ID:jvranish,项目名称:rENet,代码行数:34,代码来源:renet_connection.c

示例6: enet_address_set_host

void NetworkController::initEnet() {
	if( enet_initialize() != 0){
		root->fatalError("NetController", "Cannot initialise ENet");
	};
	
	root->sout << "Attempting to create host..." << endl;
	
	if( root->confMgr.varExists("host_address") ){
		enet_address_set_host( &enetAddress, root->confMgr.getVarS("host_address").c_str() );
		
		root->sout << "Binding to configured address " << root->confMgr.getVarS("host_address") <<endl;
	}else{
		enetAddress.host = ENET_HOST_ANY;
		root->sout << "Binding to any available address" << endl;
	}
	
	if( root->confMgr.varExists("host_port") ){
		enetAddress.port = root->confMgr.getVarI("host_port");
	}else{
		enetAddress.port = HOST_PORT_DEFAULT;
	}
	
	root->sout << "Binding to port " << enetAddress.port << endl;
	
	enetHost = enet_host_create(&enetAddress, (root->confMgr.varExists("max_clients")) ? (root->confMgr.getVarI("max_clients")) : MAX_CLIENTS_DEFAULT, 2, 0, 0);
	
	if(enetHost == NULL) {
		root->fatalError("NetController", "Cannot create ENet host");
	}
}
开发者ID:cindustries,项目名称:engine-cuboid,代码行数:30,代码来源:NetworkController.cpp

示例7: room_data_change

NetClient::NetClient(const std::string& ip)
:
    me                (0),
    server            (0),
    exit              (false),
    is_game           (false),
    game_start        (false),
    game_end          (false),
    room_data_change  (false),
    player_data_change(false),
    level_change      (false),
    updates_change    (0),
    ourself           (players.begin()) // will crash if dereferenced if empty
{
    // 0 = Client, 1 = one connection at most (to server), 0, 0 = unlim. bandw.
    //
    // #############################################################
    // # NOTE: If you get a compiler error for the following line, #
    // #  check your enet version. Lix assumes you have enet 1.3.  #
    // #############################################################
    me = enet_host_create(0, 1, LEMNET_CHANNEL_MAX, 0, 0);

    ENetAddress address;
    enet_address_set_host(&address, ip.c_str());
    address.port = gloB->server_port;

    server = enet_host_connect(me, &address, LEMNET_CHANNEL_MAX, 0);
}
开发者ID:AmandaBayless,项目名称:Lix,代码行数:28,代码来源:client.cpp

示例8: Disconnect

bool NNetwork::Connect(std::string i_HostName, unsigned int Port)
{
    if (Host)
    {
        Disconnect();
    }
    HostName = i_HostName;
    Address.port = Port;
    enet_address_set_host(&Address, HostName.c_str());

    Host = enet_host_connect(Client,&Address,2,0);
    if (!Host)
    {
        std::stringstream Message;
        Message << "Failed to connect to host " << HostName << " on port " << Port << "!";
        GetGame()->GetLog()->Send("NETWORK",0,Message.str());
        return 1;
    }
    ENetEvent Event;
    if (enet_host_service(Client, &Event, 2500) > 0 && Event.type == ENET_EVENT_TYPE_CONNECT)
    {
        std::stringstream Message;
        Message << "Successfully connected to " << HostName << " on port " << Port << ".";
        GetGame()->GetLog()->Send("NETWORK",2,Message.str());
        return 0;
    } else {
        std::stringstream Message;
        Message << "Failed to connect to host " << HostName << " on port " << Port << "!";
        GetGame()->GetLog()->Send("NETWORK",0,Message.str());
        Host = NULL;
        return 1;
    }
    return 1;
}
开发者ID:ZenX2,项目名称:Astrostruct,代码行数:34,代码来源:NNetwork.cpp

示例9: enet_address_set_host

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 ;
	}
}
开发者ID:quinsmpang,项目名称:xsilium-engine,代码行数:25,代码来源:NetworkManager.cpp

示例10: resolverloop

int resolverloop(void * data)
{
    resolverthread *rt = (resolverthread *)data;
    for(;;)
    {
        SDL_SemWait(resolversem);
        SDL_LockMutex(resolvermutex);
        if(resolverqueries.empty())
        {
            SDL_UnlockMutex(resolvermutex);
            continue;
        }
        rt->query = resolverqueries.pop();
        rt->starttime = lastmillis;
        SDL_UnlockMutex(resolvermutex);
        ENetAddress address = { ENET_HOST_ANY, CUBE_SERVINFO_PORT };
        enet_address_set_host(&address, rt->query);
        SDL_LockMutex(resolvermutex);
        resolverresult &rr = resolverresults.add();
        rr.query = rt->query;
        rr.address = address;
        rt->query = NULL;
        rt->starttime = 0;
        SDL_UnlockMutex(resolvermutex);
    };
    return 0;
};
开发者ID:Knapsack44,项目名称:w,代码行数:27,代码来源:serverbrowser.cpp

示例11: enet_address_set_host

void networkEngine::serverSetup()
{
    //gameEngine *gameE = gameEngine::Instance();
    boost::shared_ptr<gameEngine> gameE = gameEngine::Instance();

    /* Bind the server to the default localhost.     */
    /* A specific host address can be specified by   */
    /* enet_address_set_host (& address, "x.x.x.x"); */

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

    listenAddress.host = enet_address_set_host (& listenAddress, ipAddress.c_str());
//    listenAddress.host = enet_address_set_host (& listenAddress, ENET_HOST_ANY);

    /* Bind the server to port 1234. */
    listenAddress.port = 1234;

    server = enet_host_create (& listenAddress /* the address to bind the server host to */,
                               32      /* allow up to 32 clients and/or outgoing connections */,
                               2	/* allows up to 2 channels, 0, 1*/,
                               0      /* assume any amount of incoming bandwidth */,
                               0      /* assume any amount of outgoing bandwidth */);
    if (server == NULL)
    {
        logMsg("An error occurred while trying to create an ENet server host.");
        exit (EXIT_FAILURE);
    }
    gameE->setServerRunning(true);
}
开发者ID:libolt,项目名称:ubc-merge,代码行数:32,代码来源:networkengine.cpp

示例12: menu_connect_start

void menu_connect_start(component *c, void *userdata) {
    scene *s = userdata;
    connect_menu_data *local = menu_get_userdata(c->parent);
    ENetAddress address;
    const char *addr = textinput_value(local->addr_input);
    s->gs->role = ROLE_CLIENT;

    // Free old saved address, and set new
    free(settings_get()->net.net_connect_ip);
    settings_get()->net.net_connect_ip = strdup(addr);

    // Set up enet host
    local->host = enet_host_create(NULL, 1, 2, 0, 0);
    if(local->host == NULL) {
        DEBUG("Failed to initialize ENet client");
        return;
    }

    // Disable connect button and address input field
    component_disable(local->connect_button, 1);
    component_disable(local->addr_input, 1);
    menu_select(c->parent, local->cancel_button);

    // Set address
    enet_address_set_host(&address, addr);
    address.port = settings_get()->net.net_connect_port;

    ENetPeer *peer = enet_host_connect(local->host, &address, 2, 0);
    if(peer == NULL) {
        DEBUG("Unable to connect to %s", addr);
        enet_host_destroy(local->host);
        local->host = NULL;
    }
    time(&local->connect_start);
}
开发者ID:exander77,项目名称:openomf,代码行数:35,代码来源:menu_connect.c

示例13: mPlayerFactory

    NetManager::NetManager(bool isServer, const FAWorld::PlayerFactory& playerFactory) : mPlayerFactory(playerFactory)
    {
        assert(singletonInstance == NULL);
        singletonInstance = this;

        enet_initialize();

        mAddress.port = 6666;

        mIsServer = isServer;

        if(isServer)
        {
            mAddress.host = ENET_HOST_ANY;
            mHost = enet_host_create(&mAddress, 32, 2, 0, 0);
        }
        else
        {
            enet_address_set_host(&mAddress, "127.0.0.1");
            mHost = enet_host_create(NULL, 32, 2, 0, 0);

            mServerPeer = enet_host_connect(mHost, &mAddress, 2, 0);

            ENetEvent event;

            if(enet_host_service(mHost, &event, 5000))
            {
                std::cout << "connected" << std::endl;
            }
            else
            {
                std::cout << "connection failed" << std::endl;
            }
        }
    }
开发者ID:FlyingTarrasque,项目名称:freeablo,代码行数:35,代码来源:netmanager.cpp

示例14: enet_host_create

void *Network::readThread(void *context)
{
    struct thread_args *args = (struct thread_args *)context;
    char *ip = args->ip;
    Network *instance = args->instance;
    ENetEvent event;
    ENetAddress address;

    client = enet_host_create(NULL, 1, 1, 0, 0);
    if (client == NULL)
        return (void*)1;

    enet_address_set_host (&address, ip);
    address.port = 1400;
    server = enet_host_connect (client, &address, 1, 0);
    if (server == NULL)
        return (void*)2;

    if (!(enet_host_service (client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT))
        return (void*)3;
    emit instance->connected();

    while(true) {
        pthread_mutex_lock(&net_mutex);
        enet_host_service (client, &event, 0);
        pthread_mutex_unlock(&net_mutex);
        switch (event.type) {
        case ENET_EVENT_TYPE_CONNECT:
            server = event.peer;
            emit instance->connected();
            break;
        case ENET_EVENT_TYPE_RECEIVE:
            switch(event.packet->data[0]) {
            case SET_MEASURED_VALUES:
                float values[3];
                memcpy(values, event.packet->data + 1, sizeof(float) * 3);
                emit instance->setMeasuredValues(values[0], values[1], values[2]);
                break;
            case SET_CPU_USAGE:
                emit instance->setCPUUsage(event.packet->data[1]);
                break;
            case SET_MEMORY_USAGE:
                emit instance->setMemoryUsage(event.packet->data[1]);
                break;
            }
            enet_packet_destroy(event.packet);
            break;
        case ENET_EVENT_TYPE_DISCONNECT:
            server = NULL;
            emit instance->disconnect();
            break;
        default:
            break;
        }
        usleep(100);
    }
    return (void*)0;
}
开发者ID:FlorentRevest,项目名称:Raspcopter,代码行数:58,代码来源:Network.cpp

示例15: enet_host_create

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;
    }
}
开发者ID:libolt,项目名称:ubc-merge,代码行数:58,代码来源:networkengine.cpp


注:本文中的enet_address_set_host函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。