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  assert(0 && "RConProtocol::sendCommand() is not implemented");
99 }
100 
101 void RConProtocol::sendPassword_default(const QString &password)
102 {
103  assert(0 && "RConProtocol::sendPassword() is not implemented");
104 }
105 
107 {
108  d->connected = b;
109 }
110 
111 QUdpSocket &RConProtocol::socket()
112 {
113  return d->socket;
114 }
115 
void setConnected(bool b)
Set this to true when connection is successfully established, set to false upon disconnect.
QList< Player > & playersMutable()
Accessor to players list that allows modification.
bool isConnected() const
Whether connection is established or not.
const QList< Player > & players() const
List of players present on the server.
Remote console protocol interface.
Definition: rconprotocol.h:79
const QHostAddress & address() const
Address of the server to which this remote console should connect.
void sendPassword(const QString &password)
[Pure Virtual] Send remote console password.
QUdpSocket & socket()
UDP socket that can be used to communicate with remote server.
void sendCommand(const QString &cmd)
[Pure Virtual] Send command to remote server.
void disconnectFromServer()
[Pure Virtual] Close connection with the server.
quint16 port() const
Port of the server to which this remote console should connect.