-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyserver2.c
More file actions
666 lines (581 loc) · 22.2 KB
/
yserver2.c
File metadata and controls
666 lines (581 loc) · 22.2 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
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
// gcc yserver2.c -o yserver2 -pthread
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <errno.h>
#include <time.h>
#define SERVER_PORT 12345
#define MAX_CLIENTS FD_SETSIZE
#define MAX_ROOMS 10
#define MAX_USERS_PER_ROOM 40
#define BUFFER_SIZE 1024
#define NICK_SIZE 32
#define ROOM_NAME_SIZE 32
#define INPUT_BUFFER_SIZE (BUFFER_SIZE * 2)
#define MAX_WORDS_PER_ROOM 100
typedef struct ClientInfo ClientInfo;
typedef struct Room Room;
struct ClientInfo {
int sock;
char nickname[NICK_SIZE];
int roomID;
int has_set_initial_nick;
char input_buffer[INPUT_BUFFER_SIZE];
int input_len;
};
struct Room {
int id;
char name[ROOM_NAME_SIZE];
ClientInfo* users[MAX_USERS_PER_ROOM];
int user_count;
char current_answer[64];
int drawer_sock;
int round_active;
time_t round_start_time;
int round_duration_seconds;
const char* word_list[MAX_WORDS_PER_ROOM];
int word_count;
ClientInfo* current_drawer;
};
ClientInfo* clients[MAX_CLIENTS];
Room rooms[MAX_ROOMS];
int room_count = 0;
int next_room_id = 1;
// --- 함수 프로토타입 ---
void start_new_round(Room* room);
void send_to_all_clients_in_room(Room* room, const char* msg);
void send_to_client(int sock, const char* msg);
ClientInfo* get_client_by_sock(int sock);
Room* get_room_by_id(int id);
Room* get_room_by_name(const char* name);
int is_nickname_taken(const char* nick);
void add_client_to_room(Room* room, ClientInfo* client);
void remove_client_from_room(ClientInfo* client, Room* room);
void handle_client_disconnection(int client_sock);
void process_client_message(ClientInfo* client, const char* message_line);
void handle_nick_command(ClientInfo* client, const char* nick_arg);
void handle_create_room_command(ClientInfo* client, const char* room_name_arg);
void handle_join_room_command(ClientInfo* client, const char* room_identifier_arg);
void handle_list_rooms_command(ClientInfo* client);
void handle_exit_room_command(ClientInfo* client);
void send_to_all_clients_in_room(Room* room, const char* msg) {
if (!room) return;
for (int i = 0; i < room->user_count; ++i) {
send_to_client(room->users[i]->sock, msg);
}
}
void broadcast_message_to_room(ClientInfo* sender, const char* original_message_payload) {
if (sender->roomID == -1 || !sender->has_set_initial_nick) {
send_to_client(sender->sock, "SMSG:메시지를 보내려면 방에 입장하고 닉네임을 설정해야 합니다.\n");
return;
}
Room* room = get_room_by_id(sender->roomID);
if (!room) return;
// 출제자가 정답을 입력한 경우 무시
if (room->round_active &&
sender == room->current_drawer &&
strcasecmp(original_message_payload, room->current_answer) == 0) {
send_to_client(sender->sock, "SMSG:출제자는 정답을 입력할 수 없습니다.\n");
return;
}
// 일반 사용자가 정답을 맞춘 경우
if (room->round_active &&
strcasecmp(original_message_payload, room->current_answer) == 0) {
char correct_msg[BUFFER_SIZE];
snprintf(correct_msg, BUFFER_SIZE, "ROOM_EVENT:[%s]님이 정답을 맞혔습니다! 정답은 '%s'였습니다.\n",
sender->nickname, room->current_answer);
for (int i = 0; i < room->user_count; i++) {
send_to_client(room->users[i]->sock, correct_msg);
}
send_to_client(sender->sock, "SMSG:정답입니다!\n"); // 정답 메시지
send_to_all_clients_in_room(room, "CLEAR\n"); // 그림판 클리어 지시
room->round_active = 0;
room->current_drawer = sender; // 새 출제자 설정
start_new_round(room); // 다음 라운드 시작
return;
}
// 오답 처리
send_to_client(sender->sock, "SMSG:오답입니다.\n");
// 일반 채팅 메시지 전송
char msg_to_broadcast[BUFFER_SIZE];
snprintf(msg_to_broadcast, BUFFER_SIZE, "MSG:[%s] %s\n", sender->nickname, original_message_payload);
for (int i = 0; i < room->user_count; i++) {
if (room->users[i]->sock != sender->sock) {
send_to_client(room->users[i]->sock, msg_to_broadcast);
}
}
}
void broadcast_draw_data_to_room(ClientInfo* sender, const char* draw_message_line_with_newline) {
if (sender->roomID == -1 || !sender->has_set_initial_nick) {
return;
}
Room* room = get_room_by_id(sender->roomID);
if (!room) return;
// 출제자가 아닌 경우 그리기 권한 없음
if (room->drawer_sock != sender->sock) {
send_to_client(sender->sock, "SMSG:출제자만 그림을 그릴 수 있습니다.\n");
return;
}
// broadcast to others
for (int i = 0; i < room->user_count; i++) {
if (room->users[i]->sock != sender->sock) {
send_to_client(room->users[i]->sock, draw_message_line_with_newline);
}
}
}
const char* global_word_list[] = { "Dog", "Car", "Hamburger", "Cap", "Chair", "Cat", "Book", "Cow", "ant", "Spider" };
int global_word_list_size = sizeof(global_word_list) / sizeof(global_word_list[0]);
void start_new_round(Room* room) {
if (!room || room->user_count == 0 || global_word_list_size == 0) return;
ClientInfo* drawer = NULL;
// 현재 출제자가 여전히 방에 있는지 확인
int found = 0;
if (room->current_drawer != NULL) {
for (int i = 0; i < room->user_count; ++i) {
if (room->users[i] == room->current_drawer) {
found = 1;
break;
}
}
}
// 유효한 출제자가 없으면 새로 랜덤 선택
if (!found) {
int drawer_index = rand() % room->user_count;
drawer = room->users[drawer_index];
room->current_drawer = drawer;
}
else {
drawer = room->current_drawer;
}
// 정답 단어 설정
int word_index = rand() % global_word_list_size;
strncpy(room->current_answer, room->word_list[word_index], sizeof(room->current_answer) - 1);
room->current_answer[sizeof(room->current_answer) - 1] = '\0';
// 사용한 단어 제거
for (int i = word_index; i < room->word_count - 1; i++) {
room->word_list[i] = room->word_list[i + 1];
}
room->word_count--;
room->drawer_sock = drawer->sock;
room->round_active = 1;
room->round_start_time = time(NULL);
room->round_duration_seconds = 60;
// 타이머 메시지 전송
char timer_msg[64];
snprintf(timer_msg, sizeof(timer_msg), "TIMER:%d\n", room->round_duration_seconds);
send_to_all_clients_in_room(room, timer_msg);
// 제시어 전달 및 출제자 알림
char msg[BUFFER_SIZE];
snprintf(msg, BUFFER_SIZE, "SMSG:제시어는 '%s'입니다. 그려주세요.\n", room->current_answer);
send_to_client(drawer->sock, msg);
snprintf(msg, BUFFER_SIZE, "ROOM_EVENT:[%s]님이 출제자입니다. 정답을 맞혀보세요!\n", drawer->nickname);
for (int i = 0; i < room->user_count; ++i) {
if (room->users[i]->sock != drawer->sock) {
send_to_client(room->users[i]->sock, msg);
}
}
}
void send_to_client(int sock, const char* msg) {
if (send(sock, msg, strlen(msg), 0) < 0) {
perror("send to client failed");
}
}
ClientInfo* get_client_by_sock(int sock) {
if (sock < 0 || sock >= MAX_CLIENTS) return NULL;
return clients[sock];
}
Room* get_room_by_id(int id) {
for (int i = 0; i < room_count; i++) {
if (rooms[i].id == id) return &rooms[i];
}
return NULL;
}
Room* get_room_by_name(const char* name) {
for (int i = 0; i < room_count; i++) {
if (strcmp(rooms[i].name, name) == 0) return &rooms[i];
}
return NULL;
}
int is_nickname_taken(const char* nick) {
for (int i = 0; i < MAX_CLIENTS; i++) {
if (clients[i] != NULL && clients[i]->has_set_initial_nick && strcmp(clients[i]->nickname, nick) == 0) {
return 1;
}
}
return 0;
}
void add_client_to_room(Room* room, ClientInfo* client) {
if (room->user_count < MAX_USERS_PER_ROOM) {
room->users[room->user_count++] = client;
client->roomID = room->id;
char smsg[BUFFER_SIZE];
snprintf(smsg, BUFFER_SIZE, "SMSG:방 '%s'(ID:%d)에 입장했습니다. 그림판 UI가 활성화됩니다.\n", room->name, room->id);
send_to_client(client->sock, smsg);
char enter_msg[BUFFER_SIZE];
snprintf(enter_msg, BUFFER_SIZE, "ROOM_EVENT:[%s]님이 입장했습니다.\n", client->nickname);
for (int i = 0; i < room->user_count; i++) {
if (room->users[i]->sock != client->sock) {
send_to_client(room->users[i]->sock, enter_msg);
}
}
}
else {
send_to_client(client->sock, "SMSG:방이 가득 찼습니다.\n");
}
}
void remove_client_from_room(ClientInfo* client, Room* room) {
if (!room || client->roomID != room->id) return;
int found = 0;
for (int i = 0; i < room->user_count; i++) {
if (room->users[i]->sock == client->sock) {
found = 1;
}
if (found && i < room->user_count - 1) {
room->users[i] = room->users[i + 1];
}
}
if (found) {
room->user_count--;
char leave_msg[BUFFER_SIZE];
snprintf(leave_msg, BUFFER_SIZE, "ROOM_EVENT:[%s]님이 퇴장했습니다.\n", client->nickname);
for (int i = 0; i < room->user_count; i++) {
send_to_client(room->users[i]->sock, leave_msg);
}
printf("[서버] %s님이 방 '%s'(ID:%d)에서 퇴장.\n", client->nickname, room->name, room->id);
}
client->roomID = -1;
if (room->user_count == 0 && room_count > 0) {
printf("[서버] 방 '%s'(ID:%d)이(가) 비어서 삭제됩니다.\n", room->name, room->id);
int room_idx = -1;
for (int i = 0; i < room_count; ++i) if (rooms[i].id == room->id) room_idx = i;
if (room_idx != -1) {
for (int i = room_idx; i < room_count - 1; ++i) {
rooms[i] = rooms[i + 1];
}
room_count--;
// rooms[room_count]의 내용을 초기화할 필요는 없음. room_count가 줄었으므로 접근 안됨.
}
}
}
void handle_client_disconnection(int client_sock) {
ClientInfo* client = get_client_by_sock(client_sock);
if (!client) return;
printf("[서버] 클라이언트 %s (sock %d) 연결 종료 처리 시작.\n", client->has_set_initial_nick ? client->nickname : "익명", client_sock);
if (client->roomID != -1) {
Room* room = get_room_by_id(client->roomID);
if (room) {
remove_client_from_room(client, room);
}
}
close(client_sock);
free(clients[client_sock]);
clients[client_sock] = NULL;
printf("[서버] 클라이언트 (sock %d) 리소스 해제 완료.\n", client_sock);
}
void handle_nick_command(ClientInfo* client, const char* nick_arg) {
if (strlen(nick_arg) == 0 || strlen(nick_arg) >= NICK_SIZE) {
send_to_client(client->sock, "SMSG:닉네임 형식이 올바르지 않습니다. (1~31자)\n");
return;
}
if (is_nickname_taken(nick_arg)) {
char err_msg[BUFFER_SIZE];
snprintf(err_msg, BUFFER_SIZE, "ERR_NICK_TAKEN:%s\n", nick_arg);
send_to_client(client->sock, err_msg);
return;
}
char old_nick[NICK_SIZE];
strcpy(old_nick, client->nickname);
int was_initial_nick_set = client->has_set_initial_nick;
strcpy(client->nickname, nick_arg);
client->has_set_initial_nick = 1;
char smsg[BUFFER_SIZE];
snprintf(smsg, BUFFER_SIZE, "SMSG:닉네임이 '%s'(으)로 설정되었습니다.\n", client->nickname);
send_to_client(client->sock, smsg);
if (client->roomID != -1 && was_initial_nick_set) {
char nick_change_broadcast[BUFFER_SIZE];
snprintf(nick_change_broadcast, BUFFER_SIZE, "ROOM_EVENT:['%s'님이 '%s'(으)로 닉네임을 변경했습니다.]\n", old_nick, client->nickname);
Room* room = get_room_by_id(client->roomID);
if (room) {
for (int i = 0; i < room->user_count; ++i) {
if (room->users[i]->sock != client->sock) send_to_client(room->users[i]->sock, nick_change_broadcast);
}
}
}
}
void handle_create_room_command(ClientInfo* client, const char* room_name_arg) {
if (!client->has_set_initial_nick) {
send_to_client(client->sock, "ERR_NICK_REQUIRED:방 생성 먼저 /nick <닉네임> 설정 필요\n");
return;
}
if (client->roomID != -1) {
send_to_client(client->sock, "SMSG:이미 다른 방에 참여중입니다. 먼저 퇴장해주세요. (/exit)\n");
return;
}
if (room_count >= MAX_ROOMS) {
send_to_client(client->sock, "SMSG:더 이상 방을 만들 수 없습니다.\n");
return;
}
if (strlen(room_name_arg) == 0 || strlen(room_name_arg) >= ROOM_NAME_SIZE) {
send_to_client(client->sock, "SMSG:방 이름이 유효하지 않습니다.\n");
return;
}
if (get_room_by_name(room_name_arg) != NULL) {
send_to_client(client->sock, "SMSG:이미 존재하는 방 이름입니다.\n");
return;
}
Room* new_room = &rooms[room_count];
new_room->id = next_room_id++;
strncpy(new_room->name, room_name_arg, ROOM_NAME_SIZE - 1);
new_room->name[ROOM_NAME_SIZE - 1] = '\0';
new_room->user_count = 0;
// 방별 단어 리스트 초기화
for (int i = 0; i < global_word_list_size; i++) {
new_room->word_list[i] = global_word_list
[i];
}
new_room->word_count = global_word_list_size;
room_count++;
add_client_to_room(new_room, client);
start_new_round(new_room);
}
void handle_join_room_command(ClientInfo* client, const char* room_identifier_arg) {
if (!client->has_set_initial_nick) {
send_to_client(client->sock, "ERR_NICK_REQUIRED:방 입장 먼저 /nick <닉네임> 설정 필요\n");
return;
}
if (client->roomID != -1) {
send_to_client(client->sock, "SMSG:이미 다른 방에 참여중입니다. 먼저 퇴장해주세요. (/exit)\n");
return;
}
if (strlen(room_identifier_arg) == 0) {
send_to_client(client->sock, "SMSG:입장할 방 번호 또는 방 이름을 입력하세요.\n");
return;
}
Room* room_to_join = NULL;
int room_id_attempt = atoi(room_identifier_arg);
if (room_id_attempt > 0) {
room_to_join = get_room_by_id(room_id_attempt);
}
if (!room_to_join) {
room_to_join = get_room_by_name(room_identifier_arg);
}
if (!room_to_join) {
send_to_client(client->sock, "SMSG:존재하지 않는 방이거나 잘못된 입력입니다.\n");
return;
}
add_client_to_room(room_to_join, client);
// 입장 후 자동 라운드 시작 조건 확인
if (room_to_join->user_count >= 2 && !room_to_join->round_active && global_word_list_size > 0) {
printf("[서버] 두 명 이상이 입장했으므로 라운드 시작\n");
start_new_round(room_to_join);
}
}
void handle_list_rooms_command(ClientInfo* client) {
char list_buffer[BUFFER_SIZE] = "ROOMLIST:";
int current_len = strlen(list_buffer);
int first_room = 1;
if (room_count == 0) {
strcat(list_buffer, "현재 생성된 방이 없습니다.\n");
}
else {
for (int i = 0; i < room_count; i++) {
if (rooms[i].id > 0) {
char room_detail[128];
if (!first_room) {
if (current_len + 1 < BUFFER_SIZE - 1) {
strcat(list_buffer, ";"); current_len += 1;
}
else break;
}
snprintf(room_detail, sizeof(room_detail), "%d-%s(%d)", rooms[i].id, rooms[i].name, rooms[i].user_count);
if (current_len + strlen(room_detail) < BUFFER_SIZE - 1) {
strcat(list_buffer, room_detail);
current_len += strlen(room_detail);
first_room = 0;
}
else {
if (current_len + 4 < BUFFER_SIZE - 1) strcat(list_buffer, "..."); current_len += 3;
break;
}
}
}
if (list_buffer[current_len - 1] == ';') list_buffer[current_len - 1] = '\n';
else if (first_room && room_count > 0) {} // 방은 있으나 유효한 방이 없어 아무것도 안찍힌 경우
else strcat(list_buffer, "\n");
}
send_to_client(client->sock, list_buffer);
}
void handle_exit_room_command(ClientInfo* client) {
if (client->roomID == -1) {
send_to_client(client->sock, "SMSG:현재 어떤 방에도 참여하고 있지 않습니다.\n");
return;
}
Room* room = get_room_by_id(client->roomID);
if (room) {
remove_client_from_room(client, room);
}
send_to_client(client->sock, "SMSG:방에서 퇴장했습니다. 로비로 돌아갑니다.\n");
}
void process_client_message(ClientInfo* client, const char* message_line_no_newline) { // 개행 없는 메시지 라인
printf("[서버 수신] (sock %d, nick %s, room %d): %s\n", client->sock, client->nickname, client->roomID, message_line_no_newline);
if (strncmp(message_line_no_newline, "NICK:", 5) == 0) {
handle_nick_command(client, message_line_no_newline + 5);
}
else if (strncmp(message_line_no_newline, "CREATE_ROOM:", 12) == 0) {
handle_create_room_command(client, message_line_no_newline + 12);
}
else if (strncmp(message_line_no_newline, "JOIN_ROOM:", 10) == 0) {
handle_join_room_command(client, message_line_no_newline + 10);
}
else if (strcmp(message_line_no_newline, "LIST_ROOMS") == 0) {
handle_list_rooms_command(client);
}
else if (strcmp(message_line_no_newline, "EXIT_ROOM") == 0) {
handle_exit_room_command(client);
}
else if (strncmp(message_line_no_newline, "MSG:", 4) == 0) {
broadcast_message_to_room(client, message_line_no_newline + 4);
}
else if (strncmp(message_line_no_newline, "DRAW_POINT:", 11) == 0 ||
strncmp(message_line_no_newline, "DRAW_LINE:", 10) == 0 ||
strcmp(message_line_no_newline, "DRAW_CLEAR") == 0) {
char draw_message_with_newline[BUFFER_SIZE];
snprintf(draw_message_with_newline, BUFFER_SIZE, "%s\n", message_line_no_newline); // 개행 추가해서 전달
broadcast_draw_data_to_room(client, draw_message_with_newline);
}
else if (strcmp(message_line_no_newline, "QUIT") == 0) {
printf("[서버] 클라이언트 %s (sock %d)가 QUIT 요청. 연결 종료 처리 유도.\n", client->nickname, client->sock);
// 실제 연결 종료는 recv <= 0 일 때 handle_client_disconnection에서 처리됨
}
else {
send_to_client(client->sock, "SMSG:알 수 없는 형식의 메시지 또는 명령어입니다.\n");
}
}
int main() {
srand(time(NULL));
int listen_sock, new_sock_fd;
struct sockaddr_in server_address, client_address;
socklen_t client_address_len;
fd_set master_fd_set, read_fd_set;
int max_fd;
for (int i = 0; i < MAX_CLIENTS; i++) clients[i] = NULL;
for (int i = 0; i < MAX_ROOMS; i++) { rooms[i].id = 0; rooms[i].user_count = 0; }
listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock < 0) { perror("socket"); exit(EXIT_FAILURE); }
int optval = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
memset(&server_address, 0, sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(SERVER_PORT);
if (bind(listen_sock, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) {
perror("bind"); close(listen_sock); exit(EXIT_FAILURE);
}
if (listen(listen_sock, MAX_CLIENTS) < 0) { // SOMAXCONN 대신 MAX_CLIENTS
perror("listen"); close(listen_sock); exit(EXIT_FAILURE);
}
FD_ZERO(&master_fd_set);
FD_SET(listen_sock, &master_fd_set);
max_fd = listen_sock;
printf("[서버] 캐치마인드 게임 서버 실행 중 (포트: %d)...\n", SERVER_PORT);
while (1) {
read_fd_set = master_fd_set;
if (select(max_fd + 1, &read_fd_set, NULL, NULL, NULL) < 0) {
if (errno == EINTR) continue;
perror("select error"); break;
}
for (int i = 0; i < MAX_ROOMS; ++i) {
Room* room = &rooms[i];
if (room->id > 0 && room->round_active && room->user_count > 0) {
time_t now = time(NULL);
if (now - room->round_start_time >= room->round_duration_seconds) {
// 시간 초과: 새로운 라운드 시작
char msg[] = "ROOM_EVENT:시간 초과! 출제자가 자동으로 변경됩니다.\n";
send_to_all_clients_in_room(room, msg);
room->round_active = 0; // 현재 라운드 종료
room->current_drawer = NULL;
start_new_round(room); // 다음 라운드 시작
}
}
}
for (int sock_fd = 0; sock_fd <= max_fd; sock_fd++) {
if (FD_ISSET(sock_fd, &read_fd_set)) {
if (sock_fd == listen_sock) {
client_address_len = sizeof(client_address);
new_sock_fd = accept(listen_sock, (struct sockaddr*)&client_address, &client_address_len);
if (new_sock_fd < 0) { perror("accept"); continue; }
if (new_sock_fd >= MAX_CLIENTS) {
fprintf(stderr, "[서버] 최대 클라이언트 수(%d) 도달. 연결 거부 (fd %d).\n", MAX_CLIENTS, new_sock_fd);
send_to_client(new_sock_fd, "SMSG:서버가 가득 찼습니다.\n");
close(new_sock_fd);
continue;
}
clients[new_sock_fd] = (ClientInfo*)malloc(sizeof(ClientInfo));
if (!clients[new_sock_fd]) { perror("malloc for client failed"); close(new_sock_fd); continue; }
clients[new_sock_fd]->sock = new_sock_fd;
strcpy(clients[new_sock_fd]->nickname, "익명");
clients[new_sock_fd]->roomID = -1;
clients[new_sock_fd]->has_set_initial_nick = 0;
clients[new_sock_fd]->input_len = 0;
memset(clients[new_sock_fd]->input_buffer, 0, INPUT_BUFFER_SIZE);
FD_SET(new_sock_fd, &master_fd_set);
if (new_sock_fd > max_fd) max_fd = new_sock_fd;
printf("[서버] 새 클라이언트 연결: %s (sock %d)\n", inet_ntoa(client_address.sin_addr), new_sock_fd);
send_to_client(new_sock_fd, "SMSG:서버에 연결되었습니다. 라운지입니다. 먼저 /nick <원하는닉네임> 으로 닉네임을 설정해주세요.\nSMSG:닉네임 설정 후 /list, /create <방이름>, /join <방ID/이름> 명령어를 사용할 수 있습니다.\n");
}
else {
ClientInfo* client = get_client_by_sock(sock_fd);
if (!client) continue;
char recv_temp_buffer[BUFFER_SIZE];
int nbytes = recv(sock_fd, recv_temp_buffer, BUFFER_SIZE - 1, 0);
if (nbytes <= 0) {
if (nbytes == 0) printf("[서버] 클라이언트 %s (sock %d) 연결 정상 종료.\n", client->has_set_initial_nick ? client->nickname : "익명", sock_fd);
else perror("recv from client failed");
FD_CLR(sock_fd, &master_fd_set);
handle_client_disconnection(sock_fd);
}
else {
recv_temp_buffer[nbytes] = '\0';
if (client->input_len + nbytes < INPUT_BUFFER_SIZE) {
strncat(client->input_buffer, recv_temp_buffer, nbytes);
client->input_len += nbytes;
}
else {
fprintf(stderr, "[서버] 클라이언트 (sock %d) 입력 버퍼 오버플로우.\n", sock_fd);
client->input_len = 0; memset(client->input_buffer, 0, INPUT_BUFFER_SIZE);
}
char* line_start = client->input_buffer;
char* newline_ptr;
while ((newline_ptr = strchr(line_start, '\n')) != NULL) {
*newline_ptr = '\0';
char single_line_msg[INPUT_BUFFER_SIZE];
strcpy(single_line_msg, line_start);
process_client_message(client, single_line_msg); // 이제 개행 없는 메시지 전달
line_start = newline_ptr + 1;
}
if (line_start > client->input_buffer && strlen(line_start) > 0) {
char temp_remaining_data[INPUT_BUFFER_SIZE];
strcpy(temp_remaining_data, line_start);
memset(client->input_buffer, 0, INPUT_BUFFER_SIZE);
strcpy(client->input_buffer, temp_remaining_data);
client->input_len = strlen(client->input_buffer);
}
else {
client->input_len = 0;
memset(client->input_buffer, 0, INPUT_BUFFER_SIZE);
}
}
}
}
}
}
for (int i = 0; i <= max_fd; ++i) if (FD_ISSET(i, &master_fd_set) && clients[i] && i != listen_sock) handle_client_disconnection(i);
close(listen_sock);
printf("[서버] 서버 종료.\n");
return 0;
}