-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_client.c
299 lines (251 loc) · 7.99 KB
/
chat_client.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <uv.h>
#define DEFAULT_SERVER_PORT 7000
#define DEFAULT_SERVER_IP "127.0.0.1"
#define MAX_MSG_LEN 66
#define MSG_BUF_SIZE 256
#define INPUT_AREA_HEIGHT 5
#define KEY_BACKSPACE 127
#define PROMPT_LEN 14
#define USERNAME_LEN 12
typedef struct {
uv_write_t req;
uv_buf_t buf;
} write_req_t;
uv_loop_t *loop;
uv_tty_t tty; /* STDOUT */
uv_pipe_t in; /* STDIN */
uv_tcp_t *socket_;
int width;
int height;
int msgs_on_screen = 0;
uv_buf_t user_msg;
char username[USERNAME_LEN];
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
buf->base = (char*) malloc(suggested_size);
buf->len = suggested_size;
}
void free_write_req(uv_write_t *req) {
write_req_t *wr = (write_req_t*) req;
free(wr->buf.base);
free(wr);
}
void after_write(uv_write_t *req, int status) {
if (status) {
fprintf(stderr, "Write error %s\n", uv_strerror(status));
}
free_write_req(req);
}
void after_close(uv_handle_t *handle) {
free(handle);
}
void get_username() {
printf("Enter username: ");
scanf("%10s", username);
int i = strlen(username);
username[i] = ':';
while (++i <= USERNAME_LEN) username[i] = ' ';
}
void disable_line_buffering_and_echo() {
struct termios term;
tcgetattr(STDIN_FILENO, &term);
term.c_lflag &= ~ICANON;
term.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &term);
setbuf(stdin, NULL);
}
/*
* Free resources and reset terminal settings.
*/
void close_app(char *msg) {
uv_close((uv_handle_t*) socket_, after_close);
uv_close((uv_handle_t*) &tty, NULL);
uv_close((uv_handle_t*) &in, NULL);
free(user_msg.base);
uv_tty_reset_mode();
system("reset");
printf("\033[2J\033[H%s\n", msg);
}
/*
* Find window size, disable line buffering and keypress echo.
* Setup interface and initial cursor position.
*/
void setup_terminal() {
if (uv_tty_get_winsize(&tty, &width, &height)) {
fprintf(stderr, "Could not get TTY information\n");
uv_tty_reset_mode();
return;
}
disable_line_buffering_and_echo();
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf.base = (char*) malloc(MSG_BUF_SIZE);
int prompt_pos = height - INPUT_AREA_HEIGHT;
req->buf.len = sprintf(req->buf.base,
"\033[2J\033[H\033[%dB------------------\nYour message: ",
prompt_pos);
uv_write((uv_write_t*) req, (uv_stream_t*) &tty, &req->buf, 1, after_write);
}
void remove_old_messages() {
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf.base = (char*) malloc(MSG_BUF_SIZE);
int prompt_pos = height - INPUT_AREA_HEIGHT;
req->buf.len = sprintf(req->buf.base,
"\033[2J\033[H\033[%dB------------------\nYour message: %s",
prompt_pos, user_msg.base);
uv_write((uv_write_t*) req, (uv_stream_t*) &tty, &req->buf, 1, after_write);
}
/*
* Clear old messages if screen is full,
* print received message to screen and return cursor to message input area.
*/
void update_chat(const uv_buf_t *message) {
if (msgs_on_screen == height - INPUT_AREA_HEIGHT - 1) {
remove_old_messages();
msgs_on_screen = 1;
} else {
msgs_on_screen++;
}
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf.base = (char*) malloc(MSG_BUF_SIZE);
int cursor_y = height - INPUT_AREA_HEIGHT + 1;
int cursor_x = PROMPT_LEN + user_msg.len;
req->buf.len = sprintf(req->buf.base, "\033[H\033[%dB%s\033[H\033[%dB\033[%dC",
msgs_on_screen, message->base, cursor_y, cursor_x);
uv_write((uv_write_t*) req, (uv_stream_t*) &tty, &req->buf, 1, after_write);
}
/*
* Update chat. Close handles and reset terminal if server closes connection.
*/
void receive_message(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
if (nread > 0) {
update_chat(buf);
return;
} else if (nread < 0) {
if (nread != UV_EOF) {
fprintf(stderr, "Read error %s\n", uv_err_name(nread));
}
close_app("Chat server closed connection.");
}
free(buf->base);
}
/*
* Send username and message to server if message not empty.
*/
void send_message() {
if (user_msg.len == 0) {
return;
}
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf.len = USERNAME_LEN + user_msg.len + 1;
req->buf.base = (char*) malloc(req->buf.len);
memcpy(req->buf.base, username, USERNAME_LEN);
memcpy(req->buf.base + USERNAME_LEN, user_msg.base, user_msg.len + 1);
uv_write((uv_write_t*) req, (uv_stream_t*) socket_, &req->buf, 1, after_write);
for (int i = 0; i < MAX_MSG_LEN; i++) {
user_msg.base[i] = '\0';
}
user_msg.len = 0;
}
/*
* Update user_msg and tty depending on input char:
* - if char is printable, print
* - if user pressed backspace, delete char from message input
*/
void update_user_message(char input) {
int cursor_y = height - INPUT_AREA_HEIGHT + 1;
int cursor_x = PROMPT_LEN + user_msg.len;
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf.base = (char*) malloc(MSG_BUF_SIZE);
if (input == KEY_BACKSPACE && user_msg.len > 0) {
user_msg.len--;
cursor_x--;
req->buf.len = sprintf(req->buf.base, "\033[H\033[%dB\033[%dC \033[1D",
cursor_y, cursor_x);
uv_write((uv_write_t*) req, (uv_stream_t*) &tty, &req->buf, 1, after_write);
} else if (isprint(input) && user_msg.len < MAX_MSG_LEN) {
user_msg.base[user_msg.len++] = input;
user_msg.base[user_msg.len] = '\0';
req->buf.len = sprintf(req->buf.base, "\033[H\033[%dB\033[%dC%c",
cursor_y, cursor_x, input);
uv_write((uv_write_t*) req, (uv_stream_t*) &tty, &req->buf, 1, after_write);
} else {
free(req->buf.base);
free(req);
}
}
void clear_message_input() {
write_req_t *req = (write_req_t*) malloc(sizeof(write_req_t));
req->buf.base = (char*) malloc(MSG_BUF_SIZE);
int cursor_y = height - INPUT_AREA_HEIGHT + 1;
int cursor_x = PROMPT_LEN;
req->buf.len = sprintf(req->buf.base, "\033[H\033[%dB\033[%dC\033[0J",
cursor_y, cursor_x);
uv_write((uv_write_t*) req, (uv_stream_t*) &tty, &req->buf, 1, after_write);
}
/*
* Process single keypress. (line buffering is turned off)
* Update screen and send message if user pressed enter.
*/
void process_char(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf) {
if (nread < 0) {
close_app("Input error.");
} else if (nread == 1) {
char c = buf->base[0];
if (c == '\n') {
send_message();
clear_message_input();
} else if (isprint(c) || c == KEY_BACKSPACE) {
update_user_message(c);
}
}
free(buf->base);
}
/*
* If connection is sucessful, open input and output streams
* and start receiving messages. In case of error, close socket.
*/
void on_connect(uv_connect_t* req, int status) {
if (status < 0) {
uv_close((uv_handle_t*) socket_, after_close);
fprintf(stderr, "Connection error: %s\n", uv_strerror(status));
return;
}
uv_tty_init(loop, &tty, STDOUT_FILENO, 0);
uv_tty_set_mode(&tty, UV_TTY_MODE_NORMAL);
setup_terminal();
uv_pipe_init(loop, &in, 0);
uv_pipe_open(&in, 0);
uv_read_start((uv_stream_t*)&in, alloc_buffer, process_char);
strcpy(user_msg.base, "**connected**");
user_msg.len = strlen(user_msg.base);
send_message();
uv_read_start((uv_stream_t*)req->handle, alloc_buffer, receive_message);
}
/*
* TCP chat client
*/
int main() {
get_username();
user_msg.base = (char*) malloc(MAX_MSG_LEN + 1);
user_msg.len = 0;
loop = uv_default_loop();
socket_ = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
uv_tcp_init(loop, socket_);
uv_connect_t connect_req;
struct sockaddr_in addr;
uv_ip4_addr(DEFAULT_SERVER_IP, DEFAULT_SERVER_PORT, &addr);
int r = uv_tcp_connect(&connect_req, socket_, (const struct sockaddr*)&addr, on_connect);
if (r) {
fprintf(stderr, "Server connection error %s\n", uv_strerror(r));
return 1;
}
uv_run(loop, UV_RUN_DEFAULT);
uv_loop_close(loop);
return 0;
}