Esempio n. 1
3
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;
}
Esempio n. 2
0
	bool CClienteENet::init(unsigned int maxConnections, unsigned int maxinbw, unsigned int maxoutbw)
	{
		if(estado != NO_INIT)
			return false;

		if (enet_initialize () != 0)
		{
			printf ("An error occurred while initializing ENet.\n");
			return false;

		}

		client = enet_host_create (NULL /* create a client host */,
                maxConnections, /* only allow 1 outgoing connection */
                maxinbw ,
                maxoutbw );

		if (client == NULL)
		{
			printf ("An error occurred while trying to create an ENet client host.\n");
			return false;
		}
		if(DEBUG_CLIENT)
			fprintf(stdout, "Client initialized");
	
		estado = INIT_NOT_CONNECTED;

		return true;
	}
Esempio n. 3
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. 4
0
int main(int argc, char *argv[] )
{
	FeriteScript *script = NULL;
	char *error_message = NULL;
	
	SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
	SDL_EnableUNICODE(1);
	
	enet_initialize();
	
	Mix_Init(0);
	
	int audio_rate = 22050;
	Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
	int audio_channels = 2;
	int audio_buffers = 4096;
	Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers);
	
	srand(time(NULL));
	
	if( ferite_init(0, NULL) )
	{
		ferite_set_script_argv(argc, argv);
		script = ferite_script_compile("Game.fe");
		if( ferite_has_compile_error(script) )
		{
			error_message = ferite_get_error_log(script);
			fprintf(stderr, "[ferite: compile]\n%s", error_message);
		}
		else
		{
			ferite_array_init(script);
			ferite_string_init(script);
			ferite_number_init(script);
			game_engine_init(script);
			
			ferite_script_execute(script);
			if( ferite_has_runtime_error(script) )
			{
				error_message = ferite_get_error_log(script);
				fprintf(stderr, "[ferite: execution]\n%s", error_message);
			}
		}
		if( error_message )
		{
			ffree(error_message);
		}
		ferite_script_delete(script);
		ferite_deinit();
	}
	
	enet_deinitialize();
	
	Mix_CloseAudio();
	Mix_Quit();
	
	SDL_Quit();
	
	return 0;
}
Esempio n. 5
0
    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;
            }
        }
    }
Esempio n. 6
0
void network_init()
{
  ENetAddress address;
  int status, port, max_clients;
  gchar *server;

  network = g_malloc(sizeof(*network));

  server = (gchar *) config_get_string("server", default_server);
  port = config_get_int("port", default_port);
  max_clients = config_get_int("max_clients", default_max_clients);

  status = enet_initialize();
  g_return_if_fail(status == 0);

  enet_address_set_host(&address, server);
  address.port = port;

  network->server = enet_host_create(&address, max_clients, 
#ifdef HAS_RECENT_ENET
                                     2, 
#endif
                                     0, 0);
  g_return_if_fail(network->server != NULL);
  g_debug("Listening on %s:%d", server, port);

  network->clients = NULL;
  g_free(server);
}
Esempio n. 7
0
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.";
}
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");
	}
}
Esempio n. 9
0
int main (int argc, char ** argv)
{
  if (enet_initialize () != 0)
  {
    fprintf (stderr, "An error occurred while initializing ENet.\n");
    return EXIT_FAILURE;
  }
  atexit (enet_deinitialize);

  ENetAddress address;
  address.host = ENET_HOST_ANY;
  address.port = SOCKK;
  printf("create!\n");
  host = enet_host_create (& address /* the address to bind the server host to */,
                             32 /* allow up to 32 clients and/or outgoing connections */,
                             2 /* allow up to 2 channels to be used, 0 and 1 */,
                             0 /* assume any amount of incoming bandwidth */,
                             0 /* assume any amount of outgoing bandwidth */);
  if (host == NULL)
  {
    fprintf (stderr,
    "An error occurred while trying to create an ENet server host.\n");
    exit (EXIT_FAILURE);
  }

#if EMSCRIPTEN
  emscripten_set_main_loop(main_loop, 3, 1);
#else
  while (1) main_loop();
#endif

  return 1;
}
Esempio n. 10
0
/** Initialises the internal data structures and starts the protocol manager
 *  and the debug console.
 */
void STKHost::init()
{
    m_shutdown         = false;
    m_network          = NULL;
    m_lan_network      = NULL;
    m_listening_thread = NULL;
    m_game_setup       = NULL;
    m_is_registered    = false;
    m_error_message    = "";

    pthread_mutex_init(&m_exit_mutex, NULL);

    // Start with initialising ENet
    // ============================
    if (enet_initialize() != 0)
    {
        Log::error("NetworkConsole", "Could not initialize enet.");
        return;
    }

    Log::info("NetworkConsole", "Host initialized.");
    Network::openLog();  // Open packet log file
    ProtocolManager::getInstance<ProtocolManager>();

    // Optional: start the network console
    m_network_console = new NetworkConsole();
    m_network_console->run();

}   // STKHost
Esempio n. 11
0
NetworkManager::NetworkManager() {
	enet_initialize();
	peer = NULL;
	client = NULL;
	isConnectedflag = false;
	endThread = false;
}
Esempio n. 12
0
bool Network::Init(NETWORK_TYPE networkType)
{
    AutoLock<Mutex> lock(m_mutex);

    if(networkType == NONE)
        return false;

    if(enet_initialize() != 0)
    {
        std::cout << "Error initializing network" << std::endl;
        return false;
    }

    if(networkType == CLIENT)
    {
        m_host = enet_host_create (NULL, 1, NETWORK_CHANNEL_COUNT, 0, 0);

        if(!m_host)
        {
            std::cout << "Error initializing connection" << std::endl;
            return false;
        }
    }

    m_type = networkType;
    return true;
}
Esempio n. 13
0
void ServerSystem::Init()
{
    if ( enet_initialize () != 0 )
    {
        L1 ( "An error occurred while initializing ENet.\n" );
    }
    atexit ( enet_deinitialize );
    mOnFrameCounterEvent = EventServer<engine::FrameCounterEvent>::Get().Subscribe( boost::bind( &ServerSystem::OnFrameCounterEvent, this, _1 ) );
    mOnClientIdChanged = EventServer<ClientIdChangedEvent>::Get().Subscribe( boost::bind( &ServerSystem::OnClientIdChanged, this, _1 ) );

    /* Bind the server to the default localhost.     */
    /* A specific host address can be specified by   */
    /* enet_address_set_host (& address, "x.x.x.x"); */
    mAddress.host = ENET_HOST_ANY;
    /* Bind the server to port 1234. */
    mAddress.port = 1234;
    mServer = enet_host_create ( & mAddress /* the address to bind the server host to */,
                                 32      /* allow up to 32 clients and/or outgoing connections */,
                                 2      /* allow up to 2 channels to be used, 0 and 1 */,
                                 0      /* assume any amount of incoming bandwidth */,
                                 0      /* assume any amount of outgoing bandwidth */ );
    if ( mServer == NULL )
    {
        L1 ( "An error occurred while trying to create an ENet server host.\n" );
    }
}
Esempio n. 14
0
    bool Init() {
        if(enet_initialize () < 0)
            return false;

        CubeJ::Init();
        return true;
    }
Esempio n. 15
0
void jsapi_init(int filter){
  if(filter){
	ENetCallbacks callbacks = { NULL, NULL, NULL, packet_filter };
        enet_initialize_with_callbacks(ENET_VERSION, &callbacks);
 	return;
  }
  enet_initialize();
}
Esempio n. 16
0
///////////////////////////////////////////////////////////////////////////////
/// \brief Network initialization
///
/// It initializes network modules an prepares everything to start creating
/// servers or connecting to peers
///////////////////////////////////////////////////////////////////////////////
void
CNetwork::initialize() {
	// INITIALIZE ENET
	if (enet_initialize() != 0)
		throw "ERROR: There was an error initializing enet and network could"
		" not be started\n";
	m_networkInitialized = true;
}
Esempio n. 17
0
int main (int argc, char ** argv)
{
  if (enet_initialize () != 0)
  {
    fprintf (stderr, "An error occurred while initializing ENet.\n");
    return EXIT_FAILURE;
  }
  atexit (enet_deinitialize);

  printf("creating host\n");

  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 */,
                              57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
                              14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
  if (host == NULL)
  {
    fprintf (stderr,
              "An error occurred while trying to create an ENet client host.\n");
    exit (EXIT_FAILURE);
  }

  ENetAddress address;
  enet_address_set_host (& address, "localhost");
  address.port = SOCKK;

  printf("connecting to server...\n");

  ENetPeer *peer = enet_host_connect (host, & address, 2, 0);

  if (peer == NULL)
  {
    fprintf (stderr,
    "No available peers for initiating an ENet connection.\n");
    exit (EXIT_FAILURE);
  }

#ifdef __EMSCRIPTEN__
#if USE_IFRAME
  emscripten_run_script("console.log('adding iframe');"
                        "var iframe = document.createElement('iframe');"
                        "iframe.src = 'server.html';"
                        "iframe.width = '100%';"
                        "iframe.height = '33%';"
                        "document.body.appendChild(iframe);"
                        "console.log('added.');");
#endif
#endif

#ifdef __EMSCRIPTEN__
  emscripten_set_main_loop(main_loop, 3, 1);
#else
  while (1) main_loop();
#endif

  return 1;
}
Esempio n. 18
0
/**
 * Initialize the library
 * return true on success
 */
bool lanarts_net_init(bool throw_on_error) {
	if (enet_initialize() != 0) {
		if (throw_on_error) {
			__lnet_throw_connection_error("Couldn't initialize SDL_net: %s\n");
		}
		return false;
	}
	return true;
}
Esempio n. 19
0
int main(int argc, char **argv)
{
	ENetAddress address;
	ENetHost *server;
	ENetEvent event;
	int eventStatus;

	// a. Initialize enet
	if (enet_initialize() != 0) {
		fprintf(stderr, "An error occured while initializing ENet.\n");
		return EXIT_FAILURE;
	}

	atexit(enet_deinitialize);

	// b. Create a host using enet_host_create
	address.host = ENET_HOST_ANY;
	address.port = 1234;

	server = enet_host_create(&address, 32, 2, 0, 0);

	if (server == NULL) {
		fprintf(stderr, "An error occured while trying to create an ENet server host\n");
		exit(EXIT_FAILURE);
	}

	// c. Connect and user service
	eventStatus = 1;

	while (1) {
		eventStatus = enet_host_service(server, &event, 50000);

		// If we had some event that interested us
		if (eventStatus > 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);
				printf("(Server) We got a new connection from %x\n", event.peer->address.host);
				break;

			case ENET_EVENT_TYPE_RECEIVE:
				printf("(Server) Message from client : %s\n", event.packet->data);
				// Lets broadcast this message to all
				enet_host_broadcast(server, 0, event.packet);
				break;

			case ENET_EVENT_TYPE_DISCONNECT:
				printf("%s disconnected.\n", event.peer->data);
					// Reset client's information
					event.peer->data = NULL;
				break;

			}
		}
	}

}
Esempio n. 20
0
/**
	Initialize the Enet library.
**/
static value udpr_init() {
    if (enet_initialize () != 0)
    {
        fprintf (stderr, "An error occurred while initializing ENet.\n");
        exit(EXIT_FAILURE);
    }
    atexit (enet_deinitialize);
    return val_true;
}
Esempio n. 21
0
int joynet_init(void)
{
	if(enet_initialize() != 0)
	{
		return 0;
	}
	joynet_detect_endianess();
	return 1;
}
Esempio n. 22
0
Network::Network(char *ip) : QObject()
{
    m_ip = ip;
    if(enet_initialize() != 0)
        exit(EXIT_FAILURE);
    args = new struct thread_args;
    args->ip = ip;
    args->instance = this;
}
Esempio n. 23
0
		CNetManager::CNetManager(INetCallback* handler, u32 port, const SNetParams& params) :
			pHandler(handler), netIterations(10000), netParams(params), verbose(0),
			globPacketRelay(false), connectionStatus(EICS_PENDING)
		{
			if (enet_initialize() != 0)
				std::cout << "irrNetLite: An error occurred while initializing ENet.\n";

			connectionStatus = setUpServer(port) ? EICS_ESTABLISHED : EICS_FAILED;
		}
Esempio n. 24
0
void cc_startup_network()
{
	if (enet_initialize () != 0)
  {
      fprintf (stdout, "An error occurred while initializing ENet.\n");
      return EXIT_FAILURE;
  }
  atexit(cc_shutdown_network);
}
Esempio n. 25
0
static int on_load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) {
  if (enet_initialize() != 0) {
    return -1;
  }

  ex_enet_nif_resource_host = enif_open_resource_type(env, "ex_enet_nif", "ex_enet_nif_resource_host",
    NULL, ERL_NIF_RT_CREATE | ERL_NIF_RT_TAKEOVER, 0);

  return 0;
}
Esempio n. 26
0
bool Init() {
    if (enet_initialize() != 0) {
        NGLOG_ERROR(Network, "Error initalizing ENet");
        return false;
    }
    g_room = std::make_shared<Room>();
    g_room_member = std::make_shared<RoomMember>();
    NGLOG_DEBUG(Network, "initialized OK");
    return true;
}
Esempio n. 27
0
NetworkManager::NetworkManager(Game *game) : Manager(game)
{
    mNextID = 1;
    mNetworkID = -1; // Set to -1 for no connection

    mBandwidth = 10000;
    mBandwidthLeft = 10000;

    enet_initialize();
}
Esempio n. 28
0
int networkEngine::initialize()
{
    if (enet_initialize () != 0)
    {
        logMsg("An error occurred while initializing ENet.");
        return EXIT_FAILURE;
    }
    atexit (enet_deinitialize);
    return (0);
}
Esempio n. 29
0
    bool initialize() {
      if (enet_initialize () != 0) {
        fprintf (stderr, "An error occurred while initializing ENet.\n");
        return false;
      }

      Reckoner::Network::MessageMap::initialize();

      return true;
    }
Esempio n. 30
0
void register_enet_types() {

	if (enet_initialize() != 0) {
		ERR_PRINT("ENet initialization failure");
	} else {
		enet_ok = true;
	}

	ClassDB::register_class<NetworkedMultiplayerENet>();
}