-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatingServer.cpp
More file actions
135 lines (115 loc) · 4.48 KB
/
Copy pathChatingServer.cpp
File metadata and controls
135 lines (115 loc) · 4.48 KB
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
#include "TcpSocket.h"
#include "UserManagement.h"
#include <mysql++/mysql++.h>
#include <mysql++/result.h>
#include <signal.h>
#include <vector>
#include <thread>
#include <fstream>
using namespace std;
const int key=23;
void childHandler(int tmp)
{
wait(0);
}
void writeLog(string param,string logFile="../chatinglog.txt")
{
ofstream log(logFile,ios::app);
time_t nowTime=time(NULL);
string timeString(ctime(&nowTime));
log<<timeString.substr(0,timeString.length()-1)<<" : "<<param<<std::endl;
log.close();
}
int main(int argc,char** argv) //
{
signal(SIGCHLD,childHandler);
try
{
ServerTcpSocket server(9999);
while(server.accept()>0)
{
pid_t pid;
if((pid=fork())==0)
{
cout<<"connectRoom!"<<endl;
string userID=server.receive(key);
int roomNumber=atoi(server.receive(key).c_str());
writeLog("connect room("+to_string(roomNumber)+") ID("+userID+")");
mysqlpp::Connection con("chatroom","10.156.145.48","root","shangus1",3306);
mysqlpp::Query query=con.query();
mysqlpp::StoreQueryResult result;
query<<"select * from chatserver.roomnumber where number='"+to_string(roomNumber)+"'";
result=query.store();
vector<string> array=UserManagement::list2array(string(result.at(0)["memberlist"]));
bool check=false;
for(string tmp:array)
if(tmp==userID)
{
check=true;
break;
}
if(check==false)
exit(0);
writeLog("connect success room("+to_string(roomNumber)+") ID("+userID+")");
thread recv([&](){
while(1)
{
string msg=server.receive(key);
writeLog("send room("+to_string(roomNumber)+") ID("+userID+")");
query<<"insert into room"+to_string(roomNumber)+"(sender,msg) values('"+userID+"','"+msg+"')";
query.store();
query<<"update chatserver.roomnumber set nowmsg=nowmsg+1 where number="+to_string(roomNumber)+"";
query.store();
}
});
thread send([&](){
int nowmsg;
query<<"select nowmsg from chatserver.roomnumber where number='"+to_string(roomNumber)+"'";
result=query.store();
nowmsg=result.at(0)["nowmsg"];
query<<"select * from room"+to_string(roomNumber);
result=query.store();
for(int i=0;i<result.num_rows();i++)
{
server.send(string(result.at(i)["date"]),key);
server.send(string(result.at(i)["sender"]),key);
server.send(string(result.at(i)["msg"]),key);
}
while(1)
{
query<<"select nowmsg from chatserver.roomnumber where number='"+to_string(roomNumber)+"'";
result=query.store();
if(nowmsg!=atoi(result.at(0)["nowmsg"]))
{
writeLog("receive room("+to_string(roomNumber)+") ID("+userID+")");
nowmsg++;
query<<"select * from room"+to_string(roomNumber)+" where number='"+to_string(nowmsg)+"'";
result=query.store();
if(result.at(0)["sender"]!=userID)
{
server.send(string(result.at(0)["date"]),key);
server.send(string(result.at(0)["sender"]),key);
server.send(string(result.at(0)["msg"]),key);
}
}
sleep(0.1);
}
});
send.join();
recv.join();
}
}
}
catch (mysqlpp::BadQuery& e)
{
cerr << "BadQuery : " << e.what() << endl;
cerr<<e.errnum()<<endl;
cout<<"room"<<endl;
}
catch(const char* e)
{
perror(e);
cout<<"room"<<endl;
}
return 0;
}