knotesnetrecv.cpp
00001 /******************************************************************* 00002 KNotes -- Notes for the KDE project 00003 00004 Copyright (c) 2003, Daniel Martin <daniel.martin@pirack.com> 00005 2004, 2006, Michael Brade <brade@kde.org> 00006 00007 This program is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU General Public License 00009 as published by the Free Software Foundation; either version 2 00010 of the License, or (at your option) any later version. 00011 00012 This program is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with this program; if not, write to the Free Software 00019 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00020 00021 In addition, as a special exception, the copyright holders give 00022 permission to link the code of this program with any edition of 00023 the TQt library by Trolltech AS, Norway (or with modified versions 00024 of TQt that use the same license as TQt), and distribute linked 00025 combinations including the two. You must obey the GNU General 00026 Public License in all respects for all of the code used other than 00027 TQt. If you modify this file, you may extend this exception to 00028 your version of the file, but you are not obligated to do so. If 00029 you do not wish to do so, delete this exception statement from 00030 your version. 00031 *******************************************************************/ 00032 00033 #include <tqtimer.h> 00034 #include <tqdatetime.h> 00035 #include <tqregexp.h> 00036 #include <tqcstring.h> 00037 00038 #include <kdebug.h> 00039 #include <kbufferedsocket.h> 00040 #include <tdesocketaddress.h> 00041 #include <tdelocale.h> 00042 #include <tdeglobal.h> 00043 00044 #include "knotesnetrecv.h" 00045 00046 // Maximum note size in chars we are going to accept, 00047 // to prevent "note floods". 00048 #define MAXBUFFER 4096 00049 00050 // Maximum time we are going to wait between data receptions, 00051 // to prevent memory and connection floods. In milliseconds. 00052 #define MAXTIME 10000 00053 00054 // Small buffer's size 00055 #define SBSIZE 512 00056 00057 using namespace KNetwork; 00058 00059 00060 KNotesNetworkReceiver::KNotesNetworkReceiver( TDEBufferedSocket *s ) 00061 : TQObject(), 00062 m_buffer( new TQByteArray() ), m_sock( s ) 00063 { 00064 TQString date = TDEGlobal::locale()->formatDateTime( TQDateTime::currentDateTime(), true, false ); 00065 00066 // Add the remote IP or hostname and the date to the title, to help the 00067 // user guess who wrote it. 00068 m_titleAddon = TQString(" [%1, %2]") 00069 .arg( m_sock->peerAddress().nodeName() ) 00070 .arg( date ); 00071 00072 // Setup the communications 00073 connect( m_sock, TQT_SIGNAL(readyRead()), TQT_SLOT(slotDataAvailable()) ); 00074 connect( m_sock, TQT_SIGNAL(closed()), TQT_SLOT(slotConnectionClosed()) ); 00075 connect( m_sock, TQT_SIGNAL(gotError( int )), TQT_SLOT(slotError( int )) ); 00076 00077 m_sock->enableRead( true ); 00078 00079 // Setup the timer 00080 m_timer = new TQTimer( this, "m_timer" ); 00081 connect( m_timer, TQT_SIGNAL(timeout()), TQT_SLOT(slotReceptionTimeout()) ); 00082 m_timer->start( MAXTIME, true ); 00083 } 00084 00085 KNotesNetworkReceiver::~KNotesNetworkReceiver() 00086 { 00087 delete m_buffer; 00088 delete m_sock; 00089 } 00090 00091 void KNotesNetworkReceiver::slotDataAvailable() 00092 { 00093 char smallBuffer[SBSIZE]; 00094 int smallBufferLen; 00095 00096 do 00097 { 00098 // Append to "big buffer" only if we have some space left. 00099 int curLen = m_buffer->count(); 00100 00101 smallBufferLen = m_sock->readBlock( smallBuffer, SBSIZE ); 00102 00103 // Limit max transfer over buffer, to avoid overflow. 00104 smallBufferLen = kMin( smallBufferLen, MAXBUFFER - curLen ); 00105 00106 if ( smallBufferLen > 0 ) 00107 { 00108 m_buffer->resize( curLen + smallBufferLen ); 00109 memcpy( m_buffer->data() + curLen, smallBuffer, smallBufferLen ); 00110 } 00111 } 00112 while ( smallBufferLen == SBSIZE ); 00113 00114 // If we are overflowing, close connection. 00115 if ( m_buffer->count() == MAXBUFFER ) 00116 m_sock->close(); 00117 else 00118 m_timer->changeInterval( MAXTIME ); 00119 } 00120 00121 void KNotesNetworkReceiver::slotReceptionTimeout() 00122 { 00123 m_sock->close(); 00124 } 00125 00126 void KNotesNetworkReceiver::slotConnectionClosed() 00127 { 00128 if ( m_timer->isActive() ) 00129 { 00130 TQString noteText = TQString( *m_buffer ).stripWhiteSpace(); 00131 00132 // First line is the note title or, in case of ATnotes, the id 00133 int pos = noteText.find( TQRegExp("[\r\n]") ); 00134 TQString noteTitle = noteText.left( pos ).stripWhiteSpace() + m_titleAddon; 00135 00136 noteText = noteText.mid( pos ).stripWhiteSpace(); 00137 00138 if ( !noteText.isEmpty() ) 00139 emit sigNoteReceived( noteTitle, noteText ); 00140 } 00141 00142 deleteLater(); 00143 } 00144 00145 void KNotesNetworkReceiver::slotError( int err ) 00146 { 00147 kdWarning() << k_funcinfo 00148 << m_sock->KNetwork::TDESocketBase::errorString( static_cast<TDESocketBase::SocketError>(err) ) 00149 << endl; 00150 } 00151 00152 #include "knotesnetrecv.moc"