29 #include <sys/types.h> 40 #include "tdeio/connection.h" 43 #include <tqsocketnotifier.h> 45 #if defined(__OpenBSD__) || defined(__FreeBSD__) 46 #define __progname getprogname() 48 extern char *__progname;
51 using namespace TDEIO;
62 tasks.setAutoDelete(
true);
65 Connection::~Connection()
74 notifier->setEnabled(
false);
81 notifier->setEnabled(
true);
105 if (!
inited() || tasks.count() > 0) {
106 Task *task =
new Task();
115 void Connection::dequeue()
120 while (tasks.count())
123 Task *task = tasks.take();
124 sendnow( task->cmd, task->data );
133 #ifdef Q_OS_UNIX //TODO: not yet available on WIN32 136 fd_in = socket->socket();
137 f_out = KDE_fdopen( socket->socket(),
"wb" );
139 if (receiver && ( fd_in != -1 )) {
140 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
144 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
154 f_out = KDE_fdopen( fd_out,
"wb" );
155 if (receiver && ( fd_in != -1 )) {
156 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
160 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
166 void Connection::connect(TQObject *_receiver,
const char *_member)
168 receiver = _receiver;
172 if (receiver && (fd_in != -1 )) {
173 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
176 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
186 if (data.size() > 0xffffff)
189 static char buffer[ 64 ];
190 sprintf( buffer,
"%6x_%2x_", data.size(), _cmd );
192 size_t n = fwrite( buffer, 1, 10, f_out );
195 kdError(7017) <<
"Could not send header (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
199 n = fwrite( data.data(), 1, data.size(), f_out );
201 if ( n != data.size() ) {
202 kdError(7017) <<
"Could not write data (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
206 if (fflush( f_out )) {
207 kdError(7017) <<
"Could not write data (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
217 kdError(7017) <<
"read: not yet inited (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
221 static char buffer[ 10 ];
224 ssize_t n =
::read( fd_in, buffer, 10);
225 if ( n == -1 && errno == EINTR )
229 kdError(7017) <<
"Header read failed, errno=" << errno <<
" (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
234 kdError(7017) <<
"Header has invalid size (" << n <<
") (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
242 while( *p ==
' ' ) p++;
243 long int len = strtol( p, 0L, 16 );
246 while( *p ==
' ' ) p++;
247 long int cmd = strtol( p, 0L, 16 );
252 size_t bytesToGo = len;
253 size_t bytesRead = 0;
255 n =
::read(fd_in, data.data()+bytesRead, bytesToGo);
260 kdError(7017) <<
"Data read failed, errno=" << errno <<
" (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
264 kdError(7017) <<
"Connection ended unexpectedly (" << n <<
"/" << bytesToGo <<
") (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
278 #include "connection.moc" void close()
Closes the connection.
bool sendnow(int _cmd, const TQByteArray &data)
Sends the given command immediately.
A namespace for TDEIO globals.
int read(int *_cmd, TQByteArray &data)
Receive data.
Connection()
Creates a new connection.
void suspend()
Don't handle incoming data until resumed.
bool inited() const
Checks whether the connection has been initialized.
void send(int cmd, const TQByteArray &arr=TQByteArray())
Sends/queues the given command to be sent.
void init(TDESocket *sock)
Initialize this connection to use the given socket.
void resume()
Resume handling of incoming data.