29 #include <sys/types.h> 30 #include <sys/signal.h> 41 #include "tdeio/connection.h" 44 #include <tqsocketnotifier.h> 46 #if defined(__OpenBSD__) || defined(__FreeBSD__) 47 #define __progname getprogname() 49 extern char *__progname;
52 using namespace TDEIO;
63 tasks.setAutoDelete(
true);
66 Connection::~Connection()
75 notifier->setEnabled(
false);
82 notifier->setEnabled(
true);
106 if (!
inited() || tasks.count() > 0) {
107 Task *task =
new Task();
116 void Connection::dequeue()
121 while (tasks.count())
124 Task *task = tasks.take();
125 sendnow( task->cmd, task->data );
134 #ifdef Q_OS_UNIX //TODO: not yet available on WIN32 137 fd_in = socket->socket();
138 f_out = KDE_fdopen( socket->socket(),
"wb" );
140 if (receiver && ( fd_in != -1 )) {
141 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
145 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
155 f_out = KDE_fdopen( fd_out,
"wb" );
156 if (receiver && ( fd_in != -1 )) {
157 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
161 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
167 void Connection::connect(TQObject *_receiver,
const char *_member)
169 receiver = _receiver;
173 if (receiver && (fd_in != -1 )) {
174 notifier =
new TQSocketNotifier(fd_in, TQSocketNotifier::Read);
177 TQObject::connect(notifier, TQT_SIGNAL(activated(
int)), receiver, member);
187 if (data.size() > 0xffffff)
190 static char buffer[ 64 ];
191 sprintf( buffer,
"%6x_%2x_", data.size(), _cmd );
193 size_t n = fwrite( buffer, 1, 10, f_out );
196 kdError(7017) <<
"Could not send header (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
200 n = fwrite( data.data(), 1, data.size(), f_out );
202 if ( n != data.size() ) {
203 kdError(7017) <<
"Could not write data (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
207 if (fflush( f_out )) {
208 kdError(7017) <<
"Could not write data (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
218 kdError(7017) <<
"read: not yet inited (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
222 static char buffer[ 10 ];
225 ssize_t n =
::read( fd_in, buffer, 10);
226 if ( n == -1 && errno == EINTR )
230 kdError(7017) <<
"Header read failed, errno=" << errno <<
" (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
235 kdError(7017) <<
"Header has invalid size (" << n <<
") (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
243 while( *p ==
' ' ) p++;
244 long int len = strtol( p, 0L, 16 );
247 while( *p ==
' ' ) p++;
248 long int cmd = strtol( p, 0L, 16 );
253 size_t bytesToGo = len;
254 size_t bytesRead = 0;
256 n =
::read(fd_in, data.data()+bytesRead, bytesToGo);
261 kdError(7017) <<
"Data read failed, errno=" << errno <<
" (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
265 kdError(7017) <<
"Connection ended unexpectedly (" << n <<
"/" << bytesToGo <<
") (pid " << getpid() <<
" process \"" << __progname <<
"\")" << endl;
279 #include "connection.moc" void close()
Closes the connection.
bool sendnow(int _cmd, const TQByteArray &data)
Sends the given command immediately.
bool inited() const
Checks whether the connection has been initialized.
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.
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.