libkdegames Library API Documentation

kgameprocess.cpp

00001 /* 00002 This file is part of the KDE games library 00003 Copyright (C) 2001 Martin Heni (martin@heni-online.de) 00004 Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de) 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License version 2 as published by the Free Software Foundation. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 00019 */ 00020 /* 00021 $Id: kgameprocess.cpp,v 1.13 2003/02/21 14:24:17 andreas Exp $ 00022 */ 00023 00024 #include "kgameprocess.h" 00025 #include "kplayer.h" 00026 #include "kgame.h" 00027 #include "kgamemessage.h" 00028 #include "kmessageio.h" 00029 00030 #include <krandomsequence.h> 00031 00032 #include <qbuffer.h> 00033 #include <qdatastream.h> 00034 #include <qcstring.h> 00035 00036 #include <assert.h> 00037 #include <stdio.h> 00038 #include <stdlib.h> 00039 #include <unistd.h> 00040 #include <string.h> 00041 00042 #define READ_BUFFER_SIZE 1024 00043 00044 // ----------------------- Process Child --------------------------- 00045 00046 KGameProcess::KGameProcess() : QObject(0,0) 00047 { 00048 mTerminate=false; 00049 // Check whether a player is set. If not create one! 00050 rFile.open(IO_ReadOnly|IO_Raw,stdin); 00051 wFile.open(IO_WriteOnly|IO_Raw,stdout); 00052 mMessageIO=new KMessageFilePipe(this,&rFile,&wFile); 00053 // mMessageClient=new KMessageClient(this); 00054 // mMessageClient->setServer(mMessageIO); 00055 // connect (mMessageClient, SIGNAL(broadcastReceived(const QByteArray&, Q_UINT32)), 00056 // this, SLOT(receivedMessage(const QByteArray&, Q_UINT32))); 00057 connect (mMessageIO, SIGNAL(received(const QByteArray&)), 00058 this, SLOT(receivedMessage(const QByteArray&))); 00059 fprintf(stderr,"KGameProcess::constructor %p %p\n",&rFile,&wFile); 00060 00061 mRandom = new KRandomSequence; 00062 mRandom->setSeed(0); 00063 } 00064 KGameProcess::~KGameProcess() 00065 { 00066 delete mRandom; 00067 //delete mMessageClient; 00068 //delete mMessageServer; 00069 delete mMessageIO; 00070 rFile.close(); 00071 wFile.close(); 00072 fprintf(stderr,"KGameProcess::destructor\n"); 00073 } 00074 00075 00076 bool KGameProcess::exec(int argc, char *argv[]) 00077 { 00078 // Get id and cookie, ... from command line 00079 processArgs(argc,argv); 00080 do 00081 { 00082 mMessageIO->exec(); 00083 } while(!mTerminate); 00084 return true; 00085 } 00086 00087 // You have to do this to create a message 00088 // QByteArray buffer; 00089 // QDataStream wstream(buffer,IO_WriteOnly); 00090 // then stream data into the stream and call this function 00091 void KGameProcess::sendSystemMessage(QDataStream &stream,int msgid,Q_UINT32 receiver) 00092 { 00093 fprintf(stderr,"KGameProcess::sendMessage id=%d recv=%d",msgid,receiver); 00094 QByteArray a; 00095 QDataStream outstream(a,IO_WriteOnly); 00096 00097 QBuffer *device=(QBuffer *)stream.device(); 00098 QByteArray data=device->buffer();; 00099 00100 KGameMessage::createHeader(outstream,0,receiver,msgid); 00101 outstream.writeRawBytes(data.data(),data.size()); 00102 00103 //if (mMessageClient) mMessageClient->sendBroadcast(a); 00104 // TODO: The fixed received 2 will cause problems. But how to address the 00105 // proper one? 00106 // if (mMessageClient) mMessageClient->sendForward(a,2); 00107 if (mMessageIO) mMessageIO->send(a); 00108 } 00109 00110 void KGameProcess::sendMessage(QDataStream &stream,int msgid,Q_UINT32 receiver) 00111 { 00112 sendSystemMessage(stream,msgid+KGameMessage::IdUser,receiver); 00113 } 00114 00115 void KGameProcess::processArgs(int argc, char *argv[]) 00116 { 00117 int v=0; 00118 if (argc>2) 00119 { 00120 v=atoi(argv[2]); 00121 //kdDebug(11001) << "cookie (unused) " << v << endl; 00122 } 00123 if (argc>1) 00124 { 00125 v=atoi(argv[1]); 00126 //kdDebug(11001) << "id (unused) " << v << endl; 00127 } 00128 fprintf(stderr,"processArgs \n"); 00129 fflush(stderr); 00130 } 00131 00132 void KGameProcess::receivedMessage(const QByteArray& receiveBuffer) 00133 { 00134 QDataStream stream(receiveBuffer, IO_ReadOnly); 00135 int msgid; 00136 Q_UINT32 sender; 00137 Q_UINT32 receiver; 00138 KGameMessage::extractHeader(stream, sender, receiver, msgid); 00139 fprintf(stderr,"------ receiveNetworkTransmission(): id=%d sender=%d,recv=%d\n",msgid,sender,receiver); 00140 switch(msgid) 00141 { 00142 case KGameMessage::IdTurn: 00143 Q_INT8 b; 00144 stream >> b; 00145 emit signalTurn(stream,(bool)b); 00146 break; 00147 case KGameMessage::IdIOAdded: 00148 Q_INT16 id; 00149 stream >> id; 00150 emit signalInit(stream,(int)id); 00151 break; 00152 default: 00153 emit signalCommand(stream,msgid-KGameMessage::IdUser,receiver,sender); 00154 break; 00155 } 00156 } 00157 00158 #include "kgameprocess.moc"
KDE Logo
This file is part of the documentation for libkdegames Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Sep 21 12:08:03 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003