rconprotocol.cpp
1 //------------------------------------------------------------------------------
2 // rconprotocol.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "rconprotocol.h"
24 #include "serverapi/server.h"
25 
26 #include <cassert>
27 
28 DClass<RConProtocol>
29 {
30 public:
31  bool connected;
32  QList<Player> players;
33  QHostAddress serverAddress;
34  quint16 serverPort;
35  QUdpSocket socket;
36 
37  void (RConProtocol::*disconnectFromServer)();
38  void (RConProtocol::*sendCommand)(const QString &);
39  void (RConProtocol::*sendPassword)(const QString &);
40 };
41 
42 DPointeredNoCopy(RConProtocol)
43 
44 RConProtocol::RConProtocol(ServerPtr server)
45 {
46  d->connected = false;
47  d->serverAddress = server->address();
48  d->serverPort = server->port();
49 
50  d->socket.bind();
51 
52  set_disconnectFromServer(&RConProtocol::disconnectFromServer_default);
53  set_sendCommand(&RConProtocol::sendCommand_default);
54  set_sendPassword(&RConProtocol::sendPassword_default);
55 }
56 
57 RConProtocol::~RConProtocol()
58 {
59  d->socket.close();
60 }
61 
62 POLYMORPHIC_DEFINE(void, RConProtocol, disconnectFromServer, (), ())
63 POLYMORPHIC_DEFINE(void, RConProtocol, sendCommand, (const QString &cmd), (cmd))
64 POLYMORPHIC_DEFINE(void, RConProtocol, sendPassword, (const QString &password), (password))
65 
66 const QHostAddress &RConProtocol::address() const
67 {
68  return d->serverAddress;
69 }
70 
71 void RConProtocol::disconnectFromServer_default()
72 {
73  assert(0 && "RConProtocol::disconnectFromServer() is not implemented");
74 }
75 
77 {
78  return d->connected;
79 }
80 
81 quint16 RConProtocol::port() const
82 {
83  return d->serverPort;
84 }
85 
86 const QList<Player> &RConProtocol::players() const
87 {
88  return d->players;
89 }
90 
92 {
93  return d->players;
94 }
95 
96 void RConProtocol::sendCommand_default(const QString &cmd)
97 {
98  Q_UNUSED(cmd);
99  assert(0 && "RConProtocol::sendCommand() is not implemented");
100 }
101 
102 void RConProtocol::sendPassword_default(const QString &password)
103 {
104  Q_UNUSED(password);
105  assert(0 && "RConProtocol::sendPassword() is not implemented");
106 }
107 
109 {
110  d->connected = b;
111 }
112 
113 QUdpSocket &RConProtocol::socket()
114 {
115  return d->socket;
116 }