⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.cpp

📁 swain-0.5.2.zip的源代码,比较好用,希望大家喜欢.
💻 CPP
字号:
/*
This file is part of SWAIN (http://sourceforge.net/projects/swain).
Copyright (C) 2006  Daniel Lindstr鰉 and Daniel Nilsson

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA  02110-1301, USA.
*/
// server.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ServerPacketFactory.h"
#include "..\Common\ConnectionHandler.h"
#include "ServerPacketHandler.h"
//#include "DynList.h"

#define GSIZE 5

void cls();
void addNewUser(UserDB *userdb);
void createGroup(UserDB *userdb);
void addUserToGroup(UserDB *userdb);
void writeToFile(UserDB *userdb);
void readFromFile(UserDB *userdb);
void listUsers(UserDB *userdb);

int _tmain(int argc, _TCHAR* argv[])
{
	bool run = true;
	char c;

	printf("SWAIN version 0.5.2, Copyright (C) 2006 Daniel L & Daniel N\n");
	printf("This is free software.  You may redistribute copies of it under the terms of\n");
	printf("the GNU General Public License v2 <http://www.gnu.org/licenses/gpl.html>.\n");
	printf("There is NO WARRANTY, to the extent permitted by law.\n");
	printf("See `license.txt' for details.\n\n");

	ServerPacketFactory *spf = new ServerPacketFactory();
	ConnectionHandler *ch = new ConnectionHandler(spf);
	//fr錱a om ladda fr錸 fil.. eventuellt anv鋘da argv.
	UserDB *userdb = new UserDB();
	ServerPacketHandler *sph = new ServerPacketHandler(userdb);
	ch->setPacketHandler(sph);
	ch->startListen(SERVER_PORT);
	ch->startReceive();
	ch->startSend();
	ch->startHandle();
	while(run){
		fflush(stdin);
		//cls();
		printf("1. Add new user.\n");
		printf("2. Create new group.\n");
		printf("3. Add user to group.\n");
		printf("4. Write to file.\n");
		printf("5. Read from file.\n");
		printf("6. List all users.\n");
		printf("--------\n");
		printf("Q		Quit\n");
		c = getchar();
		switch(c){
			case('1'): addNewUser(userdb);		break;
			case('2'): createGroup(userdb);		break;
			case('3'): addUserToGroup(userdb);	break;
			case('4'): writeToFile(userdb);		break;
			case('5'): readFromFile(userdb);	break;
			case('6'): listUsers(userdb);		break;
			case('q'):
			case('Q'): run = false; break;
			default: break;
		}
	}
	return 0;
}

void addNewUser(UserDB *userdb){
	char name[NAME_BUF_SIZE];
	char passwd[PASSWORD_BUF_SIZE];
	int id;

	fflush(stdin);
	printf("Name: ");
	fgets(name, NAME_BUF_SIZE, stdin);
	fflush(stdin);
	printf("Password: ");
	fgets(passwd, NAME_BUF_SIZE, stdin);
	if(name[strlen(name)-1] == '\n')
		name[strlen(name)-1] = '\0';
	if(passwd[strlen(passwd)-1] == '\n')
		passwd[strlen(passwd)-1] = '\0';
	fflush(stdin);
	id = userdb->addUser(name, passwd);
	printf("User %d added successfully!\nPress enter to continue.\n", id);
	getchar();
}


void createGroup(UserDB *userdb){
	char gname[NAME_BUF_SIZE];
	int id = -1;
	fflush(stdin);
	printf("Grupp namn: ");
	fgets(gname, NAME_BUF_SIZE, stdin);
	if(gname[strlen(gname)-1] == '\n')
		gname[strlen(gname)-1] = '\0';

	fflush(stdin);
	id = userdb->createGroup(gname);
	printf("Group %d created!\nPress enter to continue.\n", id);
	fflush(stdin);
	getchar();
}

void addUserToGroup(UserDB *userdb){
	char gid[GSIZE];
	char id[GSIZE];

	int _gid, _id;
	fflush(stdin);
	printf("user id: ");
	fgets(id, GSIZE, stdin);
	fflush(stdin);
	printf("group id: ");
	fgets(gid, GSIZE, stdin);
	fflush(stdin);

	if(gid[strlen(gid)-1] == '\n')
		gid[strlen(gid)-1] = '\0';
	if(id[strlen(id)-1] == '\n')
		id[strlen(id)-1] = '\0';

	_gid = atoi(gid);
	_id = atoi(id);


	if(!userdb->userExists(_id) || !userdb->groupExists(_gid) || gid[0]>'9' || gid[0] < '0' || id[0]>'9' || id[0] < '0'){
		printf("Error input!!\nPress enter to continue.\n");
		getchar();
		return;
	}
	
	userdb->addUserToGroup(_id, _gid);
	printf("user %s added to group %s.\nPress enter to continue.\n", id, gid);
	getchar();
}


void writeToFile(UserDB *userdb){
	fflush(stdin);
	if(!userdb->writeToFile()){
		printf("Error writing to file.\nPress enter to continue.\n");
		getchar();
	}
	else{
		printf("Writing succeeded.\nPress enter to continue.\n");
		getchar();
	}
	fflush(stdin);
}


void readFromFile(UserDB *userdb){
	fflush(stdin);
	if(!userdb->readFromFile()){
		printf("Error reading file.\nPress enter to continue.\n");
		getchar();
	}
	else{
		printf("Databases loaded successfully.\nPress enter to continue.\n");
		getchar();
	}

}


void listUsers(UserDB *userdb){
	int count = userdb->getDbSize()+1;
	const char *name;
	int num_listed = 0;
	printf("Users:\n");
	for(int i=0; i<count; i++){
		name = userdb->getName(i);
		if(name == NULL)
			continue;
		printf("%s\n", name);
		num_listed++;
		if(num_listed%20 == 0){
			printf("Press enter to continue continue.\n");
			fflush(stdin);
			getchar();
		}
	}
	fflush(stdin);
	printf("All users listed.\nPress enter to contiue.");
	getchar();
}



void cls()
{
  COORD coordScreen = { 0, 0 }; /* here's where we'll home the cursor */
  DWORD cCharsWritten;
  CONSOLE_SCREEN_BUFFER_INFO csbi; /* to get buffer info */
  DWORD dwConSize; /* number of character cells in the current buffer */
 
  /* get the output console handle */
  HANDLE hConsole=GetStdHandle(STD_OUTPUT_HANDLE);
  /* get the number of character cells in the current buffer */
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
  /* fill the entire screen with blanks */
  FillConsoleOutputCharacter(hConsole, (TCHAR) ' ',
      dwConSize, coordScreen, &cCharsWritten);
  /* get the current text attribute */
  GetConsoleScreenBufferInfo(hConsole, &csbi);
  /* now set the buffer's attributes accordingly */
  FillConsoleOutputAttribute(hConsole, csbi.wAttributes,
      dwConSize, coordScreen, &cCharsWritten);
  /* put the cursor at (0, 0) */
  SetConsoleCursorPosition(hConsole, coordScreen);
  return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -