Example #1
0
// This is the entry point into the server (the main() function)
int main() {
	// Print starting info to the console
	cout << "LL        EE    EE   GG     GG   OO\n";
	cout << "LL        EE    EE   GGG    GG   OO\n";
	cout << "LL        EE    EE   GGG    GG   OO\n";
	cout << "LL        EE    EE   GG G   GG   OO\n";
	cout << "LL        EE    EE   GG  G  GG   OO\n";
	cout << "LLLLLL    EE    EE   GG   G GG   OO\n";
	cout << "LLLLLLLL   EEEEEE    GG    GGG   OO   Server\n";
	cout << "\tOriginal project: luniserver.sf.net\n\n\n";

	cout << "Initializing LUNI test server...\n \nPress enter type \"help\" and enter again for commands\n\n";

	// Initialize the auth, character, and world CONNECT_INFO structs
	CONNECT_INFO auth, character, world;

	// Load the values for them and store them into their structs
	LoadConfig(auth, character, world);

	// Create the directory .//logs// if it does not exist
	_mkdir("logs");

	// If debug is on, print additional data to the console
	#ifdef DEBUG
	{
		cout << "\nDEBUG is ON!\n";
		//cout << "\n Minifigure size: " << sizeof(MinifigureData) << endl;

		auto testbin = OpenPacket(".\\char\\char_aw2.bin");
		//PacketCharacter * pk = (PacketCharacter*) testbin;

		/*vector< Ref<Character> > characters(4);
		auto aux = (uchar*)testbin.data() +22;
		characters[0] = Ref<Character>(new Character(aux));
		cout << "\nTest (reading characters form char\\char_aw2.bin) character1 : " << characters[0]->GetName() << endl;
		//cout << characters[0]->Data.travelling << endl;
		
		aux += characters[0]->GetGeneratedPacketSize();
		characters[1] = Ref<Character>(new Character(aux));
		cout << "\nTest character2 : " << characters[1]->GetName() << endl;*/

		string s = "ciao";
		RakNet::BitStream bs;
		bs.WriteCompressed(s);

		cout << endl << RawDataToString(bs.GetData(), bs.GetNumberOfBytesUsed()) << endl;
		SavePacket("the Test.bin", (char*)bs.GetData(), bs.GetNumberOfBytesUsed());

		cout << endl;
	}
	#endif

	// Create the UserPool to store all users by their systemAddress (shared by threads)
	Ref< UsersPool > OnlineUsers = new UsersPool();
	Ref< CharactersPool > OnlineCharacters = new CharactersPool();

	// Create a new CrossThreadQueue for writing output to the console from a thread
	Ref< CrossThreadQueue< string > > OutputQueue = new CrossThreadQueue< string >();

	// Start the three new threads (Auth, Char, and World servers)
	thread thauth(AuthLoop, &auth, OnlineUsers, OutputQueue);
	thread thchar(CharactersLoop, &character, OnlineUsers, OnlineCharacters, OutputQueue);
	thread thworld(WorldLoop, &world, OnlineUsers, OnlineCharacters, OutputQueue);

	// If quit is ever equal to true, quit the server
	bool quit = false;

	// Keep the server from quitting by using a infinite loop
	while (!quit) {
		if (OutputQueue->Count() > 0) { // Pop the thread messages
			cout << OutputQueue->Pop();
		}

		if (_kbhit()) { // Parsing server commands. Press enter to start writing a command (may need to lock consoleoutmutex...)
			string command; // Initialize command string...
			cout << "> "; // Print "> " to show user where to type
			cin >> command; // Get the command
			cout << endl; // End the line

			// Match command to a pre-specified command here...
			if (command == "help") cout << "\nAvailable commands: \nquit = Quit the Server \nregister = Register New User \nuser_online = Show Number of Online Users";
			else if (command == "quit") quit = LUNIterminate = true;
			else if (command == "character_log_enable") character.logFile = true;
			else if (command == "character_log_disable") character.logFile = false;
			else if (command == "world_log_enable") world.logFile = true;
			else if (command == "world_log_disable") world.logFile = false;
			else if (command == "user_online") {
				cout << "\n Online user: "******"register") {
				string username, password;
				cout << "Username: "******"Password: "******"INSERT INTO `accounts` (`id`, `name`, `password`, `email`, `ip`, `rank`, `numChars`, `frontChar`, `lastLog`, `activeSub`, `subTime`, `legoClub`, `locked`, `banned`, `loginTries`) VALUES (NULL, '" + username + "', '" + password + "', '', '127.0.0.1', '0', '0', '0', CURRENT_TIMESTAMP, '0', '', '', '0', '0', '0');");
				} else cout << "Username already exist!\n";

			} else cout << "Invalid Command: " << command << "!" << endl;