| 1 | /*************************************************************************** |
| 2 | * Copyright (C) 2009 by The qGo Project * |
| 3 | * * |
| 4 | * This file is part of qGo. * |
| 5 | * * |
| 6 | * qGo is free software: you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
| 8 | * the Free Software Foundation; either version 2 of the License, or * |
| 9 | * (at your option) any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, see <http://www.gnu.org/licenses/> * |
| 18 | * or write to the Free Software Foundation, Inc., * |
| 19 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
| 20 | ***************************************************************************/ |
| 21 | |
| 22 | |
| 23 | #ifndef NETWORKCONNECTION_H |
| 24 | #define NETWORKCONNECTION_H |
| 25 | #include <map> |
| 26 | #include <QtCore> |
| 27 | #include <QtNetwork> |
| 28 | #include "messages.h" |
| 29 | |
| 30 | class GameListing; |
| 31 | class PlayerListing; |
| 32 | class NetworkDispatch; |
| 33 | class ConsoleDispatch; |
| 34 | class BoardDispatch; |
| 35 | |
| 36 | class GameDialog; |
| 37 | |
| 38 | class NetworkConnection; |
| 39 | |
| 40 | class MatchNegotiationState; |
| 41 | |
| 42 | class Room; |
| 43 | class QMessageBox; |
| 44 | |
| 45 | enum ConnectionState { |
| 46 | Login, |
| 47 | Password, |
| 48 | PasswordSent, |
| 49 | AuthFailed, |
| 50 | PassFailed, |
| 51 | Info, |
| 52 | Setup, |
| 53 | Connected, |
| 54 | Reconnecting, |
| 55 | Canceled, |
| 56 | ProtocolError, |
| 57 | AlreadyLoggedIn, |
| 58 | ConnectionRefused, |
| 59 | HostNotFound, |
| 60 | SockTimeout, |
| 61 | UnknownError |
| 62 | }; |
| 63 | |
| 64 | #define NOT_MAIN_CONNECTION true |
| 65 | |
| 66 | class ConnectionCredentials |
| 67 | { |
| 68 | public: |
| 69 | ConnectionCredentials(ConnectionType t, QString h, qint16 p, QString u, QString pass) : |
| 70 | type(t), hostName(h), port(p), userName(u), password(pass) {} |
| 71 | ConnectionType type; |
| 72 | QString hostName; |
| 73 | qint16 port; |
| 74 | QString userName; |
| 75 | QString password; |
| 76 | }; |
| 77 | |
| 78 | /* Game refusal motives... like we need a reason. */ |
| 79 | #define GD_REFUSE_NOTOPEN 0 |
| 80 | #define GD_REFUSE_DECLINE 1 |
| 81 | #define GD_REFUSE_CANCEL 2 |
| 82 | #define GD_REFUSE_INGAME 3 |
| 83 | #define GD_REFUSE_NODIRECT 4 |
| 84 | #define GD_INVALID_PARAMETERS 5 |
| 85 | #define GD_OPP_NO_NMATCH 6 |
| 86 | #define GD_RESET 7 |
| 87 | |
| 88 | /* NetworkConnection should emit signals for many events */ |
| 89 | class NetworkConnection : public QObject |
| 90 | { |
| 91 | Q_OBJECT |
| 92 | |
| 93 | public: |
| 94 | NetworkConnection(ConnectionCredentials credentials); |
| 95 | ~NetworkConnection(); |
| 96 | void userCanceled(void) { setState(Canceled); /* anything else? */} |
| 97 | int checkForOpenBoards(void); |
| 98 | virtual void sendPlayersRequest(void) = 0; |
| 99 | void onClose(void); //so far private? we're going to have tygem use it... |
| 100 | virtual void sendText(QString text) = 0; |
| 101 | virtual void sendText(const char * text) = 0; |
| 102 | void sendConsoleText(const char * text); |
| 103 | virtual void sendDisconnect(void) = 0; |
| 104 | virtual void sendMsg(unsigned int game_id, QString text) = 0; |
| 105 | virtual void sendMsg(PlayerListing * player, QString text) = 0; |
| 106 | virtual void sendToggle(const QString & param, bool val) = 0; |
| 107 | virtual void sendObserve(const GameListing * game) = 0; |
| 108 | virtual void sendObserveOutside(const GameListing *) {} //optional |
| 109 | virtual void stopObserving(const GameListing *) {} |
| 110 | virtual void stopReviewing(const GameListing *) {} |
| 111 | virtual void sendStatsRequest(const PlayerListing * opponent) = 0; |
| 112 | virtual void sendGamesRequest(void) = 0; |
| 113 | virtual void sendMatchInvite(const PlayerListing *) = 0; |
| 114 | virtual void adjournGame(const GameListing *) {} |
| 115 | virtual void sendTime(BoardDispatch *) {} |
| 116 | virtual void sendAddTime(int) {} |
| 117 | virtual void sendMove(unsigned int game_id, class MoveRecord * m) = 0; |
| 118 | virtual void sendRequestCount(unsigned int) {} |
| 119 | virtual void sendAcceptCountRequest(class GameData *) {} |
| 120 | virtual void sendRefuseCountRequest(class GameData *) {} |
| 121 | virtual void sendAcceptRequestMatchMode(unsigned int) {} |
| 122 | virtual void sendDeclineRequestMatchMode(unsigned int) {} |
| 123 | virtual void sendRequestDraw(unsigned int) {} |
| 124 | virtual void sendAcceptDrawRequest(class GameData *) {} |
| 125 | virtual void sendRefuseDrawRequest(class GameData *) {} |
| 126 | virtual void sendRequestMatchMode(unsigned int) {} |
| 127 | virtual void sendTimeLoss(unsigned int) {} //overwrite or not |
| 128 | virtual void sendResult(class GameData *, class GameResult *) {} //optional |
| 129 | virtual void sendMatchRequest(class MatchRequest * mr) = 0; |
| 130 | virtual void sendRematchRequest(void) {} //optional |
| 131 | virtual void sendRematchAccept(void) {} |
| 132 | virtual void declineMatchOffer(const PlayerListing * opponent) = 0; |
| 133 | virtual void cancelMatchOffer(const PlayerListing * ) {} |
| 134 | virtual void acceptMatchOffer(const PlayerListing * opponent, class MatchRequest * mr) = 0; |
| 135 | virtual QTime gd_checkMainTime(TimeSystem, const QTime & t); |
| 136 | virtual QTime gd_checkPeriodTime(TimeSystem, const QTime & t) { return t; } |
| 137 | virtual unsigned int gd_checkPeriods(TimeSystem, unsigned int p) { return p; } |
| 138 | virtual void sendRejectCount(class GameData *) {} |
| 139 | virtual void sendAcceptCount(class GameData *) {} |
| 140 | virtual void sendAdjournRequest(void) {} |
| 141 | virtual void sendAdjourn(void) {} |
| 142 | virtual void sendRefuseAdjourn(void) {} |
| 143 | public: |
| 144 | int write(const char * packet, unsigned int size); |
| 145 | void setConsoleDispatch(ConsoleDispatch * c); |
| 146 | void setDefaultRoom(Room * r) { default_room = r; } |
| 147 | ConsoleDispatch * getConsoleDispatch(void) { return console_dispatch; } |
| 148 | Room * getDefaultRoom(void) { return default_room; } |
| 149 | class PlayerListing * getPlayerListingFromFriendWatchListing(class FriendWatchListing & f); |
| 150 | virtual void handlePendingData() = 0; |
| 151 | virtual void changeServer(void) {} |
| 152 | virtual void createRoom(void) {} |
| 153 | virtual void sendCreateRoom(class RoomCreate *) {} |
| 154 | virtual void sendJoinRoom(const RoomListing &, const char * = 0) {} |
| 155 | virtual void sendJoinChannel(const ChannelListing &) {} |
| 156 | |
| 157 | |
| 158 | virtual char * sendRequestAccountInfo(int *, void *) { return NULL;} |
| 159 | virtual void handleAccountInfoMsg(int, char *) {} |
| 160 | |
| 161 | virtual void addFriend(PlayerListing * player); |
| 162 | virtual void removeFriend(PlayerListing * player); |
| 163 | virtual void addWatch(PlayerListing * player); |
| 164 | virtual void removeWatch(PlayerListing * player); |
| 165 | virtual void addBlock(PlayerListing * player); |
| 166 | virtual void removeBlock(PlayerListing * player); |
| 167 | virtual char * sendAddFriend(int *, void *) { return NULL;} |
| 168 | virtual void recvFriendResponse(int, char *) {} |
| 169 | virtual char * sendRemoveFriend(int *, void *) { return NULL;} |
| 170 | virtual char * sendAddBlock(int *, void *) { return NULL; } |
| 171 | virtual char * sendRemoveBlock(int *, void *) { return NULL; } |
| 172 | |
| 173 | std::vector<class FriendWatchListing *> & getFriendsList(void) { return friendedList; } |
| 174 | std::vector<class FriendWatchListing *> & getWatchesList(void) { return watchedList; } |
| 175 | std::vector<class FriendWatchListing *> & getBlockedList(void) { return blockedList; } |
| 176 | |
| 177 | // FIXME Not certain but maybe this chunk below should be protected:?? |
| 178 | BoardDispatch * getBoardDispatch(unsigned int game_id); |
| 179 | BoardDispatch * getIfBoardDispatch(unsigned int game_id); |
| 180 | virtual void closeBoardDispatch(unsigned int game_id); |
| 181 | int getBoardDispatches(void); |
| 182 | GameDialog * getGameDialog(const PlayerListing * opponent); |
| 183 | GameDialog * getIfGameDialog(const PlayerListing * opponent); |
| 184 | void closeGameDialog(const PlayerListing *opponent); |
| 185 | class MatchRequest * getAndCloseGameDialog(const PlayerListing *opponent); |
| 186 | |
| 187 | const QString & getUsername(void) const { return username; } |
| 188 | virtual const PlayerListing * getOurListing(void); |
| 189 | virtual unsigned short getRoomNumber(void) { return 0; } |
| 190 | virtual void requestGameInfo(unsigned int) {} //for IGS on board open |
| 191 | virtual void requestGameStats(unsigned int) {} //same |
| 192 | virtual unsigned int rankToScore(QString rank) = 0; |
| 193 | virtual unsigned long getGameDialogFlags(void) { return 0; } |
| 194 | virtual void getAndSetFriendWatchType(PlayerListing * player); |
| 195 | virtual void checkGameWatched(const GameListing *game); |
| 196 | |
| 197 | virtual int gd_verifyBoardSize(int v) { return v; } |
| 198 | |
| 199 | virtual bool playerTrackingByID(void) { return false; } |
| 200 | virtual bool flipCoords(void) { return true; } |
| 201 | virtual bool supportsMultipleUndo(void) { return false; } |
| 202 | virtual bool supportsRequestMatchMode(void) { return false; } |
| 203 | virtual bool supportsRequestAdjourn(void) { return false; } |
| 204 | virtual bool supportsRequestDraw(void) { return false; } |
| 205 | virtual bool supportsRequestCount(void) { return false; } |
| 206 | virtual bool supportsAddTime(void) { return false; } |
| 207 | virtual bool supportsObserveOutside(void) { return false; } |
| 208 | virtual bool supportsServerChange(void) { return false; } |
| 209 | virtual bool supportsRefreshListsButtons(void) { return false; } |
| 210 | virtual bool consoleIsChat(void) { return true; } |
| 211 | virtual bool supportsRematch(void) { return false; } |
| 212 | virtual bool startTimerOnOpen(void) { return false; } //name?? no "supports"? |
| 213 | virtual bool clientCountsTime(void) { return true; } |
| 214 | virtual bool clientSendsTime(void) { return false; } |
| 215 | virtual bool undoResetsScore(void) { return false; } |
| 216 | virtual bool netWillEnterScoreMode(void) { return false; } |
| 217 | virtual bool canMarkStonesDeadinScore(void) { return true; } |
| 218 | virtual bool unmarkUnmarksAllDeadStones(void) { return false; } |
| 219 | virtual bool cantMarkOppStonesDead(void) { return false; } |
| 220 | virtual bool twoPassesEndsGame(void) { return false; } //used?? FIXME |
| 221 | virtual bool supportsFriendList(void) { return false; } |
| 222 | virtual bool supportsWatchList(void) { return false; } |
| 223 | virtual bool supportsBlockList(void) { return false; } |
| 224 | virtual bool supportsSeek(void) { return false; } |
| 225 | virtual const char * getCodecString(void) { return ""; } |
| 226 | virtual QString getPlaceString(void) { return ""; } |
| 227 | virtual void saveIfDoesntSave(GameData *) {} |
| 228 | virtual unsigned long getPlayerListColumns(void) { return 0; } |
| 229 | #define PL_NOWINSLOSSES 0x01 |
| 230 | #define PL_NOMATCHPREFS 0x02 |
| 231 | virtual bool supportsChannels(void) { return false; } |
| 232 | virtual bool supportsCreateRoom(void) { return false; } |
| 233 | virtual unsigned long getRoomStructureFlags(void) = 0; |
| 234 | #define RS_NOROOMLIST 0x01 |
| 235 | #define RS_SHORTROOMLIST 0x02 |
| 236 | #define RS_LONGROOMLIST 0x04 |
| 237 | #define RS_ONEROOMATATIME 0x08 |
| 238 | #define RS_ONEGAMEPERROOM 0x10 |
| 239 | std::vector<class RoomListing *> * getRoomList(void) { return 0; } |
| 240 | void recvRoomListing(class RoomListing * r); |
| 241 | void recvChannelListing(class ChannelListing * r); |
| 242 | /* again, recvRoom is solely for igs handlers which aren't |
| 243 | * part of networkconnection FIXME */ |
| 244 | |
| 245 | void recvSeekCondition(class SeekCondition * s); |
| 246 | void recvSeekCancel(void); |
| 247 | void recvSeekPlayer(QString player, QString condition); |
| 248 | virtual void sendSeek(class SeekCondition *) {} |
| 249 | virtual void sendSeekCancel(void) {} |
| 250 | |
| 251 | signals: |
| 252 | void ready(void); |
| 253 | void playerListingReceived(PlayerListing *); |
| 254 | void gameListingReceived(GameListing *); |
| 255 | void stateChanged(ConnectionState); |
| 256 | |
| 257 | protected: |
| 258 | void closeConnection(bool send_disconnect = true); |
| 259 | virtual void onAuthenticationNegotiated(void); |
| 260 | virtual void onReady(void); |
| 261 | QTcpSocket * getQSocket(void) { return qsocket; } |
| 262 | void writeZeroPaddedString(char * dst, const QString & src, int size); |
| 263 | bool openConnection(const QString & host, const unsigned short port, bool not_main_connection = false); |
| 264 | void latencyOnSend(void); |
| 265 | void latencyOnRecv(void); |
| 266 | void changeChannel(const QString & s); |
| 267 | void setState(ConnectionState newState); |
| 268 | |
| 269 | bool firstonReadyCall; |
| 270 | Room * default_room; |
| 271 | ConsoleDispatch * console_dispatch; |
| 272 | |
| 273 | ConnectionType connectionType; |
| 274 | QString hostname; |
| 275 | qint16 port; |
| 276 | QString username; |
| 277 | QString password; |
| 278 | |
| 279 | ConnectionState connectionState; |
| 280 | |
| 281 | bool friendwatch_notify_default; |
| 282 | |
| 283 | std::vector<FriendWatchListing *> friendedList; |
| 284 | std::vector<FriendWatchListing *> watchedList; |
| 285 | std::vector<FriendWatchListing *> blockedList; |
| 286 | |
| 287 | MatchNegotiationState * match_negotiation_state; |
| 288 | int lastMainTimeChecked, lastPeriodTimeChecked, lastPeriodsChecked; |
| 289 | |
| 290 | QTcpSocket * qsocket; |
| 291 | |
| 292 | private: |
| 293 | void setupRoomAndConsole(void); |
| 294 | void tearDownRoomAndConsole(void); |
| 295 | void loadfriendswatches(void); |
| 296 | void savefriendswatches(void); |
| 297 | |
| 298 | void drawPleaseWait(void); |
| 299 | |
| 300 | Room * mainwindowroom; |
| 301 | |
| 302 | QMessageBox * connectingDialog; |
| 303 | #ifdef LATENCY |
| 304 | struct timeval latencyLast; |
| 305 | unsigned long latencyAverage; |
| 306 | #endif //LATENCY |
| 307 | |
| 308 | protected: |
| 309 | QMap <unsigned int, BoardDispatch *> boardDispatchMap; |
| 310 | // Accessed by IGS connection class |
| 311 | PlayerListing * ourListing; |
| 312 | private: |
| 313 | QMap <const PlayerListing *, GameDialog *> gameDialogMap; |
| 314 | |
| 315 | protected: |
| 316 | virtual void OnConnected(); |
| 317 | private: |
| 318 | //void OnHostFound(); |
| 319 | void OnReadyRead(); |
| 320 | void OnConnectionClosed(); |
| 321 | //OnDelayedCloseFinish(); |
| 322 | //OnBytesWritten(int); |
| 323 | void OnError(QAbstractSocket::SocketError); |
| 324 | |
| 325 | void slot_cancelConnecting(void); |
| 326 | }; |
| 327 | |
| 328 | class FriendWatchListing |
| 329 | { |
| 330 | public: |
| 331 | FriendWatchListing(QString n, bool b = false) : name(n), id(0), notify(b), online(false) {} |
| 332 | QString name; |
| 333 | unsigned short id; //if necessary |
| 334 | bool notify; |
| 335 | bool online; |
| 336 | }; |
| 337 | #endif //NETWORKCONNECTION_H |