29 #include <sys/types.h> 40 #include "kio/connection.h" 43 #include <tqsocketnotifier.h> 45 #if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__NetBSD__) 46 #define __progname getprogname() 47 #elif defined(_GNU_SOURCE) && defined(__GLIBC__) 48 #define __progname program_invocation_short_name 50 extern char *__progname;
64 tasks.setAutoDelete(
true);
67 Connection::~Connection()
76 notifier->setEnabled(
false);
83 notifier->setEnabled(
true);
107 if (!
inited() || tasks.count() > 0) {
108 Task *task =
new Task();
117 void Connection::dequeue()
122 while (tasks.count())
125 Task *task = tasks.take();
126 sendnow( task->cmd, task->data );
135 #ifdef Q_OS_UNIX //TODO: not yet available on WIN32 138 fd_in = socket->socket();
139 f_out = KDE_fdopen( socket->socket(),
"wb" );
141 if (receiver && ( fd_in != -1 )) {
142 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
146 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
156 f_out = KDE_fdopen( fd_out,
"wb" );
157 if (receiver && ( fd_in != -1 )) {
158 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
162 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
168 void Connection::connect(TQObject *_receiver,
const char *_member)
170 receiver = _receiver;
174 if (receiver && (fd_in != -1 )) {
175 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
178 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
188 if (data.size() > 0xffffff)
191 static char buffer[ 64 ];
192 sprintf( buffer,
"%6x_%2x_", data.size(), _cmd );
194 size_t n = fwrite( buffer, 1, 10, f_out );
197 kdError(7017) <<
"Could not send header (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
201 n = fwrite( data.data(), 1, data.size(), f_out );
203 if ( n != data.size() ) {
204 kdError(7017) <<
"Could not write data (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
208 if (fflush( f_out )) {
209 kdError(7017) <<
"Could not write data (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
219 kdError(7017) <<
"read: not yet inited (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
223 static char buffer[ 10 ];
226 ssize_t n =
::read( fd_in, buffer, 10);
227 if ( n == -1 && errno == EINTR )
231 kdError(7017) <<
"Header read failed, errno=" << errno <<
" (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
236 kdError(7017) <<
"Header has invalid size (" << n <<
") (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
244 while( *p ==
' ' ) p++;
245 long int len = strtol( p, 0L, 16 );
248 while( *p ==
' ' ) p++;
249 long int cmd = strtol( p, 0L, 16 );
254 size_t bytesToGo = len;
255 size_t bytesRead = 0;
257 n =
::read(fd_in, data.data()+bytesRead, bytesToGo);
262 kdError(7017) <<
"Data read failed, errno=" << errno <<
" (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
266 kdError(7017) <<
"Connection ended unexpectedly (" << n <<
"/" << bytesToGo <<
") (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
280 #include "connection.moc" bool sendnow(int _cmd, const TQByteArray &data)
Sends the given command immediately.
int read(int *_cmd, TQByteArray &data)
Receive data.
void close()
Closes the connection.
A namespace for KIO globals.
void init(KSocket *sock)
Initialize this connection to use the given socket.
bool inited() const
Checks whether the connection has been initialized.
void suspend()
Don't handle incoming data until resumed.
void resume()
Resume handling of incoming data.
Connection()
Creates a new connection.
void send(int cmd, const TQByteArray &arr=TQByteArray())
Sends/queues the given command to be sent.