-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatclient.c
187 lines (175 loc) · 6.04 KB
/
chatclient.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
#include <ncurses.h>
#include <stdio.h>
#include "chatclient.h"
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <arpa/inet.h>
#include <errno.h>
#include "defines.h"
#include "debug.h"
ChatClient initChatClient(char server_ip[SERVER_IP_BUFFER], int server_port, char username[USERNAME_BUFFER])
{
ChatClient client;
client.server_socket = socket(AF_INET, SOCK_STREAM, 0);
client.server_address.sin_family = AF_INET;
client.server_address.sin_port = htons(server_port);
client.server_address.sin_addr.s_addr = inet_addr(server_ip);
strcpy(client.username, username);
return client;
}
void sendMessage(ChatClient *client, Message message)
{
// Serizalizing message
uint32_t fromLength = strlen(message.from);
uint32_t contentLength = strlen(message.content);
if (fromLength >= USERNAME_BUFFER)
fromLength = USERNAME_BUFFER - 1;
if (contentLength >= MESSAGE_BUFFER)
contentLength = MESSAGE_BUFFER - 1;
size_t totalSize = sizeof(uint32_t) * 2 + fromLength + contentLength;
char *buffer = malloc(totalSize);
if (buffer == NULL)
{
debugLog(ERROR, "Failed to allocate memory for message buffer");
exit(1);
}
uint32_t netFromLength = htonl(fromLength);
uint32_t netContentLength = htonl(contentLength);
size_t offset = 0;
memcpy(buffer + offset, &netFromLength, sizeof(netFromLength));
offset += sizeof(netFromLength);
memcpy(buffer + offset, &netContentLength, sizeof(netContentLength));
offset += sizeof(netContentLength);
memcpy(buffer + offset, message.from, fromLength);
offset += fromLength;
memcpy(buffer + offset, message.content, contentLength);
offset += contentLength;
ssize_t totalSent = 0;
ssize_t n;
while (totalSent < totalSize)
{
n = send(client->server_socket, buffer + totalSent, totalSize - totalSent, 0);
if (n < 0)
{
debugLog(ERROR, "Failed to send message (sent %zu of %zu bytes)", totalSent, totalSize);
free(buffer);
exit(1);
}
totalSent += n;
}
debugLog(INFO, "Sent message \"%s\" to server", message.content);
free(buffer);
}
void *receiveMessages(void *arg)
{
JoinChatData *data = (JoinChatData *)arg;
ChatClient client = data->client;
MessageReceivedCallback receiveMessage = data->receiveMessageCallback;
free(data);
char buffer[BUFFER_SIZE];
ssize_t bytesReceived = 0;
debugLog(VERBOSE, "Listening to messages");
while (1)
{
uint32_t netFromLength = 0;
uint32_t netContentLength = 0;
ssize_t n;
n = recv(client.server_socket, &netFromLength, sizeof(netFromLength), MSG_WAITALL);
if (n <= 0)
{
if (n == 0)
debugLog(INFO, "Server closed the connection");
else
debugLog(ERROR, "Error receiving 'fromLength': %s (%d)", strerror(errno), errno);
break;
}
n = recv(client.server_socket, &netContentLength, sizeof(netContentLength), MSG_WAITALL);
if (n <= 0)
{
if (n == 0)
debugLog(INFO, "Server closed the connection");
else
debugLog(ERROR, "Error receiving 'contentLength': %s (%d)", strerror(errno), errno);
break;
}
uint32_t fromLength = ntohl(netFromLength);
uint32_t contentLength = ntohl(netContentLength);
if (fromLength >= USERNAME_BUFFER || contentLength >= MESSAGE_BUFFER)
{
debugLog(ERROR, "Received invalid lengths: from_length=%u, content_length=%u", fromLength, contentLength);
break;
}
char from[USERNAME_BUFFER];
char content[MESSAGE_BUFFER];
n = recv(client.server_socket, from, fromLength, MSG_WAITALL);
if (n <= 0)
{
if (n == 0)
debugLog(INFO, "Server closed the connection");
else
debugLog(ERROR, "Error receiving 'from': %s (%d)", strerror(errno), errno);
break;
}
from[fromLength] = '\0';
n = recv(client.server_socket, content, contentLength, MSG_WAITALL);
if (n <= 0)
{
if (n == 0)
debugLog(INFO, "Server closed the connection");
else
debugLog(ERROR, "Error receiving 'content': %s (%d)", strerror(errno), errno);
break;
}
content[contentLength] = '\0';
// Deserializing message
Message msg;
strcpy(msg.from, from);
strcpy(msg.content, content);
debugLog(INFO, "Message from \"%s\": %s", msg.from, msg.content);
if (receiveMessage != NULL)
receiveMessage(msg);
else
debugLog(ERROR, "ReceiveMessage is NULL");
}
pthread_exit(NULL);
}
void connectToChat(ChatClient *client, char username[USERNAME_BUFFER], MessageReceivedCallback receiveMessage)
{
if (connect(client->server_socket, (struct sockaddr *)&client->server_address, sizeof(client->server_address)) < 0)
{
debugLog(ERROR, "Falied to connect to server");
exit(1);
}
debugLog(INFO, "Connected to server");
pthread_t receiver_thread;
JoinChatData *data = malloc(sizeof(JoinChatData));
if (data == NULL)
{
debugLog(ERROR, "Failed to allocate memory for JoinChatData");
exit(1);
}
data->client = *client;
data->receiveMessageCallback = receiveMessage;
if (pthread_create(&receiver_thread, NULL, receiveMessages, (void *)data) != 0)
{
debugLog(ERROR, "Falied to create client thread");
free(data);
exit(1);
}
debugLog(VERBOSE, "Created client thread");
// Sending username
Message msg = {.content = "[Joined the chat]"};
strcpy(msg.from, username);
sendMessage(client, msg);
}
void disconnect(ChatClient *client)
{
if (close(client->server_socket) < 0)
{
debugLog(ERROR, "Couldn't close connection with server");
exit(1);
}
debugLog(VERBOSE, "Closed connection with server");
}