-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatServer.cpp
More file actions
268 lines (220 loc) · 7.61 KB
/
chatServer.cpp
File metadata and controls
268 lines (220 loc) · 7.61 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
#include <iostream>
#include <unistd.h>
#include <bits/stdc++.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include <time.h>
#define BUFLEN 2048
#define SERVER_IP_ADDRESS "127.0.0.1"
#define PORT 5300
// Structure to manage client info
struct client{
char username[20];
int uid;
int client_sock_fd;
};
// Keep track of FDs of all connected clients
std::vector<int> sock_fds;
// Keep track of all users and their sock_fds
std::unordered_map<int, int> users;
std::unordered_map<std::string, int> usernames;
// Available commands
std::vector<std::string> commands = {"whisper", "view"};
// Mutex for sync
pthread_mutex_t sock_fds_mutex PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t users_mutex PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t usernames_mutex PTHREAD_MUTEX_INITIALIZER;
// Trims strings of whitespaces
std::string trim(const std::string& str) {
size_t start = str.find_first_not_of(" \n\r\t");
size_t end = str.find_last_not_of(" \n\r\t");
return (start == std::string::npos || end == std::string::npos) ? "" : str.substr(start, end - start + 1);
}
// Broadcasts received messages to all clients
void broadcastMsg(const char *data, const char *username, int uid){
char message[BUFLEN];
snprintf(message, sizeof(message), "%s [%d]: %s", username, uid, data);
pthread_mutex_lock(&sock_fds_mutex);
for(auto sock_fd : sock_fds){
write(sock_fd, message, strlen(message));
}
pthread_mutex_unlock(&sock_fds_mutex);
}
// Whisper private message to a client
int whisperMsg(int uid, const char *username, const char *private_msg){
int sock_fd;
char message[BUFLEN];
snprintf(message, sizeof(message), "[WHISPER] %s [%d]: %s", username, uid, private_msg);
pthread_mutex_lock(&users_mutex);
sock_fd = users[uid];
pthread_mutex_unlock(&users_mutex);
if(write(sock_fd, message, sizeof(message))==-1){
std::cout << "Failed to send whisper response message to client!" << std::endl;
return 0;
}
return 1;
}
int viewAllUsers(int sock_fd){
char message[BUFLEN];
bzero(message, sizeof(message));
strcpy(message, "\nAll connected users:\n");
pthread_mutex_lock(&usernames_mutex);
for(auto it: usernames){
char user[100];
bzero(user, sizeof(user));
snprintf(user, sizeof(user), "%s : %d\n", it.first.c_str(), it.second);
strcat(message, user);
}
pthread_mutex_unlock(&usernames_mutex);
if(write(sock_fd, message, strlen(message))==-1){
std::cout << "Failed to send view clients response to client!" << std::endl;
return 0;
}
return 1;
}
int sendInvalidCommandError(int sock_fd){
char message[BUFLEN];
bzero(message, sizeof(message));
snprintf(message, sizeof(message), "Invalid command!");
if(write(sock_fd, message, sizeof(message))==-1){
std::cout << "Failed to send invalid command response to client!" << std::endl;
return 0;
}
return 1;
}
// Read data from clients
void *handleClient(void *client){
char username[20];
bzero(username, sizeof(username));
struct client *c = (struct client*)client;
int sock_fd = c->client_sock_fd;
int uid = c->uid;
strcpy(username, c->username);
username[strcspn(username, "\n")] = '\0';
std::cout << username << " joined!\n" << std::endl;
pthread_mutex_lock(&sock_fds_mutex);
sock_fds.push_back(sock_fd);
pthread_mutex_unlock(&sock_fds_mutex);
char r_buff[BUFLEN];
char private_msg[BUFLEN];
while(1){
// Recieve data from a single client
bzero(r_buff, sizeof(r_buff));
int n = read(sock_fd, r_buff, sizeof(r_buff));
if(n<=0){
std::cout << username << " disconnected!" << std::endl;
break;
}
// Command is received
if(r_buff[0]=='/'){
char user_id[10];
bzero(private_msg, sizeof(private_msg));
char *ptr = r_buff + 1;
char *temp;
temp = strtok(ptr, " ");
int args = 0;
std::string cmd;
while(temp!=NULL){
if(args==0){
cmd = std::string(temp);
cmd = trim(cmd);
args++;
}else if(args==1){
strcpy(user_id, temp);
args++;
}else{
strcat(private_msg, temp);
strcat(private_msg, " ");
}
temp = strtok(NULL, " ");
}
if(cmd=="whisper"){
whisperMsg(atoi(user_id), username, private_msg);
}else if(cmd=="view"){
viewAllUsers(sock_fd);
}else{
sendInvalidCommandError(sock_fd);
}
}else{
// Message is received
std::cout << username << "[" << uid << "]" << " says: " << r_buff << std::endl;
// Broadcast data to all clients
broadcastMsg(r_buff, username, uid);
}
}
pthread_mutex_lock(&sock_fds_mutex);
sock_fds.erase(std::remove(sock_fds.begin(), sock_fds.end(), sock_fd), sock_fds.end());
pthread_mutex_unlock(&sock_fds_mutex);
pthread_mutex_lock(&users_mutex);
users.erase(uid);
pthread_mutex_unlock(&users_mutex);
close(sock_fd);
free(c);
return NULL;
}
int main(int argc, char **argv){
int sock_fd, client_fd;
int yes = 1;
char username[20];
srand(time(NULL));
// Internet socket address
struct sockaddr_in server, client;
socklen_t client_len;
// Create a socket
if((sock_fd = socket(AF_INET, SOCK_STREAM, 0))==-1){
std::cout << "Failed to create a socket!" << std::endl;
exit(1);
}
// Define server port, address and family
server.sin_addr.s_addr = inet_addr(SERVER_IP_ADDRESS);
server.sin_port = PORT;
server.sin_family = AF_INET;
// Set socket options
if(setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes))==-1){
std::cout << "Failed to set socket options" << std::endl;
exit(1);
}
// Bind socket to server
if(bind(sock_fd, (struct sockaddr *)&server, sizeof(server))==-1){
std::cout << "Failed to bind socket to server" << std::endl;
exit(1);
}
// Listen for connections
listen(sock_fd, 5);
std::cout << "Server started on port: " << PORT << "\n" << std::endl;
while(1){
// Accept any client connections
client_len = sizeof(client);
if((client_fd = accept(sock_fd, (struct sockaddr*)&client, &client_len))==-1){
std::cout << "Failed to accept client!" << std::endl;
exit(1);
}
// Read user details from client
bzero(username, sizeof(username));
if(read(client_fd, username, sizeof(username))==-1){
std::cout << "Failed to read client details!" << std::endl;
}
int uid = rand()%100;
struct client *c = (struct client *)malloc(sizeof(struct client));
c->client_sock_fd = client_fd;
c->uid = uid;
strcpy(c->username, username);
users[uid] = client_fd;
std::string name = username;
name = trim(name);
usernames[name] = uid;
// Create a new thread for every client
pthread_t tid;
if(pthread_create(&tid, NULL, handleClient, (void *)c)!=0){
std::cout << "Failed to create client thread" << std::endl;
}
// After the thread terminates, storage for tid can be reclaimed
pthread_detach(tid);
}
close(sock_fd);
return 0;
}