-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocolhandler.cpp
173 lines (158 loc) · 4.36 KB
/
protocolhandler.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
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
#include <QDataStream>
#include "protocolhandler.h"
#include "packetnick.h"
#include "packetjoin.h"
#include "packetmessage.h"
#include "packetacknick.h"
ProtocolHandler::ProtocolHandler(QObject *parent) :
QObject(parent),
host_("127.0.0.1"),
port_("12345"),
nickname_("loki"),
currentChan_("#public"),
registered_(false)
{
connect(&sock_,SIGNAL(connected()),this,SLOT(slotConnected()));
connect(&timer_, SIGNAL(timeout()),this, SLOT(slotRead()));
connect(&sock_,
SIGNAL(error(QAbstractSocket::SocketError)),
this,
SLOT(slotError(QAbstractSocket::SocketError)));
timer_.start(100);
inBuf_.clear();
}
void ProtocolHandler::setHostAndPort(const QString& hostAdd, const QString& port)
{
host_ = hostAdd;
port_ = port;
}
void ProtocolHandler::connectToServer()
{
sock_.connectToHost(host_,port_.toInt());
}
void ProtocolHandler::disconnectFromServer()
{
sock_.disconnectFromHost();
}
void ProtocolHandler::sendMessage(const QString& mess)
{
if (mess.startsWith("/nick"))
{
QStringList list = mess.split(" ");
QString nick = list[1];
qDebug()<<"nick: "<<nick;
PacketNick pack(nick);
//test serialize in stream
QByteArray array;
QDataStream stream (&array, QIODevice::WriteOnly);
stream<<pack;
qDebug()<<"raw Packet: "<<array;
sendPacket(pack);
nickname_ = nick;
}
else if (mess.startsWith("/join"))
{
QStringList list = mess.split(" ");
QString channel = list[1];
qDebug()<<"channel: "<<channel;
PacketJoin pack(channel);
//test serialize in stream
QByteArray array;
QDataStream stream (&array, QIODevice::WriteOnly);
stream<<pack;
qDebug()<<"raw Packet: "<<array;
sendPacket(pack);
currentChan_ = channel;
}
else if(mess.startsWith("/p"))
{
QStringList list = mess.split(" ");
if (list.size() >= 3)
{
QString dest = list[1];
list.pop_front();
list.pop_front();
QString message = list.join(" ");
qDebug()<<"Private msg from: "<<nickname_<<"to: "<<dest<<" "<<message;
PacketMessage pack(nickname_,dest,message);
sendPacket(pack);
}
}
else
{
PacketMessage pack(nickname_, currentChan_, mess);
sendPacket(pack);
}
}
void ProtocolHandler::sendPacket(Packet& packet)
{
if (sock_.state() == QTcpSocket::ConnectedState)
{
sock_<<packet;
}
}
void ProtocolHandler::slotConnected()
{
emit connected();
}
QString ProtocolHandler::getTextMsg()
{
return QString("Message received");
}
void ProtocolHandler::slotRead()
{
if (sock_.bytesAvailable() > 0)
{
char header[12];
sock_.read(header,12);
Packet::message_header_t* h = reinterpret_cast<Packet::message_header_t*>(header);
int datalen = h->length-Packet::HEADER_LENGTH;
char * payload = new char[datalen];
sock_.read(payload,datalen);
switch((Packet::PacketType_t)h->type)
{
case Packet::ACK_CHANGE_NICK:
{
PacketAckNick pack("");
pack.setHeader(*h);
pack.setPayload(payload);
registered_ = pack.acked();
if (registered_)
{
QString mess = QString("NickName %1 registered on server").arg(nickname_);
emit NewMessage(mess);
}
}
break;
case Packet::MESSAGE:
{
PacketMessage pack("","","");
pack.setHeader(*h);
pack.setPayload(payload);
QString mess = QString("%0: %1").arg(pack.from()).arg(pack.getMessage());
if (mess.size() > 0)
{
emit NewMessage(mess);
}
}
default:
break;
}
}
}
void ProtocolHandler::slotError(QAbstractSocket::SocketError e)
{
switch (e)
{
case QAbstractSocket::RemoteHostClosedError:
qDebug()<<"Connection reset by peer";
disconnectFromServer();
connectToServer();
break;
default:
qDebug()<<"Unknown socket error: "<<e;
disconnectFromServer();
connectToServer();
break;
}
}