Updated Glossario.tex
[GoMoku3D.git] / src / network / StreamSocket.h
blobb17de3f1a477bc1792dea364462e339ad0f544c0
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
25 #include <QtDebug>
26 #include <QLinkedList>
27 #include <QHostAddress>
28 #include <QObject>
29 #include <QMetaObject>
30 #include <QTcpSocket>
31 #include <QTimer>
32 #include <QXmlStreamReader>
33 #include <QXmlStreamWriter>
35 #include "Global.h"
36 #include "Move.h"
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) \
49 while (!atEnd()) { \
50 readNext(); \
51 elementName = name().toString(); \
52 if (isStartElement()) { \
53 parse(elementName); \
54 } else if (isEndElement()) { \
55 Q_ASSERT(elementName == #token);
57 #define END_NONTERMINAL_HANDLER(token) \
58 } else { \
59 LOG(QString("unexpected data in <") + #token + "> {" + tokenString() + "}"); \
60 } \
61 } \
62 SET_HANDLER("") \
65 #define BEGIN_TERMINAL_HANDLER(token) \
66 HANDLER_SIGNATURE(token) \
67 if (atEnd()) return;
69 #define END_TERMINAL_HANDLER }
72 class StreamSocket : public QObject, protected QXmlStreamReader, protected QXmlStreamWriter
74 Q_OBJECT
76 TEST_FRIEND(StreamSocketTest)
77 TEST_FRIEND(ClientSocketTest)
78 TEST_FRIEND(ServerSocketTest)
80 public:
81 virtual ~StreamSocket();
83 enum ProtocolState {
84 Unconnected,
85 Listening,
86 Connecting,
87 Connected,
88 OpeningStream,
89 FullyOpened,
90 Idle,
91 AwaitingJoinRequest,
92 AwaitingJoinAnswer,
93 AwaitingPlayers,
94 AwaitingGameStart,
95 Playing,
96 AwaitingMove,
97 Closing,
98 Closed
101 void changeState(ProtocolState state);
102 ProtocolState state() const;
103 QString stateString() const;
104 Move takeFirstMove();
106 public slots:
107 void sendChatMessage(QString sender, QString msg);
108 void sendMove(Move move);
110 protected:
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;
118 QTcpSocket *_socket;
120 protected slots:
121 virtual void openStream();
122 virtual void closeStream();
123 virtual void parseData();
125 private:
126 QLinkedList<Move> _buffer;
127 ProtocolState _state;
128 QTimer _pingTimer;
129 QTimer _pongTimer;
131 private slots:
132 void handleError(QAbstractSocket::SocketError);
133 void sendPing();
134 void sendPong();
135 void timedOut();
136 void parse_stream();
137 void parse_playerJoined();
138 void parse_name();
139 void parse_color();
140 void parse_type();
141 void parse_playerLeft();
142 void parse_chatMessage();
143 void parse_move();
144 void parse_player();
145 void parse_point();
146 void parse_x();
147 void parse_y();
148 void parse_z();
149 void parse_startGame();
150 void parse_ping();
151 void parse_pong();
153 signals:
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);
161 void startGame();
162 void ping();
163 void pong();
166 #endif