QTfrontend/ipc.h
changeset 18 d49c23eba8ba
equal deleted inserted replaced
17:a6bed8d31444 18:d49c23eba8ba
       
     1 /*
       
     2  * Hedgewars, a worms-like game
       
     3  * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
       
     4  *
       
     5  * Distributed under the terms of the BSD-modified licence:
       
     6  *
       
     7  * Permission is hereby granted, free of charge, to any person obtaining a copy
       
     8  * of this software and associated documentation files (the "Software"), to deal
       
     9  * with the Software without restriction, including without limitation the
       
    10  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
       
    11  * sell copies of the Software, and to permit persons to whom the Software is
       
    12  * furnished to do so, subject to the following conditions:
       
    13  *
       
    14  * 1. Redistributions of source code must retain the above copyright notice,
       
    15  *    this list of conditions and the following disclaimer.
       
    16  * 2. Redistributions in binary form must reproduce the above copyright notice,
       
    17  *    this list of conditions and the following disclaimer in the documentation
       
    18  *    and/or other materials provided with the distribution.
       
    19  * 3. The name of the author may not be used to endorse or promote products
       
    20  *    derived from this software without specific prior written permission.
       
    21  *
       
    22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
       
    23  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
       
    24  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
       
    25  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
       
    28  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
       
    29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
       
    30  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
       
    31  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    32  */
       
    33 
       
    34 #include <qserversocket.h>
       
    35 #include <qsocket.h>
       
    36 
       
    37 #define MAXMSGCHARS 255
       
    38 #define SENDIPC(a) SendIPC(a, sizeof(a) - 1)
       
    39 
       
    40 class QSocket;
       
    41 class QServerSocket;
       
    42 
       
    43 class IPCServer : public QServerSocket
       
    44 {
       
    45 	Q_OBJECT
       
    46 public:
       
    47 	IPCServer( const QHostAddress & address, Q_UINT16 port, QObject *parent ) :
       
    48 	QServerSocket(address, port, 1, parent, 0)
       
    49 	{
       
    50 		if ( !ok() )
       
    51 		{
       
    52 			qWarning("Failed to bind");
       
    53 			exit( 1 );
       
    54 		}
       
    55 		msgsize = 0; 
       
    56 	}
       
    57 	
       
    58 private:
       
    59 	char msgbuf[MAXMSGCHARS];
       
    60 	unsigned char msgbufsize; 
       
    61 	unsigned char msgsize;	
       
    62 	QSocket* ipcsock;
       
    63 	
       
    64 	void SendConfig()
       
    65 	{
       
    66 		SENDIPC("TL");
       
    67 		SENDIPC("e$gmflags 0");
       
    68 		SENDIPC("eaddteam");
       
    69 		SENDIPC("ename team \"C0CuCKAzZz\"");
       
    70 		SENDIPC("ename hh0 \"Йожык\"");
       
    71 		SENDIPC("ename hh1 \"Ёжик\"");
       
    72 		SENDIPC("ename hh2 \"Ёжык\"");
       
    73 		SENDIPC("ename hh3 \"Йожик\"");
       
    74 		SENDIPC("ename hh4 \"Ёжик без ножек\"");
       
    75 		SENDIPC("ename hh5 \"Just hedgehog\"");
       
    76 		SENDIPC("ename hh6 \"Ёжик без головы\"");
       
    77 		SENDIPC("ename hh7 \"Валасатый йож\"");
       
    78 		SENDIPC("ebind left  \"+left\"");
       
    79 		SENDIPC("ebind right \"+right\"");
       
    80 		SENDIPC("ebind up    \"+up\"");
       
    81 		SENDIPC("ebind down  \"+down\"");
       
    82 		SENDIPC("ebind F1  \"slot 1\"");
       
    83 		SENDIPC("ebind F2  \"slot 2\"");
       
    84 		SENDIPC("ebind F3  \"slot 3\"");
       
    85 		SENDIPC("ebind F4  \"slot 4\"");
       
    86 		SENDIPC("ebind F5  \"slot 5\"");
       
    87 		SENDIPC("ebind F6  \"slot 6\"");
       
    88 		SENDIPC("ebind F7  \"slot 7\"");
       
    89 		SENDIPC("ebind F8  \"slot 8\"");
       
    90 		SENDIPC("ebind F10 \"quit\"");
       
    91 		SENDIPC("ebind F11 \"capture\"");
       
    92 		SENDIPC("ebind space     \"+attack\"");
       
    93 		SENDIPC("ebind return    \"ljump\"");
       
    94 		SENDIPC("ebind backspace \"hjump\"");
       
    95 		SENDIPC("ebind tab       \"switch\"");
       
    96 		SENDIPC("ebind 1 \"timer 1\"");
       
    97 		SENDIPC("ebind 2 \"timer 2\"");
       
    98 		SENDIPC("ebind 3 \"timer 3\"");
       
    99 		SENDIPC("ebind 4 \"timer 4\"");
       
   100 		SENDIPC("ebind 5 \"timer 5\"");
       
   101 		SENDIPC("ebind mousel \"put\"");
       
   102 		SENDIPC("egrave \"coffin\"");
       
   103 		SENDIPC("efort \"Barrelhouse\"");
       
   104 		SENDIPC("ecolor 65535");
       
   105 		SENDIPC("eadd hh0 0");
       
   106 		SENDIPC("eadd hh1 0");
       
   107 		SENDIPC("eadd hh2 0");
       
   108 		SENDIPC("eadd hh3 0");
       
   109 		SENDIPC("eaddteam");
       
   110 		SENDIPC("ename team \"-= ЕЖЫ =-\"");
       
   111 		SENDIPC("ename hh0 \"Маленький\"");
       
   112 		SENDIPC("ename hh1 \"Удаленький\"");
       
   113 		SENDIPC("ename hh2 \"Игольчатый\"");
       
   114 		SENDIPC("ename hh3 \"Стреляный\"");
       
   115 		SENDIPC("ename hh4 \"Ежиха\"");
       
   116 		SENDIPC("ename hh5 \"Ежонок\"");
       
   117 		SENDIPC("ename hh6 \"Инфернальный\"");
       
   118 		SENDIPC("ename hh7 \"X\"");
       
   119 		SENDIPC("egrave Bone");
       
   120 		SENDIPC("ecolor 16776960");
       
   121 		SENDIPC("eadd hh0 1");
       
   122 		SENDIPC("eadd hh1 1");
       
   123 		SENDIPC("eadd hh2 1");
       
   124 		SENDIPC("eadd hh3 1");
       
   125 		SENDIPC("efort Barrelhouse");		
       
   126 	}
       
   127 	
       
   128 	void ParseMessage()
       
   129 	{
       
   130 		switch(msgsize) {
       
   131 			case 1: switch(msgbuf[0]) {
       
   132 				case '?': {
       
   133 					SENDIPC("!");
       
   134 					break;
       
   135 				}
       
   136 			}
       
   137 			case 5: switch(msgbuf[0]) {
       
   138 				case 'C': {
       
   139 					SendConfig();
       
   140 					break;
       
   141 				}
       
   142 			}
       
   143 		}
       
   144 	}
       
   145 	
       
   146 	void SendIPC(const char* msg, unsigned char len)
       
   147 	{
       
   148 		ipcsock->writeBlock((char *)&len, 1);
       
   149 		ipcsock->writeBlock(msg, len);
       
   150 	}
       
   151 
       
   152 private slots:
       
   153 	void newConnection( int socket )
       
   154 	{
       
   155     	ipcsock = new QSocket( this );
       
   156 		connect( ipcsock, SIGNAL(readyRead()), this, SLOT(readClient()) );
       
   157 		connect( ipcsock, SIGNAL(delayedCloseFinished()), this, SLOT(discardClient()) );
       
   158 		ipcsock->setSocket( socket );
       
   159 	}
       
   160 
       
   161 	void readClient()
       
   162 	{
       
   163 		Q_ULONG readbytes = 1;
       
   164 		while (readbytes > 0) {
       
   165 			if (msgsize == 0) {
       
   166 				msgbufsize = 0;
       
   167 				readbytes = ipcsock->readBlock((char *)&msgsize, 1);
       
   168 			}
       
   169 			else {
       
   170 				msgbufsize += readbytes = ipcsock->readBlock((char *)&msgbuf[msgbufsize], msgsize - msgbufsize);
       
   171 				if (msgbufsize = msgsize) {
       
   172 					ParseMessage();
       
   173 					msgsize = 0;
       
   174 				}
       
   175 			}
       
   176 		}
       
   177 	}
       
   178 
       
   179 	void discardClient()
       
   180 	{
       
   181     	delete ipcsock;
       
   182 	}
       
   183 };