1 /********************************************************************
3 * Copyright (C) 2008 Davide Pesavento
5 * This file is part of GoMoku3D.
7 * GoMoku3D is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * GoMoku3D is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GoMoku3D. If not, see <http://www.gnu.org/licenses/>.
20 *******************************************************************/
22 #ifndef STREAMSOCKET_H
23 #define STREAMSOCKET_H
26 #include <QLinkedList>
27 #include <QHostAddress>
29 #include <QMetaObject>
32 #include <QXmlStreamReader>
33 #include <QXmlStreamWriter>
38 #define CLASS_NAME(c) (c)->metaObject()->className()
39 #define PRINT_DEBUG_INFO(c) (QString(CLASS_NAME(c)) + " [" + (c)->_socket->peerAddress().toString() + "] : ")
40 #define LOG(msg) qDebug() << PRINT_DEBUG_INFO(this) + (msg)
41 #define WARN(msg) qWarning() << PRINT_DEBUG_INFO(this) + (msg)
42 #define ERR(msg) qCritical() << PRINT_DEBUG_INFO(this) + (msg)
44 #define SET_HANDLER(handler) _currentHandler = (handler);
45 #define RESTORE_HANDLER(handler) SET_HANDLER(handler)
47 #define BEGIN_NONTERMINAL_HANDLER(token) \
48 HANDLER_SIGNATURE(token) \
51 elementName = name().toString(); \
52 if (isStartElement()) { \
54 } else if (isEndElement()) { \
55 Q_ASSERT(elementName == #token);
57 #define END_NONTERMINAL_HANDLER(token) \
59 LOG(QString("unexpected data in <") + #token + "> {" + tokenString() + "}"); \
65 #define BEGIN_TERMINAL_HANDLER(token) \
66 HANDLER_SIGNATURE(token) \
69 #define END_TERMINAL_HANDLER }
72 class StreamSocket
: public QObject
, protected QXmlStreamReader
, protected QXmlStreamWriter
76 TEST_FRIEND(StreamSocketTest
)
77 TEST_FRIEND(ClientSocketTest
)
78 TEST_FRIEND(ServerSocketTest
)
81 virtual ~StreamSocket();
101 void changeState(ProtocolState state
);
102 ProtocolState
state() const;
103 QString
stateString() const;
104 Move
takeFirstMove();
107 void sendChatMessage(QString sender
, QString msg
);
108 void sendMove(Move move
);
111 StreamSocket(QTcpSocket
*socket
);
112 bool parse(QString elementName
);
113 void serialize(Move m
);
115 static const QString _supportedProtocolVersion
;
116 QString _currentHandler
;
117 QString _localPlayerName
;
121 virtual void openStream();
122 virtual void closeStream();
123 virtual void parseData();
126 QLinkedList
<Move
> _buffer
;
127 ProtocolState _state
;
132 void handleError(QAbstractSocket::SocketError
);
137 void parse_playerJoined();
141 void parse_playerLeft();
142 void parse_chatMessage();
149 void parse_startGame();
154 void handshakeCompleted(StreamSocket
*socket
);
155 void protocolError(QString errorMessage
);
156 void statusChanged(QString status
);
157 void playerJoined(int id
, QString name
, QString type
);
158 void playerLeft(int id
);
159 void receivedChatMessage(QString sender
, QString msg
);
160 void receivedMove(Move move
);