[Kde-games-devel] KDE/kdegames/libkdegames/kgame
Christian Ehrlicher
Ch.Ehrlicher at gmx.de
Fri Jun 22 20:30:21 CEST 2007
SVN commit 678995 by chehrlic:
k3process--
plz check!
CCMAIL: kde-games-devel at kde.org
M +38 -80 kmessageio.cpp
M +7 -11 kmessageio.h
--- trunk/KDE/kdegames/libkdegames/kgame/kmessageio.cpp #678994:678995
@@ -24,7 +24,7 @@
#include "kmessageio.h"
#include <QTcpSocket>
#include <kdebug.h>
-#include <k3process.h>
+#include <kprocess.h>
#include <QFile>
#include <QDataStream>
// ----------------------- KMessageIO -------------------------
@@ -227,10 +227,6 @@
mProcess->kill();
delete mProcess;
mProcess=0;
- // Remove not send buffers
-// mQueue.setAutoDelete(true);
- while (!mQueue.isEmpty())
- delete mQueue.dequeue();
// Maybe todo: delete mSendBuffer
}
}
@@ -239,20 +235,16 @@
// Start process
kDebug(11001) << "@@@KMessageProcess::Start process" << endl;
mProcessName=file;
- mProcess=new K3Process;
+ mProcess=new KProcess;
int id=0;
*mProcess << mProcessName << QString("%1").arg(id);
kDebug(11001) << "@@@KMessageProcess::Init:Id= " << id << endl;
kDebug(11001) << "@@@KMessgeProcess::Init:Processname: " << mProcessName << endl;
- connect(mProcess, SIGNAL(receivedStdout(K3Process *, char *, int )),
- this, SLOT(slotReceivedStdout(K3Process *, char * , int )));
- connect(mProcess, SIGNAL(receivedStderr(K3Process *, char *, int )),
- this, SLOT(slotReceivedStderr(K3Process *, char * , int )));
- connect(mProcess, SIGNAL(processExited(K3Process *)),
- this, SLOT(slotProcessExited(K3Process *)));
- connect(mProcess, SIGNAL(wroteStdin(K3Process *)),
- this, SLOT(slotWroteStdin(K3Process *)));
- mProcess->start(K3Process::NotifyOnExit,K3Process::All);
+ connect(mProcess, SIGNAL(readyReadStandardOutput()), this, SLOT(slotReceivedStdout()));
+ connect(mProcess, SIGNAL(readyReadStandardError()), this, SLOT(slotReceivedStderr()));
+ connect(mProcess, SIGNAL(finished (int, QProcess::ExitStatus)),
+ this, SLOT(slotProcessExited(int, QProcess::ExitStatus)));
+ mProcess->start();
mSendBuffer=0;
mReceiveCount=0;
mReceiveBuffer.resize(1024);
@@ -260,14 +252,20 @@
bool KMessageProcess::isConnected() const
{
kDebug(11001) << "@@@KMessageProcess::Is conencted" << endl;
- if (!mProcess) return false;
- return mProcess->isRunning();
+ if (!mProcess)
+ return false;
+ return (mProcess->state() == QProcess::Running);
}
void KMessageProcess::send(const QByteArray &msg)
{
kDebug(11001) << "@@@KMessageProcess:: SEND("<<msg.size()<<") to process" << endl;
unsigned int size=msg.size()+2*sizeof(long);
+ if (mProcess == 0) {
+ kDebug(11001) << "@@@KMessageProcess:: cannot write to stdin, no process available" << endl;
+ return;
+ }
+
char *tmpbuffer=new char[size];
long *p1=(long *)tmpbuffer;
long *p2=p1+1;
@@ -276,80 +274,40 @@
*p1=0x4242aeae;
*p2=size;
- QByteArray* buffer = new QByteArray(tmpbuffer,size);
+ // no need to add it to a queue -> qiodevice is buffered
+ mProcess->write(tmpbuffer,size);
delete [] tmpbuffer;
- // buffer->duplicate(msg);
- mQueue.enqueue(buffer);
- writeToProcess();
}
-void KMessageProcess::writeToProcess()
-{
- // Previous send ok and item in queue
- if (mSendBuffer || mQueue.isEmpty()) return ;
- mSendBuffer=mQueue.dequeue();
- if (!mSendBuffer) return ;
- // write it out to the process
- // kDebug(11001) << " @@@@@@ writeToProcess::SEND to process " << mSendBuffer->size() << " BYTE " << endl;
- // char *p=mSendBuffer->data();
- // for (int i=0;i<16;i++) printf("%02x ",(unsigned char)(*(p+i)));printf("\n");
- /// @todo avoids crash, but is it the good solution ???
- if (mProcess == 0) {
- kDebug(11001) << "@@@KMessageProcess:: cannot write to stdin, no process available" << endl;
- return;
- }
- mProcess->writeStdin(mSendBuffer->data(),mSendBuffer->size());
-
-}
-void KMessageProcess::slotWroteStdin(K3Process * )
+void KMessageProcess::slotReceivedStderr()
{
- kDebug(11001) << k_funcinfo << endl;
- if (mSendBuffer)
- {
- delete mSendBuffer;
- mSendBuffer=0;
- }
- writeToProcess();
-}
+ QByteArray ba;
+ kDebug(11001)<<"@@@ KMessageProcess::slotReceivedStderr " << endl;
-void KMessageProcess::slotReceivedStderr(K3Process * proc, char *buffer, int buflen)
-{
- int pid=0;
- int len;
- char *p;
- char *pos;
- kDebug(11001)<<"@@@ KMessageProcess::slotReceivedStderr " << buflen << " bytes" << endl;
-
- if (!buffer || buflen==0) return ;
- if (proc) pid=proc->pid();
-
-
- pos=buffer;
- do
+ mProcess->setReadChannel(QProcess::StandardError);
+ while(mProcess->canReadLine())
{
- p=(char *)memchr(pos,'\n',buflen);
- if (!p) len=buflen;
- else len=p-pos;
+ ba = mProcess->readLine();
+ if( ba.isEmpty() )
+ return;
+ ba.chop( 1 ); // remove '\n'
- QByteArray a(pos, len);
- QString s(a);
- kDebug(11001) << "K3Process:" <<pid<<"("<<len<< "):" << s << endl;
- emit signalReceivedStderr(s);
- a.clear();
- if (p) pos=p+1;
- buflen-=len+1;
- }while(buflen>0);
+ kDebug(11001) << "KProcess (" << ba.size() << "):" << ba.constData() << endl;
+ emit signalReceivedStderr(ba);
+ ba.clear();
+ };
}
-void KMessageProcess::slotReceivedStdout(K3Process * , char *buffer, int buflen)
+void KMessageProcess::slotReceivedStdout()
{
- kDebug(11001) << "$$$$$$ " << k_funcinfo << ": Received " << buflen << " bytes over inter process communication" << endl;
+ mProcess->setReadChannel(QProcess::StandardOutput);
+ QByteArray ba = mProcess->readAll();
+ kDebug(11001) << "$$$$$$ " << k_funcinfo << ": Received " << ba.size() << " bytes over inter process communication" << endl;
// TODO Make a plausibility check on buflen to avoid memory overflow
- while (mReceiveCount+buflen>=mReceiveBuffer.size()) mReceiveBuffer.resize(mReceiveBuffer.size()+1024);
- memcpy(mReceiveBuffer.data()+mReceiveCount,buffer,buflen);
- mReceiveCount+=buflen;
+ mReceiveBuffer += ba;
+ mReceiveCount += ba.size();
// Possbile message
while (mReceiveCount>int(2*sizeof(long)))
@@ -391,9 +349,9 @@
}
}
-void KMessageProcess::slotProcessExited(K3Process * /*p*/)
+void KMessageProcess::slotProcessExited(int exitCode, QProcess::ExitStatus)
{
- kDebug(11001) << "Process exited (slot)" << endl;
+ kDebug(11001) << "Process exited (slot) with code " << exitCode << endl;
emit connectionBroken();
delete mProcess;
mProcess=0;
--- trunk/KDE/kdegames/libkdegames/kgame/kmessageio.h #678994:678995
@@ -25,15 +25,14 @@
#define _KMESSAGEIO_H_
#include <QtCore/QObject>
+#include <QtCore/QProcess>
#include <QtCore/QString>
-#include <QtCore/QQueue>
-#include <QtCore/QFile>
#include <QtNetwork/QHostAddress>
#include <kdebug.h>
class QTcpSocket;
-class K3Process;
-//class QFile;
+class KProcess;
+class QFile;
/**
@@ -346,7 +345,6 @@
~KMessageProcess();
bool isConnected() const;
void send (const QByteArray &msg);
- void writeToProcess();
/**
@return FALSE as this is no network IO.
@@ -361,18 +359,16 @@
public Q_SLOTS:
- void slotReceivedStdout(K3Process *proc, char *buffer, int buflen);
- void slotReceivedStderr(K3Process *proc, char *buffer, int buflen);
- void slotProcessExited(K3Process *p);
- void slotWroteStdin(K3Process *p);
+ void slotReceivedStdout();
+ void slotReceivedStderr();
+ void slotProcessExited(int, QProcess::ExitStatus);
Q_SIGNALS:
void signalReceivedStderr(QString msg);
private:
QString mProcessName;
- K3Process *mProcess;
- QQueue<QByteArray*> mQueue;
+ KProcess *mProcess;
QByteArray* mSendBuffer;
QByteArray mReceiveBuffer;
int mReceiveCount;
More information about the kde-games-devel
mailing list