-
Notifications
You must be signed in to change notification settings - Fork 17
/
userdeal.cpp
289 lines (258 loc) · 8.96 KB
/
userdeal.cpp
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
#include "unpthread.h"
#include <stdlib.h>
#include "unp.h"
#include "netheader.h"
#include "msg.h"
pthread_mutex_t mlock = PTHREAD_MUTEX_INITIALIZER; // accept lock
extern socklen_t addrlen;
extern int listenfd;
extern int nprocesses;
extern Room *room;
void* thread_main(void *arg)
{
void dowithuser(int connfd);
int i = *(int *)arg;
free(arg); //free
int connfd;
Pthread_detach(pthread_self()); //detach child thread
// printf("thread %d starting\n", i);
SA *cliaddr;
socklen_t clilen;
cliaddr = (SA *)Calloc(1, addrlen);
char buf[MAXSOCKADDR];
for(;;)
{
clilen = addrlen;
//lock accept
Pthread_mutex_lock(&mlock);
connfd = Accept(listenfd, cliaddr, &clilen);
//unlock accept
Pthread_mutex_unlock(&mlock);
printf("connection from %s\n", Sock_ntop(buf, MAXSOCKADDR, cliaddr, clilen));
dowithuser(connfd); // process user
}
return NULL;
}
/*
*
*read data from client
*
*/
void dowithuser(int connfd)
{
void writetofd(int fd, MSG msg);
char head[15] = {0};
//read head
while(1)
{
ssize_t ret = Readn(connfd, head, 11);
if(ret <= 0)
{
close(connfd); //close
printf("%d close\n", connfd);
return;
}
else if(ret < 11)
{
printf("data len too short\n");
}
else if(head[0] != '$')
{
printf("data format error\n");
}
else
{
//solve datatype
MSG_TYPE msgtype;
memcpy(&msgtype, head + 1, 2);
msgtype = (MSG_TYPE)ntohs(msgtype);
//solve ip
uint32_t ip;
memcpy(&ip, head + 3, 4);
ip = ntohl(ip);
//solve datasize
uint32_t datasize;
memcpy(&datasize, head + 7, 4);
datasize = ntohl(datasize);
// printf("msg type %d\n", msgtype);
if(msgtype == CREATE_MEETING)
{
char tail;
Readn(connfd, &tail, 1);
//read data from client
if(datasize == 0 && tail == '#')
{
char *c = (char *)&ip;
printf("create meeting ip: %d.%d.%d.%d\n", (u_char)c[3], (u_char)c[2], (u_char)c[1], (u_char)c[0]);
if(room->navail <=0) // no room
{
MSG msg;
memset(&msg, 0, sizeof(msg));
msg.msgType = CREATE_MEETING_RESPONSE;
int roomNo = 0;
msg.ptr = (char *) malloc(sizeof(int));
memcpy(msg.ptr, &roomNo, sizeof(int));
msg.len = sizeof(roomNo);
writetofd(connfd, msg);
}
else
{
int i;
//find room empty
Pthread_mutex_lock(&room->lock);
for(i = 0; i < nprocesses; i++)
{
if(room->pptr[i].child_status == 0) break;
}
if(i == nprocesses) //no room empty
{
MSG msg;
memset(&msg, 0, sizeof(msg));
msg.msgType = CREATE_MEETING_RESPONSE;
int roomNo = 0;
msg.ptr = (char *) malloc(sizeof(int));
memcpy(msg.ptr, &roomNo, sizeof(int));
msg.len = sizeof(roomNo);
writetofd(connfd, msg);
}
else
{
char cmd = 'C';
if(write_fd(room->pptr[i].child_pipefd, &cmd, 1, connfd) < 0)
{
printf("write fd error");
}
else
{
close(connfd);
printf("room %d empty\n", room->pptr[i].child_pid);
room->pptr[i].child_status = 1; // occupy
room->navail--;
room->pptr[i].total++;
Pthread_mutex_unlock(&room->lock);
return;
}
}
Pthread_mutex_unlock(&room->lock);
}
}
else
{
printf("1 data format error\n");
}
}
else if(msgtype == JOIN_MEETING)
{
//read msgsize
uint32_t msgsize, roomno;
memcpy(&msgsize, head + 7, 4);
msgsize = ntohl(msgsize);
//read data + #
int r = Readn(connfd, head, msgsize + 1 );
if(r < msgsize + 1)
{
printf("data too short\n");
}
else
{
if(head[msgsize] == '#')
{
memcpy(&roomno, head, msgsize);
roomno = ntohl(roomno);
// printf("room : %d\n", roomno);
//find room no
bool ok = false;
int i;
for(i = 0; i < nprocesses; i++)
{
if(room->pptr[i].child_pid == roomno && room->pptr[i].child_status == 1)
{
ok = true; //find room
break;
}
}
MSG msg;
memset(&msg, 0, sizeof(msg));
msg.msgType = JOIN_MEETING_RESPONSE;
msg.len = sizeof(uint32_t);
if(ok)
{
if(room->pptr[i].total >= 1024)
{
msg.ptr = (char *)malloc(msg.len);
uint32_t full = -1;
memcpy(msg.ptr, &full, sizeof(uint32_t));
writetofd(connfd, msg);
}
else
{
Pthread_mutex_lock(&room->lock);
char cmd = 'J';
// printf("i = %d\n", i);
if(write_fd(room->pptr[i].child_pipefd, &cmd, 1, connfd) < 0)
{
err_msg("write fd:");
}
else
{
msg.ptr = (char *)malloc(msg.len);
memcpy(msg.ptr, &roomno, sizeof(uint32_t));
writetofd(connfd, msg);
room->pptr[i].total++;// add 1
Pthread_mutex_unlock(&room->lock);
close(connfd);
return;
}
Pthread_mutex_unlock(&room->lock);
}
}
else
{
msg.ptr = (char *)malloc(msg.len);
uint32_t fail = 0;
memcpy(msg.ptr, &fail, sizeof(uint32_t));
writetofd(connfd, msg);
}
}
else
{
printf("format error\n");
}
}
}
else
{
printf("data format error\n");
}
}
}
}
void writetofd(int fd, MSG msg)
{
char *buf = (char *) malloc(100);
memset(buf, 0, 100);
int bytestowrite = 0;
buf[bytestowrite++] = '$';
uint16_t type = msg.msgType;
type = htons(type);
memcpy(buf + bytestowrite, &type, sizeof(uint16_t));
bytestowrite += 2;//skip type
bytestowrite += 4; //skip ip
uint32_t size = msg.len;
size = htonl(size);
memcpy(buf + bytestowrite, &size, sizeof(uint32_t));
bytestowrite += 4;
memcpy(buf + bytestowrite, msg.ptr, msg.len);
bytestowrite += msg.len;
buf[bytestowrite++] = '#';
if(writen(fd, buf, bytestowrite) < bytestowrite)
{
printf("write fail\n");
}
if(msg.ptr)
{
free(msg.ptr);
msg.ptr = NULL;
}
free(buf);
}