/**
 * Checks whether this IP address is contained by the given netmask.
 * @param netmask the netmask in CIDR notation to test against.
 * @note netmask without /n assumes all bits need to match.
 * @return true if this IP is within the netmask.
 */
bool NetworkAddress::IsInNetmask(char *netmask)
{
	/* Resolve it if we didn't do it already */
	if (!this->IsResolved()) this->GetAddress();

	int cidr = this->address.ss_family == AF_INET ? 32 : 128;

	NetworkAddress mask_address;

	/* Check for CIDR separator */
	char *chr_cidr = strchr(netmask, '/');
	if (chr_cidr != NULL) {
		int tmp_cidr = atoi(chr_cidr + 1);

		/* Invalid CIDR, treat as single host */
		if (tmp_cidr > 0 || tmp_cidr < cidr) cidr = tmp_cidr;

		/* Remove and then replace the / so that NetworkAddress works on the IP portion */
		*chr_cidr = '\0';
		mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
		*chr_cidr = '/';
	} else {
		mask_address = NetworkAddress(netmask, 0, this->address.ss_family);
	}

	if (mask_address.GetAddressLength() == 0) return false;

	uint32 *ip;
	uint32 *mask;
	switch (this->address.ss_family) {
		case AF_INET:
			ip = (uint32*)&((struct sockaddr_in*)&this->address)->sin_addr.s_addr;
			mask = (uint32*)&((struct sockaddr_in*)&mask_address.address)->sin_addr.s_addr;
			break;

		case AF_INET6:
			ip = (uint32*)&((struct sockaddr_in6*)&this->address)->sin6_addr;
			mask = (uint32*)&((struct sockaddr_in6*)&mask_address.address)->sin6_addr;
			break;

		default:
			NOT_REACHED();
	}

	while (cidr > 0) {
		uint32 msk = cidr >= 32 ? (uint32)-1 : htonl(-(1 << (32 - cidr)));
		if ((*mask++ & msk) != (*ip++ & msk)) return false;

		cidr -= 32;
	}

	return true;
}
Beispiel #2
0
/**
 * Create an UDP socket but don't listen yet.
 * @param bind the addresses to bind to.
 */
NetworkUDPSocketHandler::NetworkUDPSocketHandler(NetworkAddressList *bind)
{
	if (bind != NULL) {
		for (NetworkAddress *addr = bind->Begin(); addr != bind->End(); addr++) {
			*this->bind.Append() = *addr;
		}
	} else {
		/* As hostname NULL and port 0/NULL don't go well when
		 * resolving it we need to add an address for each of
		 * the address families we support. */
		*this->bind.Append() = NetworkAddress(NULL, 0, AF_INET);
		*this->bind.Append() = NetworkAddress(NULL, 0, AF_INET6);
	}
}
Beispiel #3
0
void
ClientApp::parseArgs(int argc, const char* const* argv)
{
	ArgParser argParser(this);
	bool result = argParser.parseClientArgs(args(), argc, argv);

	if (!result || args().m_shouldExit) {
		m_bye(kExitArgs);
	}
	else {
		// save server address
		if (!args().m_synergyAddress.empty()) {
			try {
				*m_serverAddress = NetworkAddress(args().m_synergyAddress, kDefaultPort);
				m_serverAddress->resolve();
			}
			catch (XSocketAddress& e) {
				// allow an address that we can't look up if we're restartable.
				// we'll try to resolve the address each time we connect to the
				// server.  a bad port will never get better.  patch by Brent
				// Priddy.
				if (!args().m_restartable || e.getError() == XSocketAddress::kBadPort) {
					LOG((CLOG_PRINT "%s: %s" BYE,
						args().m_pname, e.what(), args().m_pname));
					m_bye(kExitFailed);
				}
			}
		}
	}
}
Beispiel #4
0
bool ScenargieRouteMgr::DeleteRoute(
    const ProtoAddress& destinationProtoAddress,
    unsigned int prefixLength,
    const ProtoAddress& gatewayProtoAddress,
    unsigned int protoInterfaceIndex)
{
    bool routeIsDeletedFromProtoRouteTable = protoRouteTable.DeleteRoute(
        destinationProtoAddress,
        prefixLength,
        &gatewayProtoAddress);

    if(routeIsDeletedFromProtoRouteTable){

        NetworkAddress destinationNetworkAddress(
            destinationProtoAddress.SimGetAddress());

        NetworkAddress destinationNetworkAddressSubnetMask =
            NetworkAddress(0xFFFFFFFF << (32 - prefixLength));

        shared_ptr<RoutingTable> routingTablePtr =
            networkLayerPtr->GetRoutingTableInterface();

        routingTablePtr->DeleteRoute(
            destinationNetworkAddress,
            destinationNetworkAddressSubnetMask);

        nrlolsrProtocolPtr->OutputTraceAndStatsForDeleteRoutingTableEntry(
            destinationNetworkAddress,
            destinationNetworkAddressSubnetMask);

    }//if//

    return routeIsDeletedFromProtoRouteTable;
}//DeleteRoute//
Beispiel #5
0
void GameLoop()
{
	ProcessAsyncSaveFinish();

	/* autosave game? */
	if (_do_autosave) {
		_do_autosave = false;
		DoAutosave();
		SetWindowDirty(WC_STATUS_BAR, 0);
	}

	/* switch game mode? */
	if (_switch_mode != SM_NONE) {
		SwitchToMode(_switch_mode);
		_switch_mode = SM_NONE;
	}

	IncreaseSpriteLRU();
	InteractiveRandom();

	extern int _caret_timer;
	_caret_timer += 3;
	CursorTick();

#ifdef ENABLE_NETWORK
	/* Check for UDP stuff */
	if (_network_available) NetworkUDPGameLoop();

	if (_networking && !IsGeneratingWorld()) {
		/* Multiplayer */
		NetworkGameLoop();
	} else {
		if (_network_reconnect > 0 && --_network_reconnect == 0) {
			/* This means that we want to reconnect to the last host
			 * We do this here, because it means that the network is really closed */
			NetworkClientConnectGame(NetworkAddress(_settings_client.network.last_host, _settings_client.network.last_port), COMPANY_SPECTATOR);
		}
		/* Singleplayer */
		StateGameLoop();
	}

	/* Check chat messages roughly once a second. */
	static uint check_message = 0;
	if (++check_message > 1000 / MILLISECONDS_PER_TICK) {
		check_message = 0;
		NetworkChatMessageLoop();
	}
#else
	StateGameLoop();
#endif /* ENABLE_NETWORK */

	if (!_pause_mode && HasBit(_display_opt, DO_FULL_ANIMATION)) DoPaletteAnimations();

	if (!_pause_mode || _game_mode == GM_EDITOR || _settings_game.construction.command_pause_level > CMDPL_NO_CONSTRUCTION) MoveAllTextEffects();

	InputLoop();

	_sound_driver->MainLoop();
	MusicLoop();
}
/**
 * Helper function to resolve a connected socket.
 * @param runp information about the socket to try not
 * @return the opened socket or INVALID_SOCKET
 */
static SOCKET ConnectLoopProc(addrinfo *runp)
{
	const char *type = NetworkAddress::SocketTypeAsString(runp->ai_socktype);
	const char *family = NetworkAddress::AddressFamilyAsString(runp->ai_family);
	const char *address = NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString();

	SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
	if (sock == INVALID_SOCKET) {
		DEBUG(net, 1, "[%s] could not create %s socket: %s", type, family, strerror(errno));
		return INVALID_SOCKET;
	}

	if (!SetNoDelay(sock)) DEBUG(net, 1, "[%s] setting TCP_NODELAY failed", type);

	if (connect(sock, runp->ai_addr, (int)runp->ai_addrlen) != 0) {
		DEBUG(net, 1, "[%s] could not connect %s socket: %s", type, family, strerror(errno));
		closesocket(sock);
		return INVALID_SOCKET;
	}

	/* Connection succeeded */
	if (!SetNonBlocking(sock)) DEBUG(net, 0, "[%s] setting non-blocking mode failed", type);

	DEBUG(net, 1, "[%s] connected to %s", type, address);

	return sock;
}
Beispiel #7
0
IpcServer::IpcServer(IEventQueue* events, SocketMultiplexer* socketMultiplexer, int port) :
	m_socket(events, socketMultiplexer),
	m_address(NetworkAddress(IPC_HOST, port)),
	m_events(events)
{
	init();
}
/**
 * Connect with the content server.
 */
void ClientNetworkContentSocketHandler::Connect()
{
	this->lastActivity = _realtime_tick;

	if (this->sock != INVALID_SOCKET || this->isConnecting) return;
	this->isConnecting = true;
	new NetworkContentConnecter(NetworkAddress(NETWORK_CONTENT_SERVER_HOST, NETWORK_CONTENT_SERVER_PORT, AF_UNSPEC));
}
Beispiel #9
0
IpcClient::IpcClient(IEventQueue* events, SocketMultiplexer* socketMultiplexer) :
	m_serverAddress(NetworkAddress(IPC_HOST, IPC_PORT)),
	m_socket(events, socketMultiplexer, IArchNetwork::kINET),
	m_server(nullptr),
	m_events(events)
{
	init();
}
Beispiel #10
0
IpcClient::IpcClient(IEventQueue* events, SocketMultiplexer* socketMultiplexer, int port) :
	m_serverAddress(NetworkAddress(IPC_HOST, port)),
	m_socket(events, socketMultiplexer),
	m_server(nullptr),
	m_events(events)
{
	init();
}
Beispiel #11
0
IpcServer::IpcServer(IEventQueue* events, SocketMultiplexer* socketMultiplexer) :
	m_mock(false),
	m_events(events),
	m_socketMultiplexer(socketMultiplexer),
	m_socket(nullptr),
	m_address(NetworkAddress(IPC_HOST, IPC_PORT))
{
	init();
}
void LoadShedConfiguration::addWhitelistAddr(folly::StringPiece input) {
  auto addr = input.str();
  size_t separator = addr.find_first_of('/');
  if (separator == string::npos) {
    whitelistAddrs_.insert(SocketAddress(addr, 0));
  } else {
    unsigned prefixLen = folly::to<unsigned>(addr.substr(separator + 1));
    addr.erase(separator);
    whitelistNetworks_.insert(NetworkAddress(SocketAddress(addr, 0), prefixLen));
  }
}
Beispiel #13
0
bool ScenargieRouteMgr::SetRoute(
    const ProtoAddress& destinationProtoAddress,
    unsigned int prefixLength,
    const ProtoAddress& gatewayProtoAddress,
    unsigned int protoInterfaceIndex,
    int metric)
{
    bool routeIsSetToProtoRouteTable = protoRouteTable.SetRoute(
        destinationProtoAddress,
        prefixLength,
        gatewayProtoAddress,
        protoInterfaceIndex,
        metric);

    if (routeIsSetToProtoRouteTable){

        NetworkAddress destinationNetworkAddress(
            destinationProtoAddress.SimGetAddress());

        NetworkAddress destinationNetworkAddressSubnetMask =
            NetworkAddress(0xFFFFFFFF << (32 - prefixLength));

        SIMADDR gatewayRawAddress = gatewayProtoAddress.SimGetAddress();
        NetworkAddress nextHopNetworkAddress(gatewayRawAddress);
        if(gatewayRawAddress == 0){
            nextHopNetworkAddress = destinationNetworkAddress;
        }

        shared_ptr<RoutingTable> routingTablePtr =
            networkLayerPtr->GetRoutingTableInterface();

        routingTablePtr->AddOrUpdateRoute(
            destinationNetworkAddress,
            destinationNetworkAddressSubnetMask,
            nextHopNetworkAddress,
            interfaceIndex);

        nrlolsrProtocolPtr->OutputTraceAndStatsForAddRoutingTableEntry(
            destinationNetworkAddress,
            destinationNetworkAddressSubnetMask,
            nextHopNetworkAddress);

    }//if//

    return routeIsSetToProtoRouteTable;
}//SetRoute//
/**
 * Helper function to resolve a listening.
 * @param runp information about the socket to try not
 * @return the opened socket or INVALID_SOCKET
 */
static SOCKET ListenLoopProc(addrinfo *runp)
{
	const char *type = NetworkAddress::SocketTypeAsString(runp->ai_socktype);
	const char *family = NetworkAddress::AddressFamilyAsString(runp->ai_family);
	const char *address = NetworkAddress(runp->ai_addr, (int)runp->ai_addrlen).GetAddressAsString();

	SOCKET sock = socket(runp->ai_family, runp->ai_socktype, runp->ai_protocol);
	if (sock == INVALID_SOCKET) {
		DEBUG(net, 0, "[%s] could not create %s socket on port %s: %s", type, family, address, strerror(errno));
		return INVALID_SOCKET;
	}

	if (runp->ai_socktype == SOCK_STREAM && !SetNoDelay(sock)) {
		DEBUG(net, 3, "[%s] setting TCP_NODELAY failed for port %s", type, address);
	}

	int on = 1;
	/* The (const char*) cast is needed for windows!! */
	if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on)) == -1) {
		DEBUG(net, 3, "[%s] could not set reusable %s sockets for port %s: %s", type, family, address, strerror(errno));
	}

	if (runp->ai_family == AF_INET6 &&
			setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&on, sizeof(on)) == -1) {
		DEBUG(net, 3, "[%s] could not disable IPv4 over IPv6 on port %s: %s", type, address, strerror(errno));
	}

	if (bind(sock, runp->ai_addr, (int)runp->ai_addrlen) != 0) {
		DEBUG(net, 1, "[%s] could not bind on %s port %s: %s", type, family, address, strerror(errno));
		closesocket(sock);
		return INVALID_SOCKET;
	}

	if (runp->ai_socktype != SOCK_DGRAM && listen(sock, 1) != 0) {
		DEBUG(net, 1, "[%s] could not listen at %s port %s: %s", type, family, address, strerror(errno));
		closesocket(sock);
		return INVALID_SOCKET;
	}

	/* Connection succeeded */
	if (!SetNonBlocking(sock)) DEBUG(net, 0, "[%s] setting non-blocking mode failed for %s port %s", type, family, address);

	DEBUG(net, 1, "[%s] listening on %s port %s", type, family, address);
	return sock;
}
Beispiel #15
0
/** Requeries the (game) servers we have not gotten a reply from */
void NetworkGameListRequery()
{
	NetworkGameListHandleDelayedInsert();

	static uint8 requery_cnt = 0;

	if (++requery_cnt < REQUERY_EVERY_X_GAMELOOPS) return;
	requery_cnt = 0;

	for (NetworkGameList *item = _network_game_list; item != NULL; item = item->next) {
		item->retries++;
		if (item->retries < REFRESH_GAMEINFO_X_REQUERIES && (item->online || item->retries >= MAX_GAME_LIST_REQUERY_COUNT)) continue;

		/* item gets mostly zeroed by NetworkUDPQueryServer */
		uint8 retries = item->retries;
		NetworkUDPQueryServer(NetworkAddress(item->address));
		item->retries = (retries >= REFRESH_GAMEINFO_X_REQUERIES) ? 0 : retries;
	}
}
Beispiel #16
0
void
ServerApp::parseArgs(int argc, const char* const* argv)
{
	ArgParser argParser(this);
	bool result = argParser.parseServerArgs(args(), argc, argv);

	if (!result || args().m_shouldExit) {
		m_bye(kExitArgs);
	}
	else {
		if (!args().m_synergyAddress.empty()) {
			try {
				*m_synergyAddress = NetworkAddress(args().m_synergyAddress, 
					kDefaultPort);
				m_synergyAddress->resolve();
			}
			catch (XSocketAddress& e) {
				LOG((CLOG_PRINT "%s: %s" BYE,
					args().m_pname, e.what(), args().m_pname));
				m_bye(kExitArgs);
			}
		}
	}
}
Beispiel #17
0
int ttd_main(int argc, char *argv[])
{
	int i;
	const char *optformat;
	char *musicdriver = NULL;
	char *sounddriver = NULL;
	char *videodriver = NULL;
	char *blitter = NULL;
	char *graphics_set = NULL;
	char *sounds_set = NULL;
	char *music_set = NULL;
	Dimension resolution = {0, 0};
	Year startyear = INVALID_YEAR;
	uint generation_seed = GENERATE_NEW_SEED;
	bool save_config = true;
#if defined(ENABLE_NETWORK)
	bool dedicated = false;
	bool network   = false;
	char *network_conn = NULL;
	char *debuglog_conn = NULL;
	char *dedicated_host = NULL;
	uint16 dedicated_port = 0;
	char *join_server_password = NULL;
	char *join_company_password = NULL;

	extern bool _dedicated_forks;
	_dedicated_forks = false;
#endif /* ENABLE_NETWORK */

	_game_mode = GM_MENU;
	_switch_mode = SM_MENU;
	_switch_mode_errorstr = INVALID_STRING_ID;
	_config_file = NULL;

	/* The last param of the following function means this:
	 *   a letter means: it accepts that param (e.g.: -h)
	 *   a ':' behind it means: it need a param (e.g.: -m<driver>)
	 *   a '::' behind it means: it can optional have a param (e.g.: -d<debug>) */
	optformat = "m:s:v:b:hD::n::ei::I:S:M:t:d::r:g::G:c:xl:p:P:"
#if !defined(__MORPHOS__) && !defined(__AMIGA__) && !defined(WIN32)
		"f"
#endif
	;

	MyGetOptData mgo(argc - 1, argv + 1, optformat);

	while ((i = MyGetOpt(&mgo)) != -1) {
		switch (i) {
		case 'I': free(graphics_set); graphics_set = strdup(mgo.opt); break;
		case 'S': free(sounds_set); sounds_set = strdup(mgo.opt); break;
		case 'M': free(music_set); music_set = strdup(mgo.opt); break;
		case 'm': free(musicdriver); musicdriver = strdup(mgo.opt); break;
		case 's': free(sounddriver); sounddriver = strdup(mgo.opt); break;
		case 'v': free(videodriver); videodriver = strdup(mgo.opt); break;
		case 'b': free(blitter); blitter = strdup(mgo.opt); break;
#if defined(ENABLE_NETWORK)
		case 'D':
			free(musicdriver);
			free(sounddriver);
			free(videodriver);
			free(blitter);
			musicdriver = strdup("null");
			sounddriver = strdup("null");
			videodriver = strdup("dedicated");
			blitter = strdup("null");
			dedicated = true;
			SetDebugString("net=6");
			if (mgo.opt != NULL) {
				/* Use the existing method for parsing (openttd -n).
				 * However, we do ignore the #company part. */
				const char *temp = NULL;
				const char *port = NULL;
				ParseConnectionString(&temp, &port, mgo.opt);
				if (!StrEmpty(mgo.opt)) dedicated_host = mgo.opt;
				if (port != NULL) dedicated_port = atoi(port);
			}
			break;
		case 'f': _dedicated_forks = true; break;
		case 'n':
			network = true;
			network_conn = mgo.opt; // optional IP parameter, NULL if unset
			break;
		case 'l':
			debuglog_conn = mgo.opt;
			break;
		case 'p':
			join_server_password = mgo.opt;
			break;
		case 'P':
			join_company_password = mgo.opt;
			break;
#endif /* ENABLE_NETWORK */
		case 'r': ParseResolution(&resolution, mgo.opt); break;
		case 't': startyear = atoi(mgo.opt); break;
		case 'd': {
#if defined(WIN32)
				CreateConsole();
#endif
				if (mgo.opt != NULL) SetDebugString(mgo.opt);
				break;
			}
		case 'e': _switch_mode = SM_EDITOR; break;
		case 'i':
			/* there is an argument, it is not empty, and it is exactly 1 char long */
			if (!StrEmpty(mgo.opt) && mgo.opt[1] == '\0') {
				_use_palette = (PaletteType)(mgo.opt[0] - '0');
				if (_use_palette <= MAX_PAL) break;
			}
			usererror("Valid value for '-i' is 0, 1 or 2");
		case 'g':
			if (mgo.opt != NULL) {
				strecpy(_file_to_saveload.name, mgo.opt, lastof(_file_to_saveload.name));
				_switch_mode = SM_LOAD;
				_file_to_saveload.mode = SL_LOAD;

				/* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */
				const char *t = strrchr(_file_to_saveload.name, '.');
				if (t != NULL) {
					FiosType ft = FiosGetSavegameListCallback(SLD_LOAD_GAME, _file_to_saveload.name, t, NULL, NULL);
					if (ft != FIOS_TYPE_INVALID) SetFiosType(ft);
				}

				break;
			}

			_switch_mode = SM_NEWGAME;
			/* Give a random map if no seed has been given */
			if (generation_seed == GENERATE_NEW_SEED) {
				generation_seed = InteractiveRandom();
			}
			break;
		case 'G': generation_seed = atoi(mgo.opt); break;
		case 'c': _config_file = strdup(mgo.opt); break;
		case 'x': save_config = false; break;
		case -2:
		case 'h':
			/* The next two functions are needed to list the graphics sets.
			 * We can't do them earlier because then we can't show it on
			 * the debug console as that hasn't been configured yet. */
			DeterminePaths(argv[0]);
			BaseGraphics::FindSets();
			BaseSounds::FindSets();
			BaseMusic::FindSets();
			ShowHelp();
			return 0;
		}
	}

#if defined(WINCE) && defined(_DEBUG)
	/* Switch on debug lvl 4 for WinCE if Debug release, as you can't give params, and you most likely do want this information */
	SetDebugString("4");
#endif

	DeterminePaths(argv[0]);
	BaseGraphics::FindSets();
	BaseSounds::FindSets();
	BaseMusic::FindSets();

#if defined(ENABLE_NETWORK) && defined(UNIX) && !defined(__MORPHOS__)
	/* We must fork here, or we'll end up without some resources we need (like sockets) */
	if (_dedicated_forks) DedicatedFork();
#endif

	TarScanner::DoScan();
	AI::Initialize();
	LoadFromConfig();
	AI::Uninitialize(true);
	CheckConfig();
	LoadFromHighScore();
	LoadHotkeysFromConfig();

	if (resolution.width != 0) { _cur_resolution = resolution; }
	if (startyear != INVALID_YEAR) _settings_newgame.game_creation.starting_year = startyear;
	if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed;

	/*
	 * The width and height must be at least 1 pixel and width times
	 * height times bytes per pixel must still fit within a 32 bits
	 * integer, even for 32 bpp video modes. This way all internal
	 * drawing routines work correctly.
	 */
	_cur_resolution.width  = ClampU(_cur_resolution.width,  1, UINT16_MAX / 2);
	_cur_resolution.height = ClampU(_cur_resolution.height, 1, UINT16_MAX / 2);

#if defined(ENABLE_NETWORK)
	if (dedicated) DEBUG(net, 0, "Starting dedicated version %s", _openttd_revision);
	if (dedicated_host) {
		_network_bind_list.Clear();
		*_network_bind_list.Append() = strdup(dedicated_host);
	}
	if (dedicated_port) _settings_client.network.server_port = dedicated_port;
	if (_dedicated_forks && !dedicated) _dedicated_forks = false;
#endif /* ENABLE_NETWORK */

	/* enumerate language files */
	InitializeLanguagePacks();

	/* initialize screenshot formats */
	InitializeScreenshotFormats();

	/* Initialize FreeType */
	InitFreeType();

	/* This must be done early, since functions use the SetWindowDirty* calls */
	InitWindowSystem();

	/* Look for the sounds before the graphics. Otherwise none would be set and
	 * the first initialisation of the video happens on the wrong data. Now it
	 * can do the first initialisation right. */
	if (sounds_set == NULL && BaseSounds::ini_set != NULL) sounds_set = strdup(BaseSounds::ini_set);
	if (!BaseSounds::SetSet(sounds_set)) {
		StrEmpty(sounds_set) ?
			usererror("Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of readme.txt.") :
			usererror("Failed to select requested sounds set '%s'", sounds_set);
	}
	free(sounds_set);

	if (graphics_set == NULL && BaseGraphics::ini_set != NULL) graphics_set = strdup(BaseGraphics::ini_set);
	if (!BaseGraphics::SetSet(graphics_set)) {
		StrEmpty(graphics_set) ?
			usererror("Failed to find a graphics set. Please acquire a graphics set for OpenTTD. See section 4.1 of readme.txt.") :
			usererror("Failed to select requested graphics set '%s'", graphics_set);
	}
	free(graphics_set);

	if (music_set == NULL && BaseMusic::ini_set != NULL) music_set = strdup(BaseMusic::ini_set);
	if (!BaseMusic::SetSet(music_set)) {
		StrEmpty(music_set) ?
			usererror("Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of readme.txt.") :
			usererror("Failed to select requested music set '%s'", music_set);
	}
	free(music_set);

	/* Initialize game palette */
	GfxInitPalettes();

	DEBUG(misc, 1, "Loading blitter...");
	if (blitter == NULL && _ini_blitter != NULL) blitter = strdup(_ini_blitter);
	if (BlitterFactoryBase::SelectBlitter(blitter) == NULL) {
		StrEmpty(blitter) ?
			usererror("Failed to autoprobe blitter") :
			usererror("Failed to select requested blitter '%s'; does it exist?", blitter);
	}
	free(blitter);

	DEBUG(driver, 1, "Loading drivers...");

	if (sounddriver == NULL && _ini_sounddriver != NULL) sounddriver = strdup(_ini_sounddriver);
	_sound_driver = (SoundDriver*)SoundDriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND);
	if (_sound_driver == NULL) {
		StrEmpty(sounddriver) ?
			usererror("Failed to autoprobe sound driver") :
			usererror("Failed to select requested sound driver '%s'", sounddriver);
	}
	free(sounddriver);

	if (videodriver == NULL && _ini_videodriver != NULL) videodriver = strdup(_ini_videodriver);
	_video_driver = (VideoDriver*)VideoDriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO);
	if (_video_driver == NULL) {
		StrEmpty(videodriver) ?
			usererror("Failed to autoprobe video driver") :
			usererror("Failed to select requested video driver '%s'", videodriver);
	}
	free(videodriver);

	if (musicdriver == NULL && _ini_musicdriver != NULL) musicdriver = strdup(_ini_musicdriver);
	_music_driver = (MusicDriver*)MusicDriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC);
	if (_music_driver == NULL) {
		StrEmpty(musicdriver) ?
			usererror("Failed to autoprobe music driver") :
			usererror("Failed to select requested music driver '%s'", musicdriver);
	}
	free(musicdriver);

	/* Initialize the zoom level of the screen to normal */
	_screen.zoom = ZOOM_LVL_NORMAL;

	/* restore saved music volume */
	_music_driver->SetVolume(_msf.music_vol);

	NetworkStartUp(); // initialize network-core

#if defined(ENABLE_NETWORK)
	if (debuglog_conn != NULL && _network_available) {
		const char *not_used = NULL;
		const char *port = NULL;
		uint16 rport;

		rport = NETWORK_DEFAULT_DEBUGLOG_PORT;

		ParseConnectionString(&not_used, &port, debuglog_conn);
		if (port != NULL) rport = atoi(port);

		NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport));
	}
#endif /* ENABLE_NETWORK */

	ScanNewGRFFiles();

	ResetGRFConfig(false);

	/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
	if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();

	/* initialize the ingame console */
	IConsoleInit();
	_cursor.in_window = true;
	InitializeGUI();
	IConsoleCmdExec("exec scripts/autoexec.scr 0");

	/* Take our initial lock on whatever we might want to do! */
	_genworld_paint_mutex->BeginCritical();
	_genworld_mapgen_mutex->BeginCritical();

	GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy
	WaitTillGeneratedWorld();

	CheckForMissingGlyphsInLoadedLanguagePack();

#ifdef ENABLE_NETWORK
	if (network && _network_available) {
		if (network_conn != NULL) {
			const char *port = NULL;
			const char *company = NULL;
			uint16 rport = NETWORK_DEFAULT_PORT;
			CompanyID join_as = COMPANY_NEW_COMPANY;

			ParseConnectionString(&company, &port, network_conn);

			if (company != NULL) {
				join_as = (CompanyID)atoi(company);

				if (join_as != COMPANY_SPECTATOR) {
					join_as--;
					if (join_as >= MAX_COMPANIES) return false;
				}
			}
			if (port != NULL) rport = atoi(port);

			LoadIntroGame();
			_switch_mode = SM_NONE;
			NetworkClientConnectGame(NetworkAddress(network_conn, rport), join_as, join_server_password, join_company_password);
		}
	}
#endif /* ENABLE_NETWORK */

	_video_driver->MainLoop();

	WaitTillSaved();

	/* only save config if we have to */
	if (save_config) {
		SaveToConfig();
		SaveHotkeysToConfig();
		SaveToHighScore();
	}

	/* Reset windowing system, stop drivers, free used memory, ... */
	ShutdownGame();

	free(const_cast<char *>(BaseGraphics::ini_set));
	free(const_cast<char *>(BaseSounds::ini_set));
	free(const_cast<char *>(BaseMusic::ini_set));
	free(_ini_musicdriver);
	free(_ini_sounddriver);
	free(_ini_videodriver);
	free(_ini_blitter);

	return 0;
}
bool NetworkBaseUDP::bindSocketToPort(u16 Port)
{
    /* Create connection-less address */
    return Socket_->bindSocket(NetworkAddress(Port));
}
Beispiel #19
0
/**
 * Main entry point for this lovely game.
 * @param argc The number of arguments passed to this game.
 * @param argv The values of the arguments.
 * @return 0 when there is no error.
 */
int openttd_main(int argc, char *argv[])
{
	char *musicdriver = NULL;
	char *sounddriver = NULL;
	char *videodriver = NULL;
	char *blitter = NULL;
	char *graphics_set = NULL;
	char *sounds_set = NULL;
	char *music_set = NULL;
	Dimension resolution = {0, 0};
	/* AfterNewGRFScan sets save_config to true after scanning completed. */
	bool save_config = false;
	AfterNewGRFScan *scanner = new AfterNewGRFScan(&save_config);
#if defined(ENABLE_NETWORK)
	bool dedicated = false;
	char *debuglog_conn = NULL;

	extern bool _dedicated_forks;
	_dedicated_forks = false;
#endif /* ENABLE_NETWORK */

	_game_mode = GM_MENU;
	_switch_mode = SM_MENU;
	_config_file = NULL;

	GetOptData mgo(argc - 1, argv + 1, _options);
	int ret = 0;

	int i;
	while ((i = mgo.GetOpt()) != -1) {
		switch (i) {
		case 'I': free(graphics_set); graphics_set = stredup(mgo.opt); break;
		case 'S': free(sounds_set); sounds_set = stredup(mgo.opt); break;
		case 'M': free(music_set); music_set = stredup(mgo.opt); break;
		case 'm': free(musicdriver); musicdriver = stredup(mgo.opt); break;
		case 's': free(sounddriver); sounddriver = stredup(mgo.opt); break;
		case 'v': free(videodriver); videodriver = stredup(mgo.opt); break;
		case 'b': free(blitter); blitter = stredup(mgo.opt); break;
#if defined(ENABLE_NETWORK)
		case 'D':
			free(musicdriver);
			free(sounddriver);
			free(videodriver);
			free(blitter);
			musicdriver = stredup("null");
			sounddriver = stredup("null");
			videodriver = stredup("dedicated");
			blitter = stredup("null");
			dedicated = true;
			SetDebugString("net=6");
			if (mgo.opt != NULL) {
				/* Use the existing method for parsing (openttd -n).
				 * However, we do ignore the #company part. */
				const char *temp = NULL;
				const char *port = NULL;
				ParseConnectionString(&temp, &port, mgo.opt);
				if (!StrEmpty(mgo.opt)) scanner->dedicated_host = mgo.opt;
				if (port != NULL) scanner->dedicated_port = atoi(port);
			}
			break;
		case 'f': _dedicated_forks = true; break;
		case 'n':
			scanner->network_conn = mgo.opt; // optional IP parameter, NULL if unset
			break;
		case 'l':
			debuglog_conn = mgo.opt;
			break;
		case 'p':
			scanner->join_server_password = mgo.opt;
			break;
		case 'P':
			scanner->join_company_password = mgo.opt;
			break;
#endif /* ENABLE_NETWORK */
		case 'r': ParseResolution(&resolution, mgo.opt); break;
		case 't': scanner->startyear = atoi(mgo.opt); break;
		case 'd': {
#if defined(WIN32)
				CreateConsole();
#endif
				if (mgo.opt != NULL) SetDebugString(mgo.opt);
				break;
			}
		case 'e': _switch_mode = (_switch_mode == SM_LOAD_GAME || _switch_mode == SM_LOAD_SCENARIO ? SM_LOAD_SCENARIO : SM_EDITOR); break;
		case 'g':
			if (mgo.opt != NULL) {
				_file_to_saveload.SetName(mgo.opt);
				bool is_scenario = _switch_mode == SM_EDITOR || _switch_mode == SM_LOAD_SCENARIO;
				_switch_mode = is_scenario ? SM_LOAD_SCENARIO : SM_LOAD_GAME;
				_file_to_saveload.SetMode(SLO_LOAD, is_scenario ? FT_SCENARIO : FT_SAVEGAME, DFT_GAME_FILE);

				/* if the file doesn't exist or it is not a valid savegame, let the saveload code show an error */
				const char *t = strrchr(_file_to_saveload.name, '.');
				if (t != NULL) {
					FiosType ft = FiosGetSavegameListCallback(SLO_LOAD, _file_to_saveload.name, t, NULL, NULL);
					if (ft != FIOS_TYPE_INVALID) _file_to_saveload.SetMode(ft);
				}

				break;
			}

			_switch_mode = SM_NEWGAME;
			/* Give a random map if no seed has been given */
			if (scanner->generation_seed == GENERATE_NEW_SEED) {
				scanner->generation_seed = InteractiveRandom();
			}
			break;
		case 'q': {
			DeterminePaths(argv[0]);
			if (StrEmpty(mgo.opt)) {
				ret = 1;
				goto exit_noshutdown;
			}

			char title[80];
			title[0] = '\0';
			FiosGetSavegameListCallback(SLO_LOAD, mgo.opt, strrchr(mgo.opt, '.'), title, lastof(title));

			_load_check_data.Clear();
			SaveOrLoadResult res = SaveOrLoad(mgo.opt, SLO_CHECK, DFT_GAME_FILE, SAVE_DIR, false);
			if (res != SL_OK || _load_check_data.HasErrors()) {
				fprintf(stderr, "Failed to open savegame\n");
				if (_load_check_data.HasErrors()) {
					char buf[256];
					SetDParamStr(0, _load_check_data.error_data);
					GetString(buf, _load_check_data.error, lastof(buf));
					fprintf(stderr, "%s\n", buf);
				}
				goto exit_noshutdown;
			}

			WriteSavegameInfo(title);

			goto exit_noshutdown;
		}
		case 'G': scanner->generation_seed = atoi(mgo.opt); break;
		case 'c': free(_config_file); _config_file = stredup(mgo.opt); break;
		case 'x': scanner->save_config = false; break;
		case 'h':
			i = -2; // Force printing of help.
			break;
		}
		if (i == -2) break;
	}

	if (i == -2 || mgo.numleft > 0) {
		/* Either the user typed '-h', he made an error, or he added unrecognized command line arguments.
		 * In all cases, print the help, and exit.
		 *
		 * The next two functions are needed to list the graphics sets. We can't do them earlier
		 * because then we cannot show it on the debug console as that hasn't been configured yet. */
		DeterminePaths(argv[0]);
		TarScanner::DoScan(TarScanner::BASESET);
		BaseGraphics::FindSets();
		BaseSounds::FindSets();
		BaseMusic::FindSets();
		ShowHelp();

		goto exit_noshutdown;
	}

	DeterminePaths(argv[0]);
	TarScanner::DoScan(TarScanner::BASESET);

#if defined(ENABLE_NETWORK)
	if (dedicated) DEBUG(net, 0, "Starting dedicated version %s", _openttd_revision);
	if (_dedicated_forks && !dedicated) _dedicated_forks = false;

#if defined(UNIX) && !defined(__MORPHOS__)
	/* We must fork here, or we'll end up without some resources we need (like sockets) */
	if (_dedicated_forks) DedicatedFork();
#endif
#endif

	LoadFromConfig(true);

	if (resolution.width != 0) _cur_resolution = resolution;

	/*
	 * The width and height must be at least 1 pixel and width times
	 * height times bytes per pixel must still fit within a 32 bits
	 * integer, even for 32 bpp video modes. This way all internal
	 * drawing routines work correctly.
	 */
	_cur_resolution.width  = ClampU(_cur_resolution.width,  1, UINT16_MAX / 2);
	_cur_resolution.height = ClampU(_cur_resolution.height, 1, UINT16_MAX / 2);

	/* Assume the cursor starts within the game as not all video drivers
	 * get an event that the cursor is within the window when it is opened.
	 * Saying the cursor is there makes no visible difference as it would
	 * just be out of the bounds of the window. */
	_cursor.in_window = true;

	/* enumerate language files */
	InitializeLanguagePacks();

	/* Initialize the regular font for FreeType */
	InitFreeType(false);

	/* This must be done early, since functions use the SetWindowDirty* calls */
	InitWindowSystem();

	BaseGraphics::FindSets();
	if (graphics_set == NULL && BaseGraphics::ini_set != NULL) graphics_set = stredup(BaseGraphics::ini_set);
	if (!BaseGraphics::SetSet(graphics_set)) {
		if (!StrEmpty(graphics_set)) {
			BaseGraphics::SetSet(NULL);

			ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_GRAPHICS_NOT_FOUND);
			msg.SetDParamStr(0, graphics_set);
			ScheduleErrorMessage(msg);
		}
	}
	free(graphics_set);

	/* Initialize game palette */
	GfxInitPalettes();

	DEBUG(misc, 1, "Loading blitter...");
	if (blitter == NULL && _ini_blitter != NULL) blitter = stredup(_ini_blitter);
	_blitter_autodetected = StrEmpty(blitter);
	/* Activate the initial blitter.
	 * This is only some initial guess, after NewGRFs have been loaded SwitchNewGRFBlitter may switch to a different one.
	 *  - Never guess anything, if the user specified a blitter. (_blitter_autodetected)
	 *  - Use 32bpp blitter if baseset or 8bpp-support settings says so.
	 *  - Use 8bpp blitter otherwise.
	 */
	if (!_blitter_autodetected ||
			(_support8bpp != S8BPP_NONE && (BaseGraphics::GetUsedSet() == NULL || BaseGraphics::GetUsedSet()->blitter == BLT_8BPP)) ||
			BlitterFactory::SelectBlitter("32bpp-anim") == NULL) {
		if (BlitterFactory::SelectBlitter(blitter) == NULL) {
			StrEmpty(blitter) ?
				usererror("Failed to autoprobe blitter") :
				usererror("Failed to select requested blitter '%s'; does it exist?", blitter);
		}
	}
	free(blitter);

	if (videodriver == NULL && _ini_videodriver != NULL) videodriver = stredup(_ini_videodriver);
	DriverFactoryBase::SelectDriver(videodriver, Driver::DT_VIDEO);
	free(videodriver);

	InitializeSpriteSorter();

	/* Initialize the zoom level of the screen to normal */
	_screen.zoom = ZOOM_LVL_NORMAL;

	NetworkStartUp(); // initialize network-core

#if defined(ENABLE_NETWORK)
	if (debuglog_conn != NULL && _network_available) {
		const char *not_used = NULL;
		const char *port = NULL;
		uint16 rport;

		rport = NETWORK_DEFAULT_DEBUGLOG_PORT;

		ParseConnectionString(&not_used, &port, debuglog_conn);
		if (port != NULL) rport = atoi(port);

		NetworkStartDebugLog(NetworkAddress(debuglog_conn, rport));
	}
#endif /* ENABLE_NETWORK */

	if (!HandleBootstrap()) {
		ShutdownGame();

		goto exit_bootstrap;
	}

	VideoDriver::GetInstance()->ClaimMousePointer();

	/* initialize screenshot formats */
	InitializeScreenshotFormats();

	BaseSounds::FindSets();
	if (sounds_set == NULL && BaseSounds::ini_set != NULL) sounds_set = stredup(BaseSounds::ini_set);
	if (!BaseSounds::SetSet(sounds_set)) {
		if (StrEmpty(sounds_set) || !BaseSounds::SetSet(NULL)) {
			usererror("Failed to find a sounds set. Please acquire a sounds set for OpenTTD. See section 4.1 of README.md.");
		} else {
			ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_SOUNDS_NOT_FOUND);
			msg.SetDParamStr(0, sounds_set);
			ScheduleErrorMessage(msg);
		}
	}
	free(sounds_set);

	BaseMusic::FindSets();
	if (music_set == NULL && BaseMusic::ini_set != NULL) music_set = stredup(BaseMusic::ini_set);
	if (!BaseMusic::SetSet(music_set)) {
		if (StrEmpty(music_set) || !BaseMusic::SetSet(NULL)) {
			usererror("Failed to find a music set. Please acquire a music set for OpenTTD. See section 4.1 of README.md.");
		} else {
			ErrorMessageData msg(STR_CONFIG_ERROR, STR_CONFIG_ERROR_INVALID_BASE_MUSIC_NOT_FOUND);
			msg.SetDParamStr(0, music_set);
			ScheduleErrorMessage(msg);
		}
	}
	free(music_set);

	if (sounddriver == NULL && _ini_sounddriver != NULL) sounddriver = stredup(_ini_sounddriver);
	DriverFactoryBase::SelectDriver(sounddriver, Driver::DT_SOUND);
	free(sounddriver);

	if (musicdriver == NULL && _ini_musicdriver != NULL) musicdriver = stredup(_ini_musicdriver);
	DriverFactoryBase::SelectDriver(musicdriver, Driver::DT_MUSIC);
	free(musicdriver);

	/* Take our initial lock on whatever we might want to do! */
	_modal_progress_paint_mutex->BeginCritical();
	_modal_progress_work_mutex->BeginCritical();

	GenerateWorld(GWM_EMPTY, 64, 64); // Make the viewport initialization happy
	WaitTillGeneratedWorld();

	LoadIntroGame(false);

	CheckForMissingGlyphs();

	/* ScanNewGRFFiles now has control over the scanner. */
	ScanNewGRFFiles(scanner);
	scanner = NULL;

	VideoDriver::GetInstance()->MainLoop();

	WaitTillSaved();

	/* only save config if we have to */
	if (save_config) {
		SaveToConfig();
		SaveHotkeysToConfig();
		WindowDesc::SaveToConfig();
		SaveToHighScore();
	}

	/* Reset windowing system, stop drivers, free used memory, ... */
	ShutdownGame();
	goto exit_normal;

exit_noshutdown:
	/* These three are normally freed before bootstrap. */
	free(graphics_set);
	free(videodriver);
	free(blitter);

exit_bootstrap:
	/* These are normally freed before exit, but after bootstrap. */
	free(sounds_set);
	free(music_set);
	free(musicdriver);
	free(sounddriver);

exit_normal:
	free(BaseGraphics::ini_set);
	free(BaseSounds::ini_set);
	free(BaseMusic::ini_set);

	free(_ini_musicdriver);
	free(_ini_sounddriver);
	free(_ini_videodriver);
	free(_ini_blitter);

	delete scanner;

#ifdef ENABLE_NETWORK
	extern FILE *_log_fd;
	if (_log_fd != NULL) {
		fclose(_log_fd);
	}
#endif /* ENABLE_NETWORK */

	return ret;
}
Beispiel #20
0
	virtual void OnNewGRFsScanned()
	{
		ResetGRFConfig(false);

		TarScanner::DoScan(TarScanner::SCENARIO);

		AI::Initialize();
		Game::Initialize();

		/* We want the new (correct) NewGRF count to survive the loading. */
		uint last_newgrf_count = _settings_client.gui.last_newgrf_count;
		LoadFromConfig();
		_settings_client.gui.last_newgrf_count = last_newgrf_count;
		/* Since the default for the palette might have changed due to
		 * reading the configuration file, recalculate that now. */
		UpdateNewGRFConfigPalette();

		Game::Uninitialize(true);
		AI::Uninitialize(true);
		CheckConfig();
		LoadFromHighScore();
		LoadHotkeysFromConfig();
		WindowDesc::LoadFromConfig();

		/* We have loaded the config, so we may possibly save it. */
		*save_config_ptr = save_config;

		/* restore saved music volume */
		MusicDriver::GetInstance()->SetVolume(_settings_client.music.music_vol);

		if (startyear != INVALID_YEAR) _settings_newgame.game_creation.starting_year = startyear;
		if (generation_seed != GENERATE_NEW_SEED) _settings_newgame.game_creation.generation_seed = generation_seed;

#if defined(ENABLE_NETWORK)
		if (dedicated_host != NULL) {
			_network_bind_list.Clear();
			*_network_bind_list.Append() = stredup(dedicated_host);
		}
		if (dedicated_port != 0) _settings_client.network.server_port = dedicated_port;
#endif /* ENABLE_NETWORK */

		/* initialize the ingame console */
		IConsoleInit();
		InitializeGUI();
		IConsoleCmdExec("exec scripts/autoexec.scr 0");

		/* Make sure _settings is filled with _settings_newgame if we switch to a game directly */
		if (_switch_mode != SM_NONE) MakeNewgameSettingsLive();

#ifdef ENABLE_NETWORK
		if (_network_available && network_conn != NULL) {
			const char *port = NULL;
			const char *company = NULL;
			uint16 rport = NETWORK_DEFAULT_PORT;
			CompanyID join_as = COMPANY_NEW_COMPANY;

			ParseConnectionString(&company, &port, network_conn);

			if (company != NULL) {
				join_as = (CompanyID)atoi(company);

				if (join_as != COMPANY_SPECTATOR) {
					join_as--;
					if (join_as >= MAX_COMPANIES) {
						delete this;
						return;
					}
				}
			}
			if (port != NULL) rport = atoi(port);

			LoadIntroGame();
			_switch_mode = SM_NONE;
			NetworkClientConnectGame(NetworkAddress(network_conn, rport), join_as, join_server_password, join_company_password);
		}
#endif /* ENABLE_NETWORK */

		/* After the scan we're not used anymore. */
		delete this;
	}
Beispiel #21
0
int
ServerApp::mainLoop()
{
	// create socket multiplexer.  this must happen after daemonization
	// on unix because threads evaporate across a fork().
	SocketMultiplexer multiplexer;
	setSocketMultiplexer(&multiplexer);

	// if configuration has no screens then add this system
	// as the default
	if (args().m_config->begin() == args().m_config->end()) {
		args().m_config->addScreen(args().m_name);
	}

	// set the contact address, if provided, in the config.
	// otherwise, if the config doesn't have an address, use
	// the default.
	if (m_synergyAddress->isValid()) {
		args().m_config->setSynergyAddress(*m_synergyAddress);
	}
	else if (!args().m_config->getSynergyAddress().isValid()) {
		args().m_config->setSynergyAddress(NetworkAddress(kDefaultPort));
	}

	// canonicalize the primary screen name
	String primaryName = args().m_config->getCanonicalName(args().m_name);
	if (primaryName.empty()) {
		LOG((CLOG_CRIT "unknown screen name `%s'", args().m_name.c_str()));
		return kExitFailed;
	}

	// load all available plugins.
	ARCH->plugin().load();
	// pass log and arch into plugins.
	ARCH->plugin().init(Log::getInstance(), Arch::getInstance());

	// start server, etc
	appUtil().startNode();
	
	// init ipc client after node start, since create a new screen wipes out
	// the event queue (the screen ctors call adoptBuffer).
	if (argsBase().m_enableIpc) {
		initIpcClient();
	}

	// init event for all available plugins.
	ARCH->plugin().initEvent(m_serverScreen->getEventTarget(), m_events);

	// handle hangup signal by reloading the server's configuration
	ARCH->setSignalHandler(Arch::kHANGUP, &reloadSignalHandler, NULL);
	m_events->adoptHandler(m_events->forServerApp().reloadConfig(),
		m_events->getSystemTarget(),
		new TMethodEventJob<ServerApp>(this, &ServerApp::reloadConfig));

	// handle force reconnect event by disconnecting clients.  they'll
	// reconnect automatically.
	m_events->adoptHandler(m_events->forServerApp().forceReconnect(),
		m_events->getSystemTarget(),
		new TMethodEventJob<ServerApp>(this, &ServerApp::forceReconnect));

	// to work around the sticky meta keys problem, we'll give users
	// the option to reset the state of synergys
	m_events->adoptHandler(m_events->forServerApp().resetServer(),
		m_events->getSystemTarget(),
		new TMethodEventJob<ServerApp>(this, &ServerApp::resetServer));

	// run event loop.  if startServer() failed we're supposed to retry
	// later.  the timer installed by startServer() will take care of
	// that.
	DAEMON_RUNNING(true);
	
#if defined(MAC_OS_X_VERSION_10_7)
	
	Thread thread(
		new TMethodJob<ServerApp>(
			this, &ServerApp::runEventsLoop,
			NULL));
	
	// wait until carbon loop is ready
	OSXScreen* screen = dynamic_cast<OSXScreen*>(
		m_serverScreen->getPlatformScreen());
	screen->waitForCarbonLoop();
	
	runCocoaApp();
#else
	m_events->loop();
#endif
	
	DAEMON_RUNNING(false);

	// close down
	LOG((CLOG_DEBUG1 "stopping server"));
	m_events->removeHandler(m_events->forServerApp().forceReconnect(),
		m_events->getSystemTarget());
	m_events->removeHandler(m_events->forServerApp().reloadConfig(),
		m_events->getSystemTarget());
	cleanupServer();
	updateStatus();
	LOG((CLOG_NOTE "stopped server"));

	if (argsBase().m_enableIpc) {
		cleanupIpcClient();
	}

	// unload all plugins.
	ARCH->plugin().unload();

	return kExitSuccess;
}