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
| #include"HandleServerV2.h" #include"global.h"
extern unordered_map<string,int> name_sock_map; extern unordered_map<int,set<int>> group_map; extern unordered_map<string,string> from_to_map;
extern int total_handle; extern int total_recv_request; extern double top_speed; extern int Bloom_Filter_bitmap[1000000]; extern queue<int> mission_queue; extern int mission_num; extern pthread_cond_t mission_cond; extern pthread_spinlock_t name_mutex; extern pthread_spinlock_t group_mutex; extern pthread_spinlock_t from_mutex; extern pthread_mutex_t queue_mutex; extern int epollfd; extern pthread_spinlock_t count_mutex;
void* handle_all_request(void *arg){ while(1){ int conn_num; pthread_mutex_lock(&queue_mutex); while(mission_queue.empty()){ pthread_cond_wait(&mission_cond,&queue_mutex); } conn_num=mission_queue.front(); mission_queue.pop(); pthread_mutex_unlock(&queue_mutex);
time_point<system_clock> begin_clock= system_clock::now(); int conn=conn_num; int target_conn=-1; char buffer[1000]; string name,pass; bool if_login=false; string login_name; string target_name; int group_num; MYSQL *con=mysql_init(NULL); mysql_real_connect(con,"127.0.0.1","root","","test_connect",0,NULL,CLIENT_MULTI_STATEMENTS); redisContext *redis_target = redisConnect("127.0.0.1",6379); if(redis_target->err){ redisFree(redis_target); cout<<"连接redis失败"<<endl; }
cout<<"-----------------------------\n"; string recv_str; while(1){ char buf[10]; memset(buf, 0, sizeof(buf)); int ret = recv(conn, buf, sizeof(buf), 0); if(ret < 0){ cout<<"recv返回值小于0"<<endl; if((errno == EAGAIN) || (errno == EWOULDBLOCK)){ printf("数据读取完毕\n"); cout<<"接收到的完整内容为:"<<recv_str<<endl; cout<<"开始处理事件"<<endl; break; } cout<<"errno:"<<errno<<endl; close(conn); mysql_close(con); if(!redis_target->err) redisFree(redis_target); return NULL; } else if(ret == 0){ cout<<"recv返回值为0"<<endl; close(conn); mysql_close(con); if(!redis_target->err) redisFree(redis_target); return NULL; } else{ printf("接收到内容如下: %s\n",buf); string tmp(buf); recv_str+=tmp; } } string str=recv_str;
if(str.find("cookie:")!=str.npos){ string cookie=str.substr(7); string redis_str="hget "+cookie+" name"; redisReply *r = (redisReply*)redisCommand(redis_target,redis_str.c_str()); string send_res; if(r->str){ cout<<"查询redis结果:"<<r->str<<endl; send_res=r->str; pthread_spin_lock(&name_mutex); name_sock_map[send_res]=conn; pthread_spin_unlock(&name_mutex); } else send_res="NULL"; send(conn,send_res.c_str(),send_res.length()+1,0); }
else if(str.find("login")!=str.npos){ int p1=str.find("login"),p2=str.find("pass:"),flag=0; name=str.substr(p1+5,p2-5); pass=str.substr(p2+5,str.length()-p2-4);
int hash=0; for(auto ch:name){ hash=hash*131+ch; if(hash>=10000000) hash%=10000000; } int index=hash/32,pos=hash%32; if((Bloom_Filter_bitmap[index]&(1<<pos))==0){ cout<<"布隆过滤器查询为0,登录用户名必然不存在数据库中\n"; char str1[100]="wrong"; send(conn,str1,strlen(str1),0); flag=1; } if(flag==0){ string search="SELECT * FROM user WHERE NAME=\""; search+=name; search+="\";"; cout<<"sql语句:"<<search<<endl; auto search_res=mysql_query(con,search.c_str()); auto result=mysql_store_result(con); int row; if(result) row=mysql_num_rows(result); if(search_res==0&&row!=0){ cout<<"查询成功\n"; auto info=mysql_fetch_row(result); cout<<"查询到用户名:"<<info[0]<<" 密码:"<<info[1]<<endl; if(info[1]==pass){ cout<<"登录密码正确\n"; string str1="ok"; if_login=true; login_name=name; pthread_spin_lock(&name_mutex); name_sock_map[name]=conn; pthread_spin_unlock(&name_mutex);
srand(time(NULL)); for(int i=0;i<10;i++){ int type=rand()%3; if(type==0) str1+='0'+rand()%9; else if(type==1) str1+='a'+rand()%26; else if(type==2) str1+='A'+rand()%26; } string redis_str="hset "+str1.substr(2)+" name "+login_name; redisReply *r = (redisReply*)redisCommand(redis_target,redis_str.c_str()); redis_str="expire "+str1.substr(2)+" 300"; r=(redisReply*)redisCommand(redis_target,redis_str.c_str());
cout<<"随机生成的sessionid为:"<<str1.substr(2)<<endl;
send(conn,str1.c_str(),str1.length()+1,0); } else{ cout<<"登录密码错误\n"; char str1[100]="wrong"; send(conn,str1,strlen(str1),0); } } else{ cout<<"查询失败\n"; char str1[100]="wrong"; send(conn,str1,strlen(str1),0); } } }
else if(str.find("name:")!=str.npos){ int p1=str.find("name:"),p2=str.find("pass:"); name=str.substr(p1+5,p2-5); pass=str.substr(p2+5,str.length()-p2-4); string search="INSERT INTO user VALUES (\""; search+=name; search+="\",\""; search+=pass; search+="\");"; cout<<endl<<"sql语句:"<<search<<endl; mysql_query(con,search.c_str()); } else if(str.find("target:")!=str.npos){ int pos1=str.find("from"); string target=str.substr(7,pos1-7),from=str.substr(pos1+5); target_name=target; if(name_sock_map.find(target)==name_sock_map.end()) cout<<"源用户为"<<from<<",目标用户"<<target_name<<"仍未登陆,无法发起私聊\n"; else{ pthread_spin_lock(&from_mutex); from_to_map[from]=target; pthread_spin_unlock(&from_mutex); login_name=from; cout<<"源用户"<<login_name<<"向目标用户"<<target_name<<"发起的私聊即将建立"; cout<<",目标用户的套接字描述符为"<<name_sock_map[target]<<endl; target_conn=name_sock_map[target]; } }
else if(str.find("content:")!=str.npos){ for(auto i:name_sock_map){ if(i.second==conn){ login_name=i.first; target_name=from_to_map[i.first]; target_conn=name_sock_map[target_name]; break; } } if(target_conn==-1){ cout<<"找不到目标用户"<<target_name<<"的套接字,将尝试重新寻找目标用户的套接字\n"; if(name_sock_map.find(target_name)!=name_sock_map.end()){ target_conn=name_sock_map[target_name]; cout<<"重新查找目标用户套接字成功\n"; } else{ cout<<"查找仍然失败,转发失败!\n"; } } string recv_str(str); string send_str=recv_str.substr(8); cout<<"用户"<<login_name<<"向"<<target_name<<"发送:"<<send_str<<endl; send_str="["+login_name+"]:"+send_str; send(target_conn,send_str.c_str(),send_str.length(),0); } else if(str.find("group:")!=str.npos){ string recv_str(str); string num_str=recv_str.substr(6); group_num=stoi(num_str); for(auto i:name_sock_map) if(i.second==conn){ login_name=i.first; break; } cout<<"用户"<<login_name<<"绑定群聊号为:"<<num_str<<endl; pthread_spin_lock(&group_mutex); group_map[group_num].insert(conn); pthread_spin_unlock(&group_mutex); }
else if(str.find("gr_message:")!=str.npos){ for(auto i:name_sock_map) if(i.second==conn){ login_name=i.first; break; } for(auto i:group_map) if(i.second.find(conn)!=i.second.end()){ group_num=i.first; break; } string send_str(str); send_str=send_str.substr(11); send_str="["+login_name+"]:"+send_str; cout<<"群聊信息:"<<send_str<<endl; for(auto i:group_map[group_num]){ if(i!=conn) send(i,send_str.c_str(),send_str.length(),0); } } cout<<"---------------------"<<endl;
epoll_event event; event.data.fd=conn; event.events=EPOLLIN|EPOLLET|EPOLLONESHOT; epoll_ctl(epollfd,EPOLL_CTL_MOD,conn,&event); mysql_close(con); if(!redis_target->err) redisFree(redis_target);
auto end_clock = system_clock::now(); auto duration = duration_cast<microseconds>(end_clock - begin_clock); pthread_spin_lock(&count_mutex); total_time+=double(duration.count()) * microseconds::period::num / microseconds::period::den; total_handle++; pthread_spin_unlock(&count_mutex); double now_rate=total_handle/total_time; if(now_rate>top_speed) top_speed=now_rate; cout<<"已用时"<<total_time<<"秒,"; cout<<"共收到"<<total_recv_request<<"个请求,"; cout<<"已处理"<<total_handle<<"个请求\n"; cout<<"处理一个请求平均需要"<<total_time/total_handle<<"秒,"; cout<<"平均一秒处理"<<now_rate<<"个请求\n"; cout<<"峰值性能为一秒处理"<<top_speed<<"个请求"; cout<<"---------------------------------\n"; } }
|