source: trunk/examples/network/network-chat/connection.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 7.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include "connection.h"
42
43#include <QtNetwork>
44
45static const int TransferTimeout = 30 * 1000;
46static const int PongTimeout = 60 * 1000;
47static const int PingInterval = 5 * 1000;
48static const char SeparatorToken = ' ';
49
50Connection::Connection(QObject *parent)
51 : QTcpSocket(parent)
52{
53 greetingMessage = tr("undefined");
54 username = tr("unknown");
55 state = WaitingForGreeting;
56 currentDataType = Undefined;
57 numBytesForCurrentDataType = -1;
58 transferTimerId = 0;
59 isGreetingMessageSent = false;
60 pingTimer.setInterval(PingInterval);
61
62 QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead()));
63 QObject::connect(this, SIGNAL(disconnected()), &pingTimer, SLOT(stop()));
64 QObject::connect(&pingTimer, SIGNAL(timeout()), this, SLOT(sendPing()));
65 QObject::connect(this, SIGNAL(connected()),
66 this, SLOT(sendGreetingMessage()));
67}
68
69QString Connection::name() const
70{
71 return username;
72}
73
74void Connection::setGreetingMessage(const QString &message)
75{
76 greetingMessage = message;
77}
78
79bool Connection::sendMessage(const QString &message)
80{
81 if (message.isEmpty())
82 return false;
83
84 QByteArray msg = message.toUtf8();
85 QByteArray data = "MESSAGE " + QByteArray::number(msg.size()) + ' ' + msg;
86 return write(data) == data.size();
87}
88
89void Connection::timerEvent(QTimerEvent *timerEvent)
90{
91 if (timerEvent->timerId() == transferTimerId) {
92 abort();
93 killTimer(transferTimerId);
94 transferTimerId = 0;
95 }
96}
97
98void Connection::processReadyRead()
99{
100 if (state == WaitingForGreeting) {
101 if (!readProtocolHeader())
102 return;
103 if (currentDataType != Greeting) {
104 abort();
105 return;
106 }
107 state = ReadingGreeting;
108 }
109
110 if (state == ReadingGreeting) {
111 if (!hasEnoughData())
112 return;
113
114 buffer = read(numBytesForCurrentDataType);
115 if (buffer.size() != numBytesForCurrentDataType) {
116 abort();
117 return;
118 }
119
120 username = QString(buffer) + '@' + peerAddress().toString() + ':'
121 + QString::number(peerPort());
122 currentDataType = Undefined;
123 numBytesForCurrentDataType = 0;
124 buffer.clear();
125
126 if (!isValid()) {
127 abort();
128 return;
129 }
130
131 if (!isGreetingMessageSent)
132 sendGreetingMessage();
133
134 pingTimer.start();
135 pongTime.start();
136 state = ReadyForUse;
137 emit readyForUse();
138 }
139
140 do {
141 if (currentDataType == Undefined) {
142 if (!readProtocolHeader())
143 return;
144 }
145 if (!hasEnoughData())
146 return;
147 processData();
148 } while (bytesAvailable() > 0);
149}
150
151void Connection::sendPing()
152{
153 if (pongTime.elapsed() > PongTimeout) {
154 abort();
155 return;
156 }
157
158 write("PING 1 p");
159}
160
161void Connection::sendGreetingMessage()
162{
163 QByteArray greeting = greetingMessage.toUtf8();
164 QByteArray data = "GREETING " + QByteArray::number(greeting.size()) + ' ' + greeting;
165 if (write(data) == data.size())
166 isGreetingMessageSent = true;
167}
168
169int Connection::readDataIntoBuffer(int maxSize)
170{
171 if (maxSize > MaxBufferSize)
172 return 0;
173
174 int numBytesBeforeRead = buffer.size();
175 if (numBytesBeforeRead == MaxBufferSize) {
176 abort();
177 return 0;
178 }
179
180 while (bytesAvailable() > 0 && buffer.size() < maxSize) {
181 buffer.append(read(1));
182 if (buffer.endsWith(SeparatorToken))
183 break;
184 }
185 return buffer.size() - numBytesBeforeRead;
186}
187
188int Connection::dataLengthForCurrentDataType()
189{
190 if (bytesAvailable() <= 0 || readDataIntoBuffer() <= 0
191 || !buffer.endsWith(SeparatorToken))
192 return 0;
193
194 buffer.chop(1);
195 int number = buffer.toInt();
196 buffer.clear();
197 return number;
198}
199
200bool Connection::readProtocolHeader()
201{
202 if (transferTimerId) {
203 killTimer(transferTimerId);
204 transferTimerId = 0;
205 }
206
207 if (readDataIntoBuffer() <= 0) {
208 transferTimerId = startTimer(TransferTimeout);
209 return false;
210 }
211
212 if (buffer == "PING ") {
213 currentDataType = Ping;
214 } else if (buffer == "PONG ") {
215 currentDataType = Pong;
216 } else if (buffer == "MESSAGE ") {
217 currentDataType = PlainText;
218 } else if (buffer == "GREETING ") {
219 currentDataType = Greeting;
220 } else {
221 currentDataType = Undefined;
222 abort();
223 return false;
224 }
225
226 buffer.clear();
227 numBytesForCurrentDataType = dataLengthForCurrentDataType();
228 return true;
229}
230
231bool Connection::hasEnoughData()
232{
233 if (transferTimerId) {
234 QObject::killTimer(transferTimerId);
235 transferTimerId = 0;
236 }
237
238 if (numBytesForCurrentDataType <= 0)
239 numBytesForCurrentDataType = dataLengthForCurrentDataType();
240
241 if (bytesAvailable() < numBytesForCurrentDataType
242 || numBytesForCurrentDataType <= 0) {
243 transferTimerId = startTimer(TransferTimeout);
244 return false;
245 }
246
247 return true;
248}
249
250void Connection::processData()
251{
252 buffer = read(numBytesForCurrentDataType);
253 if (buffer.size() != numBytesForCurrentDataType) {
254 abort();
255 return;
256 }
257
258 switch (currentDataType) {
259 case PlainText:
260 emit newMessage(username, QString::fromUtf8(buffer));
261 break;
262 case Ping:
263 write("PONG 1 p");
264 break;
265 case Pong:
266 pongTime.restart();
267 break;
268 default:
269 break;
270 }
271
272 currentDataType = Undefined;
273 numBytesForCurrentDataType = 0;
274 buffer.clear();
275}
Note: See TracBrowser for help on using the repository browser.