加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
threadPool.c 8.49 KB
一键复制 编辑 原始数据 按行查看 历史
SpaceX_zhao 提交于 2023-05-18 22:23 . alopex modify
#define _XOPEN_SOURCE 600
#define STR_LEN 10
#include "threadPool.h"
void *threadFunc(void* arg);
int epollAdd(int epfd, int fd);
int epollDel(int epfd, int fd);
char *GenerateStr();
int disconnectCheck(int ret,int netfd);
int checkusr(const char* usrName,const char* password, MYSQL* usrSql);
int insertUsr(const char* usrName,const char* password,MYSQL* usrSql);
int getUsrNameAndPassword(int netfd,train_t* usrName, train_t* usrPassword);
// 将文件描述符添加到epoll池中
int epollAdd(int epfd, int fd){
struct epoll_event event;
event.events = EPOLLIN;
event.data.fd = fd;
int ret = epoll_ctl(epfd,EPOLL_CTL_ADD,fd,&event);
ERROR_CHECK(ret,-1,"epollAdd");
return 0;
}
// 将文件描述符从epoll池中删除
int epollDel(int epfd, int fd){
int ret = epoll_ctl(epfd,EPOLL_CTL_DEL,fd,NULL);
ERROR_CHECK(ret,-1,"epollDel");
return 0;
}
// 生成盐值
char *GenerateStr()
{
char *str = (char *)calloc(1,STR_LEN+1);
bzero(str,STR_LEN+1);
int i,flag;
srand(time(NULL));//通过时间函数设置随机数种子,使得每次运行结果随机。
for(i = 0; i < STR_LEN; i ++)
{
flag = rand()%3;
switch(flag)
{
case 0:
str[i] = rand()%26 + 'a';
break;
case 1:
str[i] = rand()%26 + 'A';
break;
case 2:
str[i] = rand()%10 + '0';
break;
}
}
return str;
}
// 检查用户连接是否断开,断开则关闭netfd
int disconnectCheck(int ret,int netfd){
if(ret == 0){
close(netfd);
}
return ret;
}
// 在表中检查用户是否存在
int checkusr(const char* usrName,const char* password, MYSQL* usrSql){
// 从数据库找到对应的盐值
char sqlCommand[1024];
bzero(sqlCommand,sizeof(sqlCommand));
sprintf(sqlCommand,"select salt, code from password where usrname = '%s'",usrName);
int ret = mysql_query(usrSql, sqlCommand);
MYSQL_RES *result = mysql_store_result(usrSql);
MYSQL_ROW row = mysql_fetch_row(result);
//mysql_free_result(result);
LOGRECORD(INFO,"mysql_fetch_row");
if(row == NULL){
// 服务端用户表中没有此用户,就注册
ret = insertUsr(usrName,password,usrSql);
LOGRECORD(INFO,"usr not exist, insertUsr finish");
return ret ;
}
// 从服务端的password表中获取盐值
LOGRECORD(INFO,"get salt from password");
char salt[20] = {0};
strcpy(salt,row[0]);
// 从服务端的password表中获取密码
char storecode[41] = {0};
strcpy(storecode,row[1]);
// 本地加密密码
char code[128] = {0};
strcpy(code,crypt(password,salt));
// 比较本地 和 云端加密密码
ret = strcmp(code,storecode);
LOGRECORD(INFO,"check finish");
mysql_free_result(result);
return ret;
}
// 将用户名 和 密码插入密码表
int insertUsr(const char* usrName,const char* password,MYSQL* usrSql){
char sqlCommand[1024] = {0};
char* salt = GenerateStr();
printf("%s\n",salt);
char code[40] = {0};
LOGRECORD(INFO,"generateStr finish");
char* encode = crypt(password, salt);
LOGRECORD(INFO,"crypt");
strcpy(code,encode);
printf("%s\n",code);
LOGRECORD(INFO,"strcpy");
sprintf(sqlCommand,"insert into password (usrname,salt,code) VALUES ('%s','%s','%s')",
usrName,salt,code);
int ret = mysql_query(usrSql, sqlCommand);
LOGRECORD(INFO,"insertUsr finish");
bzero(sqlCommand,sizeof(sqlCommand));
sprintf(sqlCommand,"insert into vfsystem (name,type,preid,path,user,tomb,md5)"
" VALUES ('/','d',-1,'/','%s',0,'NULL')",
usrName);
ret = mysql_query(usrSql, sqlCommand);
LOGRECORD(INFO,"insert vfsystem finish");
return ret;
}
int getUsrNameAndPassword(int netfd, train_t* usrName, train_t* usrPassword){
// 接收用户名
bzero(usrName,sizeof(train_t));
//int ret = recvn(netfd, &usrName->length,sizeof(int));
int ret = recv(netfd, &usrName->length, sizeof(int),MSG_WAITALL);
printf("usrName->length = %d\n", usrName->length);
if(ret == 0){
LOGRECORD(ERROR,"client close !");
goto end;
}
//ret = recvn(netfd, usrName->data, usrName->length);
ret = recv(netfd, usrName->data, usrName->length,MSG_WAITALL);
printf("usrName->data=%s\n", usrName->data);
if(ret == 0){
LOGRECORD(ERROR,"client close !");
goto end;
}
// 接受用户密码
bzero(usrPassword,sizeof(train_t));
ret = recv(netfd, &usrPassword->length, sizeof(int),MSG_WAITALL);
printf("usrPassword->length = %d\n", usrPassword->length);
if(ret == 0){
LOGRECORD(ERROR,"client close !");
goto end;
}
ret = recv(netfd, usrPassword->data, usrPassword->length,MSG_WAITALL);
printf("usrPassword->data=%s\n", usrPassword->data);
if(ret == 0){
LOGRECORD(ERROR,"client close !");
goto end;
}
end:
return ret;
}
void *threadFunc(void* arg){
// 接收传进来的threadPool
threadPool_t *pthreadPool = (threadPool_t *)arg;
while(1){
// 加锁
pthread_mutex_lock(&pthreadPool->mutex);
while(pthreadPool->taskQueue.queueSize == 0){
// 没有就绪任务则陷入等待
pthread_cond_wait(&pthreadPool->cond,&pthreadPool->mutex);
}
LOGRECORD(INFO,"childthread gets a task !!");
int netfd = pthreadPool->taskQueue.pfront->fd;
Dequeue(&pthreadPool->taskQueue);
pthreadPool->taskQueue.queueSize--;
pthread_mutex_unlock(&pthreadPool->mutex);
// 记录用户的信息
userProcess_t usr;
bzero(&usr,sizeof(usr));
MYSQL *usrsql = mysql_init(NULL);
MYSQL *pret = mysql_real_connect(usrsql, "localhost", "root", "root", "disk", 0, NULL, 0);
LOGRECORD(INFO,"mysql_real_connect");
if(pret == NULL){
LOGRECORD(ERROR,"mysql disconnect !!");
continue;
}
train_t usrName;
train_t usrPassword;
while(1){
// 接收用户名
int flag = 0;
while(!flag){
bzero(&usrName,sizeof(usrName));
bzero(&usrPassword,sizeof(usrPassword));
flag = getUsrNameAndPassword(netfd, &usrName, &usrPassword);
LOGRECORD(INFO,"getUsrNameAndPassword");
if(flag == 0){
close(netfd);
goto finish;
}
LOGRECORD(INFO,"flag");
}
LOGRECORD(INFO,"getusrname and password finish");
// 检查该用户名 和 密码 是否正确,是否存在
int retcheck = checkusr(usrName.data, usrPassword.data, usrsql);
LOGRECORD(INFO,"checkusr");
if(retcheck == 0){
send(netfd, &retcheck,sizeof(int),MSG_NOSIGNAL);
LOGRECORD(INFO,"check success ,send finish");
break;
}else{
retcheck = -1;
send(netfd, &retcheck, sizeof(int),MSG_NOSIGNAL);
LOGRECORD(INFO,"check failed ,send finish");
}
}
strncpy(usr.username, usrName.data, strlen(usrName.data));
// 将usr表中用户id,复制到usr结构体中,方便使用
char sqlCommand[1024] = {0};
sprintf(sqlCommand,"select id from vfsystem where user = '%s' AND path = '/'",usr.username);
int ret = mysql_query(usrsql, sqlCommand);
if(ret == 0){
LOGRECORD(INFO,"select id success");
}
MYSQL_RES *result = mysql_store_result(usrsql);
if(result == NULL){
LOGRECORD(ERROR,"result is NULL");
}
MYSQL_ROW row = mysql_fetch_row(result);
if(row == NULL){
LOGRECORD(ERROR,"row is NULL");
}
LOGRECORD(ERROR,"copy initid");
int id = atoi(row[0]);
memcpy(&usr.initid,&id,sizeof(int));
//printf("initid :%d \n",usr.initid);
memcpy(&usr.curid,&usr.initid,sizeof(int));
LOGRECORD(INFO,"usr initiaze finish!");
mysql_free_result(result);
while(1){
int ret = execCommand(netfd,&usr,usrsql);
if(ret == -1){
break;
}
}
finish:
LOGRECORD(INFO,"finish");
continue;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化