• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdenetwork API Reference
  • KDE Home
  • Contact Us
 

kopete/libkopete

  • sources
  • kde-4.14
  • kdenetwork
  • kopete
  • libkopete
kopetesockettimeoutwatcher.cpp
Go to the documentation of this file.
1 /*
2  kopetesockettimeoutwatcher.cpp - Kopete Socket Timeout Watcher
3 
4  Copyright (c) 2009 Roman Jarosz <kedgedev@centrum.cz>
5  Kopete (c) 2009 by the Kopete developers <kopete-devel@kde.org>
6 
7  *************************************************************************
8  * *
9  * This library is free software; you can redistribute it and/or *
10  * modify it under the terms of the GNU Lesser General Public *
11  * License as published by the Free Software Foundation; either *
12  * version 2 of the License, or (at your option) any later version. *
13  * *
14  *************************************************************************
15 */
16 #include "kopetesockettimeoutwatcher.h"
17 
18 #ifdef Q_OS_LINUX
19 #include <sys/socket.h>
20 #include <sys/types.h>
21 #include <netinet/tcp.h>
22 #endif
23 
24 #include <kdebug.h>
25 
26 #include <QAbstractSocket>
27 #include <QTimer>
28 #include <QHostAddress>
29 
30 namespace Kopete
31 {
32 
33 QSet<QAbstractSocket*> SocketTimeoutWatcher::watchedSocketSet;
34 
35 SocketTimeoutWatcher* SocketTimeoutWatcher::watch( QAbstractSocket* socket, quint32 msecTimeout )
36 {
37  if (watchedSocketSet.contains(socket))
38  {
39  kDebug() << "Socket is already being watched " << socket;
40  return 0;
41  }
42 
43  return new Kopete::SocketTimeoutWatcher( socket, msecTimeout );
44 }
45 
46 SocketTimeoutWatcher::SocketTimeoutWatcher( QAbstractSocket* socket, quint32 msecTimeout )
47 : QObject(socket), mSocket(socket), mActive(true), mTimeoutThreshold(msecTimeout)
48 {
49  watchedSocketSet.insert( mSocket );
50  mAckCheckTimer = new QTimer();
51 
52  connect( socket, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWritten()) );
53  connect( socket, SIGNAL(disconnected()), mAckCheckTimer, SLOT(stop()) );
54  connect( socket, SIGNAL(error(QAbstractSocket::SocketError)), mAckCheckTimer, SLOT(stop()) );
55 
56  connect( mAckCheckTimer, SIGNAL(timeout()), this, SLOT(ackTimeoutCheck()) );
57  mAckCheckTimer->setInterval( mTimeoutThreshold );
58 }
59 
60 SocketTimeoutWatcher::~SocketTimeoutWatcher()
61 {
62  watchedSocketSet.remove( mSocket );
63  delete mAckCheckTimer;
64 }
65 
66 void SocketTimeoutWatcher::bytesWritten()
67 {
68  if ( !mActive )
69  return;
70 
71  if ( mSocket->socketDescriptor() != -1 )
72  {
73  if ( !mAckCheckTimer->isActive() )
74  mAckCheckTimer->start();
75  }
76  else
77  {
78  if ( mAckCheckTimer->isActive() )
79  mAckCheckTimer->stop();
80  }
81 
82 }
83 
84 void SocketTimeoutWatcher::ackTimeoutCheck()
85 {
86  const int sDesc = mSocket->socketDescriptor();
87  if ( sDesc != -1 )
88  {
89 #ifdef Q_OS_LINUX
90  struct tcp_info info;
91  int info_length = sizeof(info);
92  if ( getsockopt( sDesc, SOL_TCP, TCP_INFO, (void*)&info, (socklen_t*)&info_length ) == 0 )
93  {
94  if ( info.tcpi_last_ack_recv < info.tcpi_last_data_sent || info.tcpi_last_data_sent <= 0 )
95  {
96  mAckCheckTimer->stop();
97  }
98  else if ( info.tcpi_last_ack_recv > info.tcpi_last_data_sent && info.tcpi_last_data_sent > mTimeoutThreshold )
99  {
100  kWarning() << "Connection timeout for " << mSocket->peerAddress();
101  mAckCheckTimer->stop();
102  emit error( QAbstractSocket::RemoteHostClosedError );
103  emit errorInt( QAbstractSocket::RemoteHostClosedError );
104  mSocket->abort();
105  }
106  return;
107  }
108 #endif
109 
110  if ( mActive )
111  {
112  mAckCheckTimer->stop();
113  mActive = false;
114  kWarning() << "Timeout watcher not active for " << mSocket->peerAddress();
115  }
116  }
117  else
118  {
119  mAckCheckTimer->stop();
120  }
121 }
122 
123 }
124 
125 #include "kopetesockettimeoutwatcher.moc"
QTimer::setInterval
void setInterval(int msec)
QAbstractSocket::peerAddress
QHostAddress peerAddress() const
Kopete::SocketTimeoutWatcher::error
void error(QAbstractSocket::SocketError socketError)
Emitted when timeout is reached.
QAbstractSocket::abort
void abort()
QSet::insert
const_iterator insert(const T &value)
QAbstractSocket
Kopete::SocketTimeoutWatcher
Definition: kopetesockettimeoutwatcher.h:31
Kopete::SocketTimeoutWatcher::~SocketTimeoutWatcher
~SocketTimeoutWatcher()
Definition: kopetesockettimeoutwatcher.cpp:60
Kopete::SocketTimeoutWatcher::watch
static SocketTimeoutWatcher * watch(QAbstractSocket *socket, quint32 msecTimeout=15000)
Returns new SocketTimeoutWatcher or 0 if SocketTimeoutWatcher was already created for this socket...
Definition: kopetesockettimeoutwatcher.cpp:35
QTimer
QObject
kopetesockettimeoutwatcher.h
QSet< QAbstractSocket * >
QTimer::stop
void stop()
Kopete::SocketTimeoutWatcher::errorInt
void errorInt(int socketError)
QSet::contains
bool contains(const T &value) const
QSet::remove
bool remove(const T &value)
QTimer::start
void start(int msec)
QTimer::isActive
bool isActive() const
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QAbstractSocket::socketDescriptor
int socketDescriptor() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:29:19 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kopete/libkopete

Skip menu "kopete/libkopete"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdenetwork API Reference

Skip menu "kdenetwork API Reference"
  • kget
  • kopete
  •   kopete
  •   libkopete
  • krdc
  • krfb

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal