X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcore%2Fnetworkconnection.cpp;h=f5e6a367b04a3d58973b03a3c7a209a8bfd9b8c9;hp=530ee32ba9c82b66594fc7e79b4f6a5399ae071f;hb=c30f8eb1f1d360284b38016655cdb6a3e40db8ed;hpb=fd1833906d6d9d3a67c9aa92bffa35d1024e41a9 diff --git a/src/core/networkconnection.cpp b/src/core/networkconnection.cpp index 530ee32b..f5e6a367 100644 --- a/src/core/networkconnection.cpp +++ b/src/core/networkconnection.cpp @@ -45,6 +45,7 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session) _userInputHandler(new UserInputHandler(this)), _ctcpHandler(new CtcpHandler(this)), _autoReconnectCount(0), + _quitRequested(false), _previousConnectionAttemptFailed(false), _lastUsedServerlistIndex(0), @@ -54,26 +55,20 @@ NetworkConnection::NetworkConnection(Network *network, CoreSession *session) _autoWhoInterval(90), _autoWhoNickLimit(0), // unlimited _autoWhoDelay(3), - + // TokenBucket to avaid sending too much at once _messagesPerSecond(1), _burstSize(5), - _tokenBucket(5), // init with a full bucket - - // TODO: - // should be 510 (2 bytes are added when writing to the socket) - // maxMsgSize is 510 minus the hostmask which will be added by the server - _maxMsgSize(450) + _tokenBucket(5) // init with a full bucket { _autoReconnectTimer.setSingleShot(true); - + _socketCloseTimer.setSingleShot(true); + connect(&_socketCloseTimer, SIGNAL(timeout()), this, SLOT(socketCloseTimeout())); + _autoWhoTimer.setInterval(_autoWhoDelay * 1000); - _autoWhoTimer.setSingleShot(false); _autoWhoCycleTimer.setInterval(_autoWhoInterval * 1000); - _autoWhoCycleTimer.setSingleShot(false); - + _tokenBucketTimer.start(_messagesPerSecond * 1000); - _tokenBucketTimer.setSingleShot(false); QHash channels = coreSession()->persistentChannels(networkId()); foreach(QString chan, channels.keys()) { @@ -260,12 +255,13 @@ void NetworkConnection::disconnectFromIrc(bool requested) { setConnectionState(Network::Disconnected); socketDisconnected(); } else { - socket.disconnectFromHost(); + _socketCloseTimer.start(10000); // the irc server has 10 seconds to close the socket } - if(requested) { - emit quitRequested(networkId()); - } + // this flag triggers quitRequested() once the socket is closed + // it is needed to determine whether or not the connection needs to be + // in the automatic session restore. + _quitRequested = requested; } void NetworkConnection::socketHasData() { @@ -369,18 +365,26 @@ void NetworkConnection::socketStateChanged(QAbstractSocket::SocketState socketSt setConnectionState(state); } +void NetworkConnection::socketCloseTimeout() { + socket.disconnectFromHost(); +} + void NetworkConnection::socketDisconnected() { _autoWhoCycleTimer.stop(); _autoWhoTimer.stop(); _autoWhoQueue.clear(); _autoWhoInProgress.clear(); + _socketCloseTimer.stop(); + network()->setConnected(false); emit disconnected(networkId()); if(_autoReconnectCount != 0) { setConnectionState(Network::Reconnecting); if(_autoReconnectCount == network()->autoReconnectRetries()) doAutoReconnect(); // first try is immediate else _autoReconnectTimer.start(); + } else if(_quitRequested) { + emit quitRequested(networkId()); } } @@ -424,34 +428,76 @@ void NetworkConnection::fillBucketAndProcessQueue() { } } -void NetworkConnection::putCmd(const QString &cmd, const QVariantList ¶ms, const QByteArray &prefix) { - QByteArray msg; - if(!prefix.isEmpty()) - msg += ":" + prefix + " "; - msg += cmd.toUpper().toAscii(); +// returns 0 if the message will not be chopped by the irc server or number of chopped bytes if message is too long +int NetworkConnection::lastParamOverrun(const QString &cmd, const QList ¶ms) { + //the server will pass our message that trunkated to 512 bytes including CRLF with the following format: + // ":prefix COMMAND param0 param1 :lastparam" + // where prefix = "nickname!user@host" + // that means that the last message can be as long as: + // 512 - nicklen - userlen - hostlen - commandlen - sum(param[0]..param[n-1])) - 2 (for CRLF) - 4 (":!@" + 1space between prefix and command) - max(paramcount - 1, 0) (space for simple params) - 2 (space and colon for last param) + IrcUser *me = network()->me(); + int maxLen = 480 - cmd.toAscii().count(); // educated guess in case we don't know us (yet?) - for(int i = 0; i < params.size() - 1; i++) { - msg += " " + params[i].toByteArray(); + if(me) + maxLen = 512 - serverEncode(me->nick()).count() - serverEncode(me->user()).count() - serverEncode(me->host()).count() - cmd.toAscii().count() - 6; + + if(!params.isEmpty()) { + for(int i = 0; i < params.count() - 1; i++) { + maxLen -= (params[i].count() + 1); + } + maxLen -= 2; // " :" last param separator; + + if(params.last().count() > maxLen) { + return params.last().count() - maxLen; + } else { + return 0; + } + } else { + return 0; } - if(!params.isEmpty()) - msg += " :" + params.last().toByteArray(); +} +void NetworkConnection::putCmd(const QString &cmd, const QList ¶ms, const QByteArray &prefix) { + QByteArray msg; if(cmd == "PRIVMSG" && params.count() > 1) { - QByteArray msghead = "PRIVMSG " + params[0].toByteArray() + " :"; + int overrun = lastParamOverrun(cmd, params); + if(overrun) { + QList paramCopy1; + QList paramCopy2; + for(int i = 0; i < params.count() - 1; i++) { + paramCopy1 << params[i]; + paramCopy2 << params[i]; + } - while (msg.size() > _maxMsgSize) { + QByteArray lastPart = params.last(); QByteArray splitter(" .,-"); - int splitPosition = 0; + int maxSplitPos = params.last().count() - overrun; + int splitPos = -1; for(int i = 0; i < splitter.size(); i++) { - splitPosition = qMax(splitPosition, msg.lastIndexOf(splitter[i], _maxMsgSize)); + splitPos = qMax(splitPos, lastPart.lastIndexOf(splitter[i], maxSplitPos)); } - if(splitPosition < 300) { - splitPosition = _maxMsgSize; + + if(splitPos == -1) { + splitPos = maxSplitPos; } - putRawLine(msg.left(splitPosition)); - msg = msghead + msg.mid(splitPosition); + + paramCopy1 << lastPart.left(splitPos); + paramCopy2 << lastPart.mid(splitPos); + putCmd(cmd, paramCopy1, prefix); + putCmd(cmd, paramCopy2, prefix); + return; } } + + if(!prefix.isEmpty()) + msg += ":" + prefix + " "; + msg += cmd.toUpper().toAscii(); + + for(int i = 0; i < params.size() - 1; i++) { + msg += " " + params[i]; + } + if(!params.isEmpty()) + msg += " :" + params.last(); putRawLine(msg); }