-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsconvoclientconnection.cpp
112 lines (96 loc) · 3.01 KB
/
dsconvoclientconnection.cpp
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
#include "dsconvoclientconnection.h"
#include <QtDebug>
#include <QScopedPointer>
#include "dsconvocommon.h"
DSConvoClientConnection::DSConvoClientConnection(QTcpSocket *socket,
QObject *parent)
: DSConvoConnection(socket, parent)
, state_(Ungreeted)
{
}
void DSConvoClientConnection::sendHello(const QString &username)
{
Q_ASSERT(state_ == Ungreeted);
qDebug("[DEBUG] DSConvoClientConnection::sendHello(%s)",
username.toLocal8Bit().constData());
state_ = Greeting;
send(DSConvo::Protocol::makeHelloRequest(username.toStdString()));
}
void DSConvoClientConnection::sendMessage(const QString &message)
{
Q_ASSERT(state_ == Greeted);
send(DSConvo::Protocol::makeMessageRequest(message.toStdString()));
}
void DSConvoClientConnection::sendGoodbye()
{
send(DSConvo::Protocol::makeGoodbye());
state_ = Ungreeted;
}
void DSConvoClientConnection::handleHelloReply(const google::protobuf::Message *p)
{
using DSConvoProtocol::HelloReplyPayload;
if (state_ != Greeting) {
return;
}
const auto *payload = dynamic_cast<const HelloReplyPayload*>(p);
if (payload == nullptr) {
return;
}
auto result = payload->result();
switch (result) {
case HelloReplyPayload::OK:
username_ = QString::fromStdString(payload->username());
state_ = Greeted;
emit greeted();
break;
case HelloReplyPayload::BAD_USERNAME:
emit errorOccurred(BadUsername);
break;
case HelloReplyPayload::USERNAME_IN_USE:
emit errorOccurred(RepeatedUsername);
break;
case HelloReplyPayload::SERVER_LOAD:
emit errorOccurred(ServerLoad);
break;
default:
// not expected
break;
}
if (state_ == Greeting) {
state_ = Ungreeted;
}
}
void DSConvoClientConnection::handleMessageBroadcast(const google::protobuf::Message *p)
{
using DSConvoProtocol::MessageBroadcastPayload;
if (state_ != Greeted) {
return;
}
const auto *payload = dynamic_cast<const MessageBroadcastPayload*>(p);
if (payload == nullptr) {
return;
}
DSConvo::Protocol::MessageBroadcast m;
m.username = QString::fromStdString(payload->username());
m.displayName = QString::fromStdString(payload->display_name());
m.message = QString::fromStdString(payload->msg());
emit messaged(m);
}
void DSConvoClientConnection::streamMessageParsed(DSConvoStream::ParsedMessage &message)
{
using DSConvoProtocol::DSConvoMessage;
auto type = message.first;
QScopedPointer<google::protobuf::Message> untypedPayload(message.second);
switch (type) {
case DSConvoMessage::HELLO_REPLY:
handleHelloReply(untypedPayload.get());
break;
case DSConvoMessage::MESSAGE_BROADCAST:
handleMessageBroadcast(untypedPayload.get());
break;
default:
qDebug("[DEBUG] [DSConvoClientConnection::streamMessageParsed] "
"client received bad message");
return;
}
}