knotes

knotesnetrecv.cpp
1 /*******************************************************************
2  KNotes -- Notes for the KDE project
3 
4  Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com>
5  2004, 2006, Michael Brade <brade@kde.org>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License
9  as published by the Free Software Foundation; either version 2
10  of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the TQt library by Trolltech AS, Norway (or with modified versions
24  of TQt that use the same license as TQt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  TQt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 *******************************************************************/
32 
33 #include <tqtimer.h>
34 #include <tqdatetime.h>
35 #include <tqregexp.h>
36 #include <tqcstring.h>
37 
38 #include <kdebug.h>
39 #include <kbufferedsocket.h>
40 #include <tdesocketaddress.h>
41 #include <tdelocale.h>
42 #include <tdeglobal.h>
43 
44 #include "knotesnetrecv.h"
45 
46 // Maximum note size in chars we are going to accept,
47 // to prevent "note floods".
48 #define MAXBUFFER 4096
49 
50 // Maximum time we are going to wait between data receptions,
51 // to prevent memory and connection floods. In milliseconds.
52 #define MAXTIME 10000
53 
54 // Small buffer's size
55 #define SBSIZE 512
56 
57 using namespace KNetwork;
58 
59 
60 KNotesNetworkReceiver::KNotesNetworkReceiver( TDEBufferedSocket *s )
61  : TQObject(),
62  m_buffer( new TQByteArray() ), m_sock( s )
63 {
64  TQString date = TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime(), true, false );
65 
66  // Add the remote IP or hostname and the date to the title, to help the
67  // user guess who wrote it.
68  m_titleAddon = TQString(" [%1, %2]")
69  .arg( m_sock->peerAddress().nodeName() )
70  .arg( date );
71 
72  // Setup the communications
73  connect( m_sock, TQ_SIGNAL(readyRead()), TQ_SLOT(slotDataAvailable()) );
74  connect( m_sock, TQ_SIGNAL(closed()), TQ_SLOT(slotConnectionClosed()) );
75  connect( m_sock, TQ_SIGNAL(gotError( int )), TQ_SLOT(slotError( int )) );
76 
77  m_sock->enableRead( true );
78 
79  // Setup the timer
80  m_timer = new TQTimer( this, "m_timer" );
81  connect( m_timer, TQ_SIGNAL(timeout()), TQ_SLOT(slotReceptionTimeout()) );
82  m_timer->start( MAXTIME, true );
83 }
84 
85 KNotesNetworkReceiver::~KNotesNetworkReceiver()
86 {
87  delete m_buffer;
88  delete m_sock;
89 }
90 
91 void KNotesNetworkReceiver::slotDataAvailable()
92 {
93  char smallBuffer[SBSIZE];
94  int smallBufferLen;
95 
96  do
97  {
98  // Append to "big buffer" only if we have some space left.
99  int curLen = m_buffer->count();
100 
101  smallBufferLen = m_sock->readBlock( smallBuffer, SBSIZE );
102 
103  // Limit max transfer over buffer, to avoid overflow.
104  smallBufferLen = kMin( smallBufferLen, MAXBUFFER - curLen );
105 
106  if ( smallBufferLen > 0 )
107  {
108  m_buffer->resize( curLen + smallBufferLen );
109  memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen );
110  }
111  }
112  while ( smallBufferLen == SBSIZE );
113 
114  // If we are overflowing, close connection.
115  if ( m_buffer->count() == MAXBUFFER )
116  m_sock->close();
117  else
118  m_timer->changeInterval( MAXTIME );
119 }
120 
121 void KNotesNetworkReceiver::slotReceptionTimeout()
122 {
123  m_sock->close();
124 }
125 
126 void KNotesNetworkReceiver::slotConnectionClosed()
127 {
128  if ( m_timer->isActive() )
129  {
130  TQString noteText = TQString( *m_buffer ).stripWhiteSpace();
131 
132  // First line is the note title or, in case of ATnotes, the id
133  int pos = noteText.find( TQRegExp("[\r\n]") );
134  TQString noteTitle = noteText.left( pos ).stripWhiteSpace() + m_titleAddon;
135 
136  noteText = noteText.mid( pos ).stripWhiteSpace();
137 
138  if ( !noteText.isEmpty() )
139  emit sigNoteReceived( noteTitle, noteText );
140  }
141 
142  deleteLater();
143 }
144 
145 void KNotesNetworkReceiver::slotError( int err )
146 {
147  kdWarning() << k_funcinfo
148  << m_sock->KNetwork::TDESocketBase::errorString( static_cast<TDESocketBase::SocketError>(err) )
149  << endl;
150 }
151 
152 #include "knotesnetrecv.moc"