FTP文件管理项目(本地云)项目日报(九)

举报
看,未来 发表于 2020/12/30 01:24:01 2020/12/30
【摘要】 文章目录 剩下的四个人本日任务完成情况代码 写了这第九篇日报,就意味着这个为期12个“工作日”的项目到了大后期啦。 这两天我去找我哥聊了聊这个项目的架构,他是做Java的,不过对我用C++讲的这个项目还是很有兴趣哈哈哈。 一路下来,只剩四个人啦,从刚开始的十个人。 剩下的四个人 1号(服务器)日报八(系统整体重构图)5号(测试端)日报7(...



一路下来,只剩四个人啦,从刚开始的十个人。

剩下的四个人

1号(服务器) 日报八(系统整体重构图)
5号(测试端) 日报7(QT定时器)
日报8(日志模块)
8号(客户端)
9号(客户端)

其中5号已经交上代码功成身退了。

其实我的初衷也是如此,除去我还剩九个人,最后能留下三个足矣。
且不说我们做的咋样,只要坚持下来,就是好样的!!!


本日任务

重构

完成情况

高估了自己,任务太重,FTP服务器模块还没重构,DB服务器重构之后还没对各模块再次测试,估计还要一天。

这两天做了些动态库,把进程间通信由静态变为动态(之前代码里的备注说回头改的地方),现在可以自由拓展了。

数据库功能完善与上锁,协议包再修订(今晚还要微调)。

代码

Linux系统关了,不想再开,关之前忘了把代码拿出来,所以下面这个代码是改bug之前的。

//DB_command.h

#include"DB_base.h"
#include"Mutex.h"

class DB_command
{
private:
	DB_base* My_DB;
	Mutex* DB_Mutex;

public:
	DB_command();

	//增
	bool add_user(int id,int pwd,char* tel,char* err);
	bool add_file(char* file_name,int owner_id, int pwd, int mode,int dir_id,int force,char* err);//force,是否替换原文件
	bool add_list(int id,int pwd,int father_dir,char* dir_name,char* err); //删
	bool del_user(int user_id,int pwd);
	bool del_file(int user_id,int pwd,char* file_name,int dir_id,char* err);
	bool del_list(int user_id,int pwd,int dir_id,char* err); //查
	bool login_check(int user_id,int pwd);	//登录验证,不带目录id
	int login(int user_id, int pwd,char* err);	//带上id的登录验证
	int pwd(int user_id,char* tel);
	void file_list(int user_id,int pwd,int dir_id, char* ret,char* err); bool download(int user_id,int pwd,char* file_name, int dir_id,int shared,char* err); //改
	bool cpwd(int user_id, int old_pwd, int new_id,char* err);
	bool cfile_addr(int user_id,int pwd,char* file_name, int old_dir, int new_dir,char* err);
	bool cdir_addr(int user_id,int pwd,char* dir_name, int new_dir,char* err);
	bool share_file(int user_id, int pwd, int shared, char* file_name, int dir_id,char* err);
	bool cfile_name(int user_id, int pwd, char* file_name, int file_id, int dir_id,char* err);
	bool cdir_name(int user_id, int pwd, char* dir_name, int dir_id,char* err);

	//适配器函数
	char* dir(int dir_id);	//将dir_id转换为dir_name
	char* file(int file_id);//将file_id转换为file_name
};

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
//DB_command.cpp

#include "DB_command.h"

DB_command::DB_command() {
/*	DB_init* init = new DB_init();
	init->commmand();
	delete init;*/

	char* name = new char[10];
	sprintf(name, "FTP.db");
	My_DB = DB_base::instence(name);
	delete name;

	DB_Mutex = new Mutex();
}

//增
bool DB_command::add_user(int id, int pwd, char* tel,char* err) { //先查一下该用户是否已存在
	char* sql = new char[120];
	int ret = 0;
	DB_Mutex->lock();
	sprintf(sql, "select pwd from UserMsg where id = %d", id);
	if (int a = My_DB->return_serch_num(sql) == 1) {
		strcpy(err, "User already in!");
		DB_Mutex->unlock();
		delete sql;
		return false;
	}
	else {
	//查询目录id使用情况
		sprintf(sql, "select max(dir_id) from Dir_Msg");
		int max_dir = My_DB->ireturn_serch(sql,1) + 1; char* name = new char[6];
		sprintf(name,"MyDir");
		sprintf(sql, "insert into DirMsg values(%d,'%s',%d,%d)", max_dir,name, 0, id);
		ret = My_DB->excute(sql); sprintf(sql, "insert into UserMsg values(%d,%d,'%s',%d)", id, pwd, tel, max_dir); ret = My_DB->excute(sql); DB_Mutex->unlock();
		delete sql;
		return ret;
	}
}

bool DB_command::add_file(char* file_name, int owner_id, int pwd,int mode, int dir_id,int change, char* err) {
	DB_Mutex->lock();
	if (!login_check(owner_id, pwd)) {
		strcpy(err, "Have no right!");
		DB_Mutex->unlock();
		return false;
	}
	char* sql = new char[120];
	int ret = 0;
	sprintf(sql, "select owner_id from DirMsg where dir_id = %d", dir_id);
	if (My_DB->return_serch_num(sql)) {
		//再对文件id进行安排
		sprintf(sql, "select owner_id from FileMsg where file_name = '%s' and dir_id = %d", file_name, dir_id);
		if (My_DB->ireturn_serch(sql, 1) == owner_id) { if (change == 0) { DB_Mutex->unlock(); strcpy(err, "Duplicate file!"); delete sql; return false; } else { sprintf(sql, "update FileMsg set file_name = '%s' and mode = %d ", file_name, mode); ret = My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true; }
		}
		else { sprintf(sql, "select max(file_id) from FileMsg"); int file_id = My_DB->ireturn_serch(sql, 1) + 1; sprintf(sql, "insert into FileMsg values('%s',%d,%d,%d,%d,0)", file_name, owner_id, mode, dir_id, file_id); ret = My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "dir is not exit!");
		delete sql;
		return false;
	}
}

bool DB_command::add_list(int id, int pwd, int father_dir, char* dir_name, char* err) {
	DB_Mutex->lock();
	if (login_check(id,pwd)) {
		char* sql = new char[120];
		int ret = 0;
		sprintf(sql, "select grade from DirMsg where dir_name = '%s'", dir_name);
		ret = My_DB->ireturn_serch(sql, 1);
		if (ret == father_dir) { DB_Mutex->unlock(); strcpy(err, "dir is exit!"); delete sql; return false;
		}
		else { sprintf(sql, "select max(dir_id) from Dir_Msg"); int max_dir = My_DB->ireturn_serch(sql, 1) + 1; sprintf(sql, "insert into DirMsg values(%d,'%s',%d,%d)", max_dir,dir_name,father_dir,id); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");	//无权
		return false;
	}
} //删
bool DB_command::del_user(int user_id,int pwd) {
	DB_Mutex->lock();
	if (login_check(user_id,pwd)) {
		char* sql = new char[120];
		sprintf(sql, "delete from UserMsg where id = %d and pwd = %d", user_id, pwd);
		My_DB->excute(sql);
		sprintf(sql, "delete from FileMsg where owner_id = %d", user_id); My_DB->excute(sql);
		sprintf(sql, "delete from DirMsg where owner_id = %d", user_id);
		My_DB->excute(sql);
		DB_Mutex->unlock();
		delete sql;
		return true;
	}
	else {
		DB_Mutex->unlock();
		return false;
	}
}

bool DB_command::del_file(int user_id, int pwd, char* file_name,int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select * from FileMsg where owner_id = %d and file_name = '%s'", user_id, file_name);
		int ret = My_DB->return_serch_num(sql);
		if (ret) { sprintf(sql, "delete from FileMsg where dir_id = %d and file_name = '%s'", dir_id, file_name); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true;
		}
		else { DB_Mutex->unlock(); strcpy(err, "Have no right!");	//无权 delete sql; return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");	//无权
		return false;
	}
}

//目前不支持非空文件夹删除操作,不然子又生孙,孙又生子,怎么搞?
bool DB_command::del_list(int user_id, int pwd, int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select * from FileMsg where owner_id = %d and dir_id = %d", user_id, dir_id);
		if (!My_DB->return_serch_num) { sprintf(sql, "select * from DirMsg where grade = %d", dir_id); if (!My_DB->return_serch_num) { sprintf(sql, "delete from DirMsg where dir_id = %d", dir_id); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true; } else { DB_Mutex->unlock(); strcpy(err, "Directs residual!"); delete sql; return false; }
		}
		else { DB_Mutex->unlock(); strcpy(err, "Files residual!"); delete sql; return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

//查
bool DB_command::login_check(int user_id, int pwd) {	//哈哈,这个不能上锁
	char* sql = new char[120];
	sprintf(sql, "select pwd from UserMsg where id = %d", user_id);
	int ret = My_DB->ireturn_serch(sql,1);
	delete sql;
	if (pwd == ret)
		return true;
	else
		return false;
}

int DB_command::login(int user_id, int pwd, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select dir_id from UserMsg where id = %d", user_id);
		int ret = My_DB->ireturn_serch(sql, 1);
		DB_Mutex->unlock();
		delete sql;
		return ret;
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Login defeat!");
		return false;
	}
}

int DB_command::pwd(int user_id, char* tel) {
	char* sql = new char[120];
	sprintf(sql, "select pwd from UserMsg where id = %d and tel = '%s'", user_id,tel);
	DB_Mutex->lock();
	int ret = My_DB->ireturn_serch(sql,1);
	DB_Mutex->unlock();
	delete sql;
	return ret;
}

void DB_command::file_list(int user_id, int pwd, int dir_id, char* ret, char* err){
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[150];
		sprintf(sql, "select file_id,file_name,FileMsg.[dir_id],dir_name from FileMsg,DirMsg where FileMsg.[owner_id] = %d and FileMsg.[dir_id] = DirMsg.[dir_id]", user_id); My_DB->ret_any(sql, ret);
		DB_Mutex->unlock();
		delete sql;
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
	}
}

bool DB_command::download(int user_id, int pwd, char* file_name, int dir_id,int shared, char* err) {
	char* sql = new char[120]; if (shared != 0) {
		sprintf(sql, "select shared from FileMsg where file_name = '%s' and owner_id = %d", file_name,user_id);
		DB_Mutex->lock();
		int ret = My_DB->ireturn_serch(sql,1);
		DB_Mutex->unlock();
		if (shared == ret) { delete sql; return true;
		} }
	else {
		DB_Mutex->lock();
		if (login_check(user_id, pwd)) { sprintf(sql, "select file_id from FileMsg where file_name = '%s' and dir_id = %d", file_name,dir_id); if (My_DB->return_serch_num) { DB_Mutex->unlock(); delete sql; return true; } else { DB_Mutex->unlock(); strcpy(err, "Can't find file!"); delete sql; return false; } }
		else { DB_Mutex->unlock(); strcpy(err, "Have no right!"); delete sql; return false;
		}
	}
}

//改
bool DB_command::cpwd(int user_id, int old_pwd, int new_id, char* err) {
	char* sql = new char[120];
	sprintf(sql, "update UserMsg set pwd = %d where id = %d and pwd = %d", user_id, old_pwd, new_id);
	DB_Mutex->lock();
	int ret = My_DB->excute(sql);
	DB_Mutex->unlock();
	delete sql;
	return ret;
}

bool DB_command::cfile_addr(int user_id, int pwd, char* file_name, int old_dir, int new_dir, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from DirMsg where dir_id = %d", new_dir); int id = My_DB->ireturn_serch(sql,1);
		int ret = 0;
		if (user_id == id) { sprintf(sql, "select owner_id from FileMsg where file_name = '%s'", file_name); ret = My_DB->ireturn_serch(sql, 1); if (user_id == id) { sprintf(sql, "update FileMsg set dir_id = %d where owner_id = %d and dir_id = %d and file_name = '%s'", new_dir, user_id, old_dir, file_name); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true; } else { DB_Mutex->unlock(); strcpy(err, "Have no right!"); delete sql; return false; }
		}
		else { DB_Mutex->unlock(); strcpy(err, "Dir is not exit!"); delete sql; return false;
		}	
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

bool DB_command::cdir_addr(int user_id, int pwd, char* dir_name, int new_dir, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[150];
		sprintf(sql, "select grade from DirMsg where dir_id = %d and owner_id = (select owner_id from DirMsg where dir_name = '%s')",new_dir,dir_name);
		if (My_DB->return_serch_num == 1) { sprintf(sql, "update DirMsg set where grade = %d", new_dir); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true; }
		else { DB_Mutex->unlock(); strcpy(err, "Have no right!"); delete sql; return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}		
}

bool DB_command::share_file(int user_id, int pwd, int shared,char* file_name,int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from FileMsg where file_name = '%s'",file_name);
		int id = My_DB->ireturn_serch(sql, 1);
		if (user_id == id) { sprintf(sql, "update FileMsg set shared = %d where file_name = '%s' and dir_id = %d", shared, file_name, dir_id); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true;
		}
		else { DB_Mutex->unlock(); strcpy(err, "Have no right!"); delete sql; return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}	
}

bool DB_command::cfile_name(int user_id, int pwd, char* file_name, int file_id,int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from FileMsg where file_id = %d", file_id);
		int id = My_DB->ireturn_serch(sql, 1);
		if (user_id == id) { sprintf(sql, "update FileMsg set file_name = '%s' where file_id = %d and dir_id = %d", file_name, file_id, dir_id); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true;
		}
		else { DB_Mutex->unlock(); strcpy(err, "Have no right!"); delete sql; return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

bool DB_command::cdir_name(int user_id, int pwd, char* dir_name, int dir_id, char* err) {
	DB_Mutex->lock();
	if (login_check(user_id, pwd)) {
		char* sql = new char[120];
		sprintf(sql, "select owner_id from DirMsg where dir_name = '%s'", dir_name);
		int id = My_DB->ireturn_serch(sql, 1);
		if (user_id == id) { sprintf(sql, "update DirMsg set dir_name = '%s' where dir_id = %d", dir_name, dir_id); My_DB->excute(sql); DB_Mutex->unlock(); delete sql; return true;
		}
		else { DB_Mutex->unlock(); strcpy(err, "Have no right!"); delete sql; return false;
		}
	}
	else {
		DB_Mutex->unlock();
		strcpy(err, "Have no right!");
		return false;
	}
}

char* DB_command::dir(int dir_id) {
	char* sql = new char[80];
	char* ret = new char[60];
	strcpy(sql, "select dir_name from DirMsg where dir_id = %d", dir_id);
	DB_Mutex->lock();
	ret = My_DB->creturn_serch(sql, 1);
	DB_Mutex->unlock();
	return ret;
}

char* DB_command::file(int file_id) {
	char* sql = new char[80];
	char* ret = new char[60];
	strcpy(sql, "select file_name from FileMsg where file_id = %d", file_id);
	DB_Mutex->lock();
	ret = My_DB->creturn_serch(sql, 1);
	DB_Mutex->unlock();
	return ret;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295
  • 296
  • 297
  • 298
  • 299
  • 300
  • 301
  • 302
  • 303
  • 304
  • 305
  • 306
  • 307
  • 308
  • 309
  • 310
  • 311
  • 312
  • 313
  • 314
  • 315
  • 316
  • 317
  • 318
  • 319
  • 320
  • 321
  • 322
  • 323
  • 324
  • 325
  • 326
  • 327
  • 328
  • 329
  • 330
  • 331
  • 332
  • 333
  • 334
  • 335
  • 336
  • 337
  • 338
  • 339
  • 340
  • 341
  • 342
  • 343
  • 344
  • 345
  • 346
  • 347
  • 348
  • 349
  • 350
  • 351
  • 352
  • 353
  • 354
  • 355
  • 356
  • 357
  • 358
  • 359
  • 360
  • 361
  • 362
  • 363
  • 364
  • 365
  • 366
  • 367
  • 368
  • 369
  • 370
  • 371
  • 372
  • 373
  • 374
  • 375
  • 376
  • 377
  • 378
  • 379
  • 380
  • 381
  • 382
  • 383
  • 384
  • 385
  • 386
  • 387
  • 388
  • 389
  • 390
  • 391
  • 392
  • 393
  • 394
  • 395
  • 396
  • 397
  • 398
  • 399
  • 400
  • 401
  • 402
  • 403
  • 404
  • 405
  • 406
  • 407
  • 408
  • 409
  • 410
  • 411
  • 412
  • 413
  • 414
  • 415
  • 416
  • 417
  • 418
  • 419
  • 420
  • 421
  • 422
  • 423
  • 424
  • 425
  • 426
  • 427
  • 428
  • 429
  • 430
  • 431
  • 432
  • 433
  • 434
  • 435
  • 436
  • 437
  • 438
  • 439
  • 440
  • 441
  • 442
  • 443
  • 444
  • 445
  • 446
  • 447
  • 448
  • 449
  • 450
  • 451
  • 452
  • 453
  • 454
  • 455
  • 456
  • 457
  • 458
  • 459
  • 460
  • 461
  • 462
  • 463
  • 464
  • 465
  • 466
  • 467
  • 468
  • 469
  • 470
  • 471
  • 472
  • 473
  • 474
  • 475
  • 476
  • 477
  • 478
  • 479
  • 480

接下来是为DB服务器与FTP服务器对接而设计的包

//Packet3.h

#pragma once

typedef struct CF {
	char cip[16];
	char filename[60];	
}cf_t;	//Client to FTP

typedef struct FC {
	char cip[16];
	char filename[60];	
}fc_t;	//FTO to Client

typedef struct DF {
	char filename[60];
}df_t;	//Delete File

typedef struct MF {
	char oldpath[60];
	char newpath[60];
}mf_t;	//Move File Path

typedef struct CFN {
	char oldname[60];
	char newname[60];
}cfn_t;	//Change File Name


//Ŀ¼²¿·Ö
typedef struct ND {
	char dirname[60];
}nd_t;	//New Direct 

typedef struct DD {
	char dirname[60];
}dd_t;	//Delete Direct

typedef struct MD {
	char olddir[60];
	char newdir[60];
}md_t;	//Move Direct

typedef struct CDN {
	char olddir[60];
	char newdir[60];
}cdn_t;	//Change Direct Name

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
//PacketCommand3.h
#pragma once
//用于DB服务器向FTP服务器发包
#include "PacketBase.h"
#include "Packet3.h"

class PacketCommand3 :
	public PacketBase
{
public:
	bool replyCF(char* filename,char* cip);
	bool replyFC(char* filename,char* cip);
	bool replyDF(char* filename);
	bool replyMF(char* oldpath, char* newpath);
	bool replyCFN(char* oldpath, char* newpath);
	bool replyND(char* diename);
	bool replyDD(char* dirname);
	bool replyMD(char* oldpath, char* newpath);
	bool replyCDN(char* oldpath, char* newpath);
};

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
//PacketCommand3.cpp

#include "PacketCommand3.h"

bool PacketCommand3::replyCF(char* filename, char* cip) { int sz = sizeof(cf_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x12; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); cf_t* body = (cf_t*)Body; strcpy(body->filename, filename); strcpy(body->cip, cip); //到时候大不了转回string Tail.pack_tail = 0x05; return this->pack();
}

bool PacketCommand3::replyFC(char* filename, char* cip) { int sz = sizeof(fc_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x13; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); fc_t* body = (fc_t*)Body; strcpy(body->filename, filename); strcpy(body->cip, cip); //到时候大不了转回string Tail.pack_tail = 0x05; return this->pack();
}

bool PacketCommand3::replyDF(char* filename) { int sz = sizeof(df_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x17; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); df_t* body = (df_t*)Body; strcpy(body->filename, filename); Tail.pack_tail = 0x05; return this->pack();
}
bool PacketCommand3::replyMF(char* oldpath, char* newpath) { int sz = sizeof(mf_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x18; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); mf_t* body = (mf_t*)Body; strcpy(body->oldpath, oldpath); strcpy(body->newpath, newpath); Tail.pack_tail = 0x05; return this->pack();
}

bool PacketCommand3::replyCFN(char* oldpath, char* newpath) { int sz = sizeof(cfn_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x20; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); cfn_t* body = (cfn_t*)Body; strcpy(body->oldname, oldpath); strcpy(body->oldname, newpath); Tail.pack_tail = 0x05; return this->pack();
}

bool PacketCommand3::replyND(char* diename) { int sz = sizeof(nd_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x15; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); nd_t* body = (nd_t*)Body; strcpy(body->dirname, diename); Tail.pack_tail = 0x05; return this->pack();
}

bool PacketCommand3::replyDD(char* dirname) { int sz = sizeof(dd_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x16; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); dd_t* body = (dd_t*)Body; strcpy(body->dirname, dirname); Tail.pack_tail = 0x05; return this->pack();
}

bool PacketCommand3::replyMD(char* oldpath, char* newpath) { int sz = sizeof(md_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x19; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); md_t* body = (md_t*)Body; strcpy(body->olddir, oldpath); strcpy(body->newdir, newpath); Tail.pack_tail = 0x05; return this->pack();
}
bool PacketCommand3::replyCDN(char* oldpath, char* newpath) { int sz = sizeof(cdn_t); this->setBodySize(sz); this->Body = new char[sz]; Head.funcId = 0x22; Head.optid = 0x01; Head.usrlenth = sz; Head.syn = 0x04; strcpy(Head.to_fd, "FTP"); cdn_t* body = (cdn_t*)Body; strcpy(body->olddir, oldpath); strcpy(body->newdir, newpath); Tail.pack_tail = 0x05; return this->pack();
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178

接下来是中间操控的主类:

//BackServer.h
#pragma once

#include"BtoC.h"
#include"Pthread_Pool.h"
#include"PacketCommand1.h"
#include"PacketCommand3.h"
#include"DB_command.h"

#include<stdlib.h>
#include<string.h>
#include <stdio.h>
#include <iostream>
using namespace std;

class DoTask : public Task
{
public: DoTask(BtoC& send, PacketCommand1& packet); int run();
private: DB_command* task_db; BtoC* m_send; PacketCommand1 m_packet; PacketCommand3* f_packet;
};


class BackServer
{
public: BackServer(char* IPnum); ~BackServer() {} int run();
private: PacketCommand1 m_packet; BtoC m_send; Pthread_Pool* m_pool;
};

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
//BackServer.cpp
#include "BackServer.h"

BackServer::BackServer(char* IPnum)
{ m_pool = new Pthread_Pool(50, 30, 5); m_send.Start(IPnum); char* name = new char[6]; strcpy(name, "DB"); m_send.send_name(name); delete name;
}

int BackServer::run()
{ int n = 0; while (1) { n = m_send.Read_date(m_packet.getData()); m_packet.setSize(n); DoTask* t = new DoTask(m_send, m_packet); m_pool->addTask(t); } return 0;
}

DoTask::DoTask(BtoC& send, PacketCommand1& packet) :m_send(&send), m_packet(packet)
{ task_db = new DB_command(); f_packet = new PacketCommand3();
}

int DoTask::run()
{ printf("task get data \n"); int tru = 0; //1. unpack m_packet.unpack(); //2. deal the packet by funid void* pData = m_packet.getBody(); char* err = new char[40]; switch (m_packet.getHead()->funcId) { case 0x01://登陆 { err = new char[40]; Login_t* plogin = (Login_t*)(pData); tru = task_db->login(plogin->id, plogin->pwd,err); m_packet.replyLogin(tru, tru,err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); }break; case 0x02://注册 { Register_t* pregister = (Register_t*)(pData); tru = task_db->add_user(pregister->id, pregister->pwd, pregister->tel,err); m_packet.replyRegist(tru,err,m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); }break; case 0x03://找回密码 { F_Pwd_t* Fpwd = (F_Pwd_t*)(pData); tru = task_db->pwd(Fpwd->id, Fpwd->tel); m_packet.replyFpwd(tru, tru, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } case 0x04://修改密码 { c_pwd_t* pc_pwd = (c_pwd_t*)(pData); tru = task_db->cpwd(pc_pwd->id, pc_pwd->pwd, pc_pwd->newpwd,err); m_packet.replyCpwd(tru,err,m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); }break; case 0x11:  //获取文件列表 { FileList_t* pfl = (FileList_t*)(pData); char* getList = new char[800]; task_db->file_list(pfl->user_id, pfl->pwd, pfl->dir_id, getList, err); m_packet.replyFileList(err, getList, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); }break; case 0x12:  //上传文件 { UpFile_t* pul = (UpFile_t*)(pData); tru = task_db->add_file(pul->file_name, pul->user_id, pul->pwd, pul->mode, pul->dir_id, pul->force, err); //不论成功与否,客户端都应该知道 m_packet.replyAddList(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); //如果成功,那就发往FTP处理 if (true == tru) { f_packet->replyCF(pul->file_name, pul->ip); m_send->Write_date(f_packet->getData(),f_packet->getSize()); } }break; case 0x13:  //下载文件 { Download_t* pdf = (Download_t*)(pData); tru = task_db->download(pdf->user_id, pdf->pwd, pdf->file_name, pdf->user_id, pdf->shared, err); //不论成功与否,客户端都应该知道 m_packet.replyDownFile(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); //如果成功,那就发往FTP处理 if (true == tru){ f_packet->replyFC(pdf->file_name, pdf->ip); m_send->Write_date(f_packet->getData(), f_packet->getSize()); } }break; case 0x14:  //共享文件 { C_mode_t* pcm = (C_mode_t*)(pData); tru = task_db->share_file(pcm->id, pcm->pwd, pcm->share_num, pcm->file_name, pcm->dir_id, err); m_packet.replyShareFile(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); }break; case 0x15:  //新增目录 { A_filelist_t* pafl = (A_filelist_t*)(pData); tru = task_db->add_list(pafl->id, pafl->pwd, pafl->father_dir, pafl->dirname, err); if (true == tru) { f_packet->replyND(pafl->dirname); m_send->Write_date(f_packet->getData(), f_packet->getSize()); } else { m_packet.replyAddList(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } }break; case 0x16:  //删除目录 { D_list_t* pdl = (D_list_t*)(pData); tru = task_db->del_list(pdl->id, pdl->pwd, pdl->dir_id, err); if (true == tru) { char* name = new char[60]; name = task_db->dir(pdl->dir_id); f_packet->replyDD(name); m_send->Write_date(f_packet->getData(), f_packet->getSize()); } else { m_packet.replyDelDir(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } }break; case 0x17:  //删除文件 { D_file_t* pdfl = (D_file_t*)(pData); tru = task_db->del_file(pdfl->id, pdfl->pwd, pdfl->file_name, pdfl->dir_id, err); if (true == tru) { f_packet->replyDF(pdfl->file_name); m_send->Write_date(f_packet->getData(), f_packet->getSize()); } else { m_packet.replyDelFile(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } }break; case 0x18:  //文件移动 { C_filelist_t* pcf = (C_filelist_t*)(pData); tru = task_db->cfile_addr(pcf->id, pcf->pwd, pcf->file_name,pcf->old_dir,pcf->new_dir,err); if (true == tru) { char* oldname = new char[60]; char* newname = new char[60]; oldname = task_db->dir(pcf->old_dir); newname = task_db->dir(pcf->new_dir); f_packet->replyMF(oldname,newname);   //这里,需要有id向名字转换的数据库操作函数 m_send->Write_date(f_packet->getData(), f_packet->getSize()); } else { m_packet.replyMoveFile(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } }break; case 0x19:  //目录移动 { C_Dirlist_t* pcl = (C_Dirlist_t*)(pData); tru = task_db->cdir_addr(pcl->id, pcl->pwd,pcl->dir_name,pcl->new_father, err); if (true == tru) { char* name = new char[60]; name = task_db->dir(pcl->new_father); f_packet->replyMD(pcl->dir_name,name);   //这里 m_send->Write_date(f_packet->getData(), f_packet->getSize()); } else { m_packet.replyMoveDir(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } }break; case 0x20:  //文件重命名 { C_fileName_t* pfn = (C_fileName_t*)(pData); tru = task_db->cfile_name(pfn->user_id, pfn->pwd, pfn->filename, pfn->file_id,pfn->dir_id,err); if (true == tru) { char* name = new char[60]; name = task_db->file(pfn->file_id); f_packet->replyCFN(name,pfn->filename);   //这里 m_send->Write_date(f_packet->getData(), f_packet->getSize()); } else { m_packet.replyChangeFileName(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } }break; case 0x22:  //目录重命名 { C_dirName_t* pdn = (C_dirName_t*)(pData); tru = task_db->cdir_name(pdn->user_id, pdn->pwd, pdn->dirname, pdn->dir_id,err); if (true == tru) { char* name = new char[60]; name = task_db->dir(pdn->dir_id); f_packet->replyCDN(name, pdn->dirname);   //这里 m_send->Write_date(f_packet->getData(), f_packet->getSize()); } else { m_packet.replyChangeDirName(tru, err, m_packet.getHead()->fd); m_send->Write_date(m_packet.getData(), m_packet.getSize()); } }break; return 0; }

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259

场景类相对来说就很简单

//BackCommand.cpp
#include"BackServer.h"

int main(int argc, char* argv[]) {
	BackServer* bk = new BackServer(argv[1]);
	bk->run();
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

文章来源: lion-wu.blog.csdn.net,作者:看,未来,版权归原作者所有,如需转载,请联系作者。

原文链接:lion-wu.blog.csdn.net/article/details/107022234

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。