本文整理汇总了C++中enet_host_create函数的典型用法代码示例。如果您正苦于以下问题:C++ enet_host_create函数的具体用法?C++ enet_host_create怎么用?C++ enet_host_create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了enet_host_create函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: m_server
ServerBase::ServerBase( int port, int maxClients, int numChannels, int incomingBytes /*= 0*/, int outgoingBytes /*= 0*/)
: m_server(NULL), m_listener(NULL)
{
m_keyMsg = "nZGlA5C7pBWTpVCh8xC7Ad";
m_address.port = port;
if(port != 0)
{
m_address.host = ENET_HOST_ANY;
m_server = enet_host_create(
&m_address,maxClients,numChannels,incomingBytes,outgoingBytes);
if(m_server == NULL)
{
throw Exception("Server Host failed to be created!");
}
}
else
{
m_server = enet_host_create(
NULL,1,numChannels,incomingBytes,outgoingBytes);
if(m_server == NULL)
{
throw Exception("Server Client failed to be created!");
}
}
}
示例2: enet_address_set_host
bool ConnectionHandler::startListen(enet_uint16 port,
const std::string &listenHost)
{
// Bind the server to the default localhost.
address.host = ENET_HOST_ANY;
address.port = port;
if (!listenHost.empty())
enet_address_set_host(&address, listenHost.c_str());
LOG_INFO("Listening on port " << port << "...");
#if defined(ENET_VERSION) && ENET_VERSION >= ENET_CUTOFF
host = enet_host_create(
&address /* the address to bind the server host to */,
Configuration::getValue("net_maxClients", 1000) /* allowed connections */,
0 /* unlimited channel count */,
0 /* assume any amount of incoming bandwidth */,
0 /* assume any amount of outgoing bandwidth */);
#else
host = enet_host_create(
&address /* the address to bind the server host to */,
Configuration::getValue("net_maxClients", 1000) /* allowed connections */,
0 /* assume any amount of incoming bandwidth */,
0 /* assume any amount of outgoing bandwidth */);
#endif
return host != 0;
}
示例3: 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;
}
}
}
示例4: ERR_FAIL_COND_V
Error GDNetHost::bind(Ref<GDNetAddress> addr) {
ERR_FAIL_COND_V(_host != NULL, FAILED);
if (addr.is_null()) {
_host = enet_host_create(NULL, _max_peers, _max_channels, _max_bandwidth_in, _max_bandwidth_out);
} else {
CharString host_addr = addr->get_host().ascii();
ENetAddress enet_addr;
enet_addr.port = addr->get_port();
if (host_addr.length() == 0) {
enet_addr.host = ENET_HOST_ANY;
} else {
if (enet_address_set_host(&enet_addr, host_addr.get_data()) != 0) {
ERR_EXPLAIN("Unable to resolve host");
return FAILED;
}
}
_host = enet_host_create(&enet_addr, _max_peers, _max_channels, _max_bandwidth_in, _max_bandwidth_out);
}
ERR_FAIL_COND_V(_host == NULL, FAILED);
thread_start();
return OK;
}
示例5: shmup_game_network_connect
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);
}
}
}
示例6: shmup_game_init
shmup_game *
shmup_game_init()
{
ENetAddress e;
shmup_game *g;
g = malloc(sizeof(shmup_game));
glfwGetWindowSize(&g->window_width, &g->window_height);
g->render_type = 2;
g->quit = 0;
g->emitter = v2(g->window_width / 2, g->window_height / 2);
g->gravity = v2(0, -250);
g->bpool = bpool_new(8000);
g->bpool->tex[0] = SOIL_load_OGL_texture("./data/flare.tga",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID, 0);
if(g->bpool->tex[0] == 0)
fprintf(stderr, "loading error: '%s'\n", SOIL_last_result());
g->bpool->tex[1] = SOIL_load_OGL_texture("./data/arrow.tga",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID, 0);
if(g->bpool->tex[1] == 0)
fprintf(stderr, "loading error: '%s'\n", SOIL_last_result());
g->bpool->prog = load_shaders("./data/glsl/bullets.vsh",
"./data/glsl/bullets.fsh");
if (enet_initialize () != 0) {
fprintf (stderr, "An error occurred while initializing ENet.\n");
exit(EXIT_FAILURE);
}
if (g->network_type == SERVER) {
e.host = ENET_HOST_ANY;
e.port = 4000;
g->host = enet_host_create(&e, 4, 2, 0, 0);
} else {
g->host = enet_host_create(NULL, 4, 2, 0, 0);
}
g->player[0].pos = v2(g->window_width/2, g->window_height/2);
g->player[0].vel = v2zero;
g->player[0].acc = v2zero;
// fire(g, 1000, 0);
// fire(g, 1000, 1);
return g;
}
示例7: mServer
Host::Host():
mServer(NULL), mConnected(false)
{
// create a new client host for connecting to the server
#ifdef ENET_VERSION
mClient = enet_host_create(NULL, 1, 0, 57600 / 8, 14400 / 8);
#else
mClient = enet_host_create (NULL /* create a client host */,
1 /* only allow 1 outgoing connection */,
57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
#endif
}
示例8: enet_host_create
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);
}
}
示例9: enet_address_set_host
bool Room::Create(const std::string& name, const std::string& server_address, u16 server_port,
const std::string& password, const u32 max_connections,
const std::string& preferred_game, u64 preferred_game_id) {
ENetAddress address;
address.host = ENET_HOST_ANY;
if (!server_address.empty()) {
enet_address_set_host(&address, server_address.c_str());
}
address.port = server_port;
room_impl->server = enet_host_create(&address, max_connections, NumChannels, 0, 0);
if (!room_impl->server) {
return false;
}
room_impl->state = State::Open;
room_impl->room_information.name = name;
room_impl->room_information.member_slots = max_connections;
room_impl->room_information.port = server_port;
room_impl->room_information.preferred_game = preferred_game;
room_impl->room_information.preferred_game_id = preferred_game_id;
room_impl->password = password;
room_impl->CreateUniqueID();
room_impl->StartLoop();
return true;
}
示例10: enet_host_create
void Client::Connect(std::string ip, int port)
{
client = enet_host_create(NULL, 1, 2, 57600, 14400);
if(client == NULL) {
std::cout << "Cannot create client." << std::endl;
}
}
示例11: Save
Server::Server(uint16 port, const char *worldId) {
save = std::unique_ptr<Save>(new Save(worldId));
boost::filesystem::path path(save->getPath());
if (!boost::filesystem::exists(path)) {
boost::filesystem::create_directories(path);
std::random_device rng;
boost::random::uniform_int_distribution<uint64> distr;
uint64 seed = distr(rng);
save->initialize(worldId, seed);
save->store();
}
ServerChunkManager *cm = new ServerChunkManager(save->getWorldGenerator(), save->getChunkArchive());
chunkManager = std::unique_ptr<ServerChunkManager>(cm);
world = std::unique_ptr<World>(new World(chunkManager.get()));
LOG_INFO(logger) << "Creating server";
gameServer = std::unique_ptr<GameServer>(new GameServer(this));
chunkServer = std::unique_ptr<ChunkServer>(new ChunkServer(this));
if (enet_initialize() != 0)
LOG_FATAL(logger) << "An error occurred while initializing ENet.";
ENetAddress address;
address.host = ENET_HOST_ANY;
address.port = port;
host = enet_host_create(&address, MAX_CLIENTS, NUM_CHANNELS, 0, 0);
if (!host)
LOG_FATAL(logger) << "An error occurred while trying to create an ENet server host.";
}
示例12: EnsureTraversalClient
bool EnsureTraversalClient(const std::string& server, u16 server_port, u16 listen_port)
{
if (!g_MainNetHost || !g_TraversalClient || server != g_OldServer ||
server_port != g_OldServerPort || listen_port != g_OldListenPort)
{
g_OldServer = server;
g_OldServerPort = server_port;
g_OldListenPort = listen_port;
ENetAddress addr = {ENET_HOST_ANY, listen_port};
ENetHost* host = enet_host_create(&addr, // address
50, // peerCount
1, // channelLimit
0, // incomingBandwidth
0); // outgoingBandwidth
if (!host)
{
g_MainNetHost.reset();
return false;
}
g_MainNetHost.reset(host);
g_TraversalClient.reset(new TraversalClient(g_MainNetHost.get(), server, server_port));
}
return true;
}
示例13: 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;
}
示例14: 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;
}
示例15: 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;
}