-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
76 lines (67 loc) · 1.78 KB
/
client.cpp
File metadata and controls
76 lines (67 loc) · 1.78 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
#include "client.h"
Client::Client(void (MainWindow::* updateTextBox)(QString), MainWindow *mw, QString ip, QString port, QString username)
{
this->updateTextBox = updateTextBox;
this->mw = mw;
openSocket();
connect(ip, port);
ReceivedInfo si;
si.type = JOIN;
memcpy(&si.joinInfo.username, username.toStdString().c_str(), std::min(MAX_USERNAME_LEN, (int)(username.length())));
Network::send (si, servaddr);
ReceivedInfo ri;
for (int index = 0; index < 5; index++)
{
ri = recieve(servaddr);
if (ri.type == ACCEPT_JOIN)
{
break;
}
}
if (ri.type == ACCEPT_JOIN)
{
connected = true;
networkThread = std::thread(&Client::loop, this);
}
}
void Client::connect(QString ip, QString port)
{
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = inet_addr(ip.toStdString().c_str());
servaddr.sin_port = htons(port.toInt());
}
Client::~Client()
{
close(sockfd);
}
void Client::loop()
{
ReceivedInfo ri;
loopBool = true;
while(loopBool)
{
memset(&ri, 0, sizeof(ReceivedInfo));
ri = recieve(servaddr);
if(ri.type)
{
QString displayedMessage;
displayedMessage += ri.serverMessageInfo.userName;
displayedMessage += ": ";
displayedMessage += ri.serverMessageInfo.message;
(mw->*updateTextBox)(displayedMessage);
}
}
}
void Client::send(QString string)
{
ReceivedInfo si;
si.type = USER_MESSAGE;
memcpy(si.userMessageInfo.message, string.toStdString().c_str(), MAX_MESSAGE_LEN);
Network::send(si, servaddr);
QString name = "me: ";
(mw->*updateTextBox)(name + si.userMessageInfo.message);
}
bool Client::getConnected()
{
return connected;
}