summaryrefslogtreecommitdiffstats
path: root/signet
diff options
context:
space:
mode:
authorBen Boeckel <MathStuf@gmail.com>2008-11-05 00:31:21 +0000
committerBen Boeckel <MathStuf@gmail.com>2008-11-05 00:31:21 +0000
commit7e5e639f42bebfb6ff8aae14819a2c174f48e76d (patch)
tree8c28db2d3db8badae45619d8eeb89303020015e3 /signet
parenta227920e1c983853c5c39b0768d8495f8097c194 (diff)
downloadsigen-7e5e639f42bebfb6ff8aae14819a2c174f48e76d.tar.gz
sigen-7e5e639f42bebfb6ff8aae14819a2c174f48e76d.tar.xz
sigen-7e5e639f42bebfb6ff8aae14819a2c174f48e76d.zip
[FIX] Added some preliminary code to handling requests in Server
[FIX] Added DisconnectMediator git-svn-id: https://pokegen.svn.sourceforge.net/svnroot/pokegen/trunk@304 6ecfd1a5-f3ed-3746-8530-beee90d26b22
Diffstat (limited to 'signet')
-rw-r--r--signet/CMakeLists.txt2
-rw-r--r--signet/Server.cpp70
-rw-r--r--signet/protocol/DisconnectMediator.cpp74
-rw-r--r--signet/protocol/DisconnectMediator.h65
-rw-r--r--signet/protocol/PacketMaker.cpp10
-rw-r--r--signet/protocol/PacketMaker.h3
6 files changed, 217 insertions, 7 deletions
diff --git a/signet/CMakeLists.txt b/signet/CMakeLists.txt
index 3aeb09dd..996da19f 100644
--- a/signet/CMakeLists.txt
+++ b/signet/CMakeLists.txt
@@ -7,6 +7,7 @@ ENDIF (NOT SIGEN_VERSION)
SET(signet_PROTOCOL_HEADERS
protocol/ChallengeMediator.h
protocol/ConnectMediator.h
+ protocol/DisconnectMediator.h
protocol/Packet.h
protocol/PacketMaker.h
protocol/ProgressMeter.h
@@ -24,6 +25,7 @@ SET(signet_HEADERS
SET(signet_PROTOCOL_SRCS
protocol/ChallengeMediator.cpp
protocol/ConnectMediator.cpp
+ protocol/DisconnectMediator.cpp
protocol/Packet.cpp
protocol/PacketMaker.cpp
)
diff --git a/signet/Server.cpp b/signet/Server.cpp
index 77992cb7..8db9f09a 100644
--- a/signet/Server.cpp
+++ b/signet/Server.cpp
@@ -20,6 +20,9 @@
// Protocol includes
#include "protocol/ChallengeMediator.h"
+#include "protocol/ConnectMediator.h"
+#include "protocol/DisconnectMediator.h"
+#include "protocol/PacketMaker.h"
// Signet includes
#include "Room.h"
@@ -47,7 +50,66 @@ void Signet::Server::packetReceived(QTcpSocket* client, Protocol::Packet* packet
QString receiver = packet->received();
if (receiver.isEmpty())
{
- // TODO: Handle the packet
+ switch (packet->type())
+ {
+ case Protocol::Packet::ConnectRequest:
+ {
+ Protocol::ConnectMediator mediator(client, Protocol::ConnectMediator::Server);
+ Protocol::ConnectMediator::Error error = mediator.replyToConnection();
+ switch (error)
+ {
+ case Protocol::ConnectMediator::NoError:
+ qDebug("Client connected from %s:%d", client->peerAddress().toString().toUtf8().constData(), client->peerPort());
+ break;
+ case Protocol::ConnectMediator::SocketError:
+ qWarning("Socket error on connect");
+ break;
+ case Protocol::ConnectMediator::UnexpectedError:
+ qWarning("Unexpected packet from %s:%d on connect", client->peerAddress().toString().toUtf8().constData(), client->peerPort());
+ break;
+ case Protocol::ConnectMediator::SideError:
+ qCritical("Called the wrong method on connect");
+ break;
+ }
+ break;
+ }
+ case Protocol::Packet::DisconnectRequest:
+ {
+ Protocol::DisconnectMediator mediator(client, Protocol::DisconnectMediator::Server);
+ Protocol::DisconnectMediator::Error error = mediator.replyToDisconnection();
+ switch (error)
+ {
+ case Protocol::DisconnectMediator::NoError:
+ qDebug("Client disconnected from %s:%d", client->peerAddress().toString().toUtf8().constData(), client->peerPort());
+ break;
+ case Protocol::DisconnectMediator::SocketError:
+ qWarning("Socket error on disconnect");
+ break;
+ case Protocol::DisconnectMediator::UnexpectedError:
+ qWarning("Unexpected packet from %s:%d on disconnect", client->peerAddress().toString().toUtf8().constData(), client->peerPort());
+ break;
+ case Protocol::DisconnectMediator::SideError:
+ qCritical("Called the wrong method on disconnect");
+ break;
+ }
+ break;
+ }
+ case Protocol::Packet::ChallengeRequest:
+ break;
+ case Protocol::Packet::UserListRequest:
+ break;
+ case Protocol::Packet::ServerListRequest:
+ break;
+ case Protocol::Packet::RoomListRequest:
+ break;
+ case Protocol::Packet::SigmodListRequest:
+ break;
+ case Protocol::Packet::SigmodRequest:
+ break;
+ default:
+ Protocol::PacketMaker::unhandledRequest(client, QStringList() << QString("server-").arg(m_name));
+ break;
+ }
}
else
{
@@ -86,10 +148,10 @@ void Signet::Server::metaserverConnected()
qDebug("Authenticated with metaserver");
break;
case Protocol::ChallengeMediator::UnknownUser:
- qWarning("Server is not registered");
+ qWarning("Server is not registered with the metaserver");
break;
case Protocol::ChallengeMediator::ClientFailed:
- qWarning("Could not authenticate server");
+ qWarning("Could not authenticate against metaserver");
break;
case Protocol::ChallengeMediator::ServerFailed:
qWarning("Metaserver failed authentication");
@@ -101,7 +163,7 @@ void Signet::Server::metaserverConnected()
qWarning("Unexpected packet");
break;
case Protocol::ChallengeMediator::SideError:
- qWarning("Acted on wrong end of authentication");
+ qCritical("Acted on wrong end of authentication");
break;
}
}
diff --git a/signet/protocol/DisconnectMediator.cpp b/signet/protocol/DisconnectMediator.cpp
new file mode 100644
index 00000000..8f67731d
--- /dev/null
+++ b/signet/protocol/DisconnectMediator.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2008 Ben Boeckel <MathStuf@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+// Header include
+#include "DisconnectMediator.h"
+
+// Protocol includes
+#include "PacketMaker.h"
+
+// Qt includes
+#include <QtNetwork/QTcpSocket>
+
+Signet::Protocol::DisconnectMediator::DisconnectMediator(QTcpSocket* socket, Side side) :
+ m_side(side),
+ m_error(NoError),
+ m_socket(socket)
+{
+}
+
+Signet::Protocol::DisconnectMediator::Error Signet::Protocol::DisconnectMediator::requestDisconnection()
+{
+ if (m_side != Client)
+ return SideError;
+ init();
+ recvAck();
+ return m_error;
+}
+
+Signet::Protocol::DisconnectMediator::Error Signet::Protocol::DisconnectMediator::replyToDisconnection()
+{
+ if (m_side != Server)
+ return SideError;
+ recvAck();
+ if (m_error != NoError)
+ return m_error;
+ sendAck();
+ return m_error;
+}
+
+void Signet::Protocol::DisconnectMediator::init()
+{
+ PacketMaker::dropConnection(m_socket);
+}
+
+void Signet::Protocol::DisconnectMediator::sendAck()
+{
+ PacketMaker::ack(m_socket);
+}
+
+void Signet::Protocol::DisconnectMediator::recvAck()
+{
+ Packet packet = PacketMaker::unwrap(m_socket);
+ if (packet.isValid())
+ {
+ if (packet.type() != Packet::Acknowledge)
+ m_error = UnexpectedError;
+ }
+ else
+ m_error = SocketError;
+}
diff --git a/signet/protocol/DisconnectMediator.h b/signet/protocol/DisconnectMediator.h
new file mode 100644
index 00000000..cb8a9694
--- /dev/null
+++ b/signet/protocol/DisconnectMediator.h
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2008 Ben Boeckel <MathStuf@gmail.com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef SIGNET_PROTOCOL_DISCONNECTMEDIATOR
+#define SIGNET_PROTOCOL_DISCONNECTMEDIATOR
+
+// Signet includes
+#include "../Global.h"
+
+// Forward declarations
+class QTcpSocket;
+
+namespace Signet
+{
+namespace Protocol
+{
+class SIGNET_IMPORT DisconnectMediator
+{
+ public:
+ enum Error
+ {
+ NoError = 0,
+ SocketError = 1,
+ UnexpectedError = 2,
+ SideError = 3
+ };
+
+ enum Side
+ {
+ Client = 0,
+ Server = 1
+ };
+
+ DisconnectMediator(QTcpSocket* socket, Side side);
+
+ Error requestDisconnection();
+ Error replyToDisconnection();
+ protected:
+ void init();
+
+ void sendAck();
+ void recvAck();
+ private:
+ const Side m_side;
+ Error m_error;
+ QTcpSocket* m_socket;
+};
+}
+}
+
+#endif
diff --git a/signet/protocol/PacketMaker.cpp b/signet/protocol/PacketMaker.cpp
index a5b8e890..18c52209 100644
--- a/signet/protocol/PacketMaker.cpp
+++ b/signet/protocol/PacketMaker.cpp
@@ -107,9 +107,15 @@ void Signet::Protocol::PacketMaker::dropConnection(QIODevice* device, const QStr
packet.dump(device);
}
-void Signet::Protocol::PacketMaker::unknownReceiver(QIODevice* device)
+void Signet::Protocol::PacketMaker::unknownReceiver(QIODevice* device, const QStringList& receivers)
{
- Packet packet(Packet::UnknownReceiver);
+ Packet packet(Packet::UnknownReceiver, receivers);
+ packet.dump(device);
+}
+
+void Signet::Protocol::PacketMaker::unhandledRequest(QIODevice* device, const QStringList& receivers)
+{
+ Packet packet(Packet::UnhandledPacket, receivers);
packet.dump(device);
}
diff --git a/signet/protocol/PacketMaker.h b/signet/protocol/PacketMaker.h
index 46d45f77..5c6b9c5b 100644
--- a/signet/protocol/PacketMaker.h
+++ b/signet/protocol/PacketMaker.h
@@ -52,7 +52,8 @@ class SIGNET_IMPORT PacketMaker
static void makeConnection(QIODevice* device, const QStringList& receivers = QStringList());
static void dropConnection(QIODevice* device, const QStringList& receivers = QStringList());
- static void unknownReceiver(QIODevice* device);
+ static void unknownReceiver(QIODevice* device, const QStringList& receivers = QStringList());
+ static void unhandledRequest(QIODevice* device, const QStringList& receivers = QStringList());
static Packet unwrap(QIODevice* device, ProgressMeter* progressMeter = NULL);
};