diff options
author | Matthias Schiffer <matthias@gamezock.de> | 2008-07-08 22:31:29 +0200 |
---|---|---|
committer | Matthias Schiffer <matthias@gamezock.de> | 2008-07-08 22:31:29 +0200 |
commit | 1508969d508e41de824d1d1227be708136760cfa (patch) | |
tree | 8950d27e708ac8f0451f14c94c8cf76b0ce15877 /src/Common/Request | |
parent | 4cd88a87e66dbbb9be4137de04ad79c97ac5bf2c (diff) | |
download | mad-1508969d508e41de824d1d1227be708136760cfa.tar mad-1508969d508e41de824d1d1227be708136760cfa.zip |
Request-Verarbeitung ?berarbeitet
Diffstat (limited to 'src/Common/Request')
-rw-r--r-- | src/Common/Request/DisconnectRequest.h | 44 | ||||
-rw-r--r-- | src/Common/Request/Makefile.am | 2 | ||||
-rw-r--r-- | src/Common/Request/Makefile.in | 2 | ||||
-rw-r--r-- | src/Common/Request/PingRequest.h | 47 | ||||
-rw-r--r-- | src/Common/Request/Request.h | 57 |
5 files changed, 144 insertions, 8 deletions
diff --git a/src/Common/Request/DisconnectRequest.h b/src/Common/Request/DisconnectRequest.h index 309504d..9023c13 100644 --- a/src/Common/Request/DisconnectRequest.h +++ b/src/Common/Request/DisconnectRequest.h @@ -20,15 +20,53 @@ #ifndef DISCONNECTREQUEST_H_ #define DISCONNECTREQUEST_H_ -#include "../RequestInfo.h" +#include "Request.h" +#include "../RequestManager.h" +#include <Net/Connection.h> +#include <Net/Packet.h> namespace Mad { namespace Common { namespace Request { -class DisconnectRequest: public RequestInfo { +class DisconnectRequest: public Request { + private: + DisconnectRequest() {} + public: - DisconnectRequest() : RequestInfo(TYPE_DISCONNECT) {} + static bool send(Net::Connection *connection, RequestManager &requestManager) { + Request *request = new DisconnectRequest(); + + if(requestManager.sendRequest(connection, request)) + return true; + + delete request; + return false; + } + + virtual bool sendRequest(Net::Connection *connection, unsigned short requestId) { + if(isSent()) + return false; + + if(!connection->send(Net::Packet(Net::Packet::TYPE_DISCONNECT_REQ, requestId))) + return false; + + setSent(); + return true; + } + + virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet) { + if(isFinished()) + return false; + + if(packet.getType() != Net::Packet::TYPE_DISCONNECT_REP) + return false; // TODO Logging + + connection->disconnect(); + + setFinished(); + return true; + } }; } diff --git a/src/Common/Request/Makefile.am b/src/Common/Request/Makefile.am index 35e432b..17e47fe 100644 --- a/src/Common/Request/Makefile.am +++ b/src/Common/Request/Makefile.am @@ -1 +1 @@ -noinst_HEADERS = DisconnectRequest.h PingRequest.h +noinst_HEADERS = DisconnectRequest.h PingRequest.h Request.h diff --git a/src/Common/Request/Makefile.in b/src/Common/Request/Makefile.in index 7d68911..d96c0a7 100644 --- a/src/Common/Request/Makefile.in +++ b/src/Common/Request/Makefile.in @@ -162,7 +162,7 @@ sysconfdir = @sysconfdir@ target_alias = @target_alias@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ -noinst_HEADERS = DisconnectRequest.h PingRequest.h +noinst_HEADERS = DisconnectRequest.h PingRequest.h Request.h all: all-am .SUFFIXES: diff --git a/src/Common/Request/PingRequest.h b/src/Common/Request/PingRequest.h index 35a0171..92e3c1a 100644 --- a/src/Common/Request/PingRequest.h +++ b/src/Common/Request/PingRequest.h @@ -20,15 +20,56 @@ #ifndef MAD_COMMON_REQUEST_PINGREQUEST_H_ #define MAD_COMMON_REQUEST_PINGREQUEST_H_ -#include "../RequestInfo.h" +#include "Request.h" +#include "../RequestManager.h" +#include <Net/Connection.h> +#include <Net/Packet.h> + +#include <iostream> namespace Mad { namespace Common { namespace Request { -class PingRequest : public RequestInfo { +class PingRequest : public Request { + private: + PingRequest() {} + public: - PingRequest() : RequestInfo(TYPE_PING) {} + static bool send(Net::Connection *connection, RequestManager &requestManager) { + Request *request = new PingRequest(); + + if(requestManager.sendRequest(connection, request)) + return true; + + delete request; + return false; + } + + virtual bool sendRequest(Net::Connection *connection, unsigned short requestId) { + if(isSent()) + return false; + + if(!connection->send(Net::Packet(Net::Packet::TYPE_PING, requestId))) + return false; + + setSent(); + return true; + } + + virtual bool handlePacket(Net::Connection*, const Net::Packet &packet) { + if(isFinished()) + return false; + + if(packet.getType() != Net::Packet::TYPE_PONG) + return false; // TODO Logging + + std::cout << "Received ping reply." << std::endl; + std::cout << " Request ID: " << packet.getRequestId() << std::endl; + + setFinished(); + return true; + } }; } diff --git a/src/Common/Request/Request.h b/src/Common/Request/Request.h new file mode 100644 index 0000000..211b2cd --- /dev/null +++ b/src/Common/Request/Request.h @@ -0,0 +1,57 @@ +/* + * Request.h + * + * Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de> + * + * 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 MAD_COMMON_REQUEST_REQUEST_H_ +#define MAD_COMMON_REQUEST_REQUEST_H_ + +namespace Mad { + +namespace Net { +class Connection; +class Packet; +} + +namespace Common { +namespace Request { + +class Request { + private: + bool sent, finished; + + protected: + Request() : sent(false), finished(false) {} + + void setSent() {sent = true;} + void setFinished() {finished = true;} + + public: + virtual ~Request() {} + + bool isSent() const {return sent;} + bool isFinished() const {return finished;} + + virtual bool sendRequest(Net::Connection *connection, unsigned short requestId) = 0; + virtual bool handlePacket(Net::Connection *connection, const Net::Packet &packet) = 0; +}; + +} +} +} + +#endif /* MAD_COMMON_REQUEST_REQUEST_H_ */ |