File indexing completed on 2024-05-12 05:39:52

0001 /*************************************************************************
0002  *     Copyright (C) 2011 by Joseph Boudou                               *
0003  *     Copyright (C) 2014 by Renaud Guezennec                            *
0004  *                                                                       *
0005  *     https://rolisteam.org/                                         *
0006  *                                                                       *
0007  *   Rolisteam is free software; you can redistribute it and/or modify   *
0008  *   it under the terms of the GNU General Public License as published   *
0009  *   by the Free Software Foundation; either version 2 of the License,   *
0010  *   or (at your option) any later version.                              *
0011  *                                                                       *
0012  *   This program is distributed in the hope that it will be useful,     *
0013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of      *
0014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
0015  *   GNU General Public License for more details.                        *
0016  *                                                                       *
0017  *   You should have received a copy of the GNU General Public License   *
0018  *   along with this program; if not, write to the                       *
0019  *   Free Software Foundation, Inc.,                                     *
0020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           *
0021  *************************************************************************/
0022 
0023 #ifndef RECEIVE_EVENT_H
0024 #define RECEIVE_EVENT_H
0025 
0026 #include <QEvent>
0027 #include <QMap>
0028 #include <QObject>
0029 #include <QString>
0030 
0031 #include "network_global.h"
0032 #include "networkmessagereader.h"
0033 #include "networkreceiver.h"
0034 
0035 class NetworkLink;
0036 /**
0037  * @brief The ReceiveEvent class is an QEvent posted to the appplication to notify network message arrival.
0038  */
0039 class NETWORK_EXPORT ReceiveEvent : public QEvent
0040 {
0041 public:
0042     ReceiveEvent(const NetworkMessageHeader& header, const char* buffer, NetworkLink* link);
0043     ReceiveEvent(const ReceiveEvent& other);
0044     ~ReceiveEvent();
0045 
0046     static const int Type;
0047     /**
0048      * @brief postToReceiver
0049      */
0050     void postToReceiver();
0051 
0052     /**
0053      * @brief Post again this same event after a delay.
0054      */
0055     void repostLater() const;
0056     /**
0057      * @brief link
0058      * @return
0059      */
0060     NetworkLink* link() const;
0061     /**
0062      * @brief data
0063      * @return
0064      */
0065     NetworkMessageReader& data();
0066 
0067     /**
0068      * @brief hasReceiverFor
0069      * @param categorie
0070      * @param action
0071      * @return
0072      */
0073     static bool hasReceiverFor(quint8 categorie, quint8 action);
0074     /**
0075      * @brief registerReceiver
0076      * @param categorie
0077      * @param action
0078      * @param receiver
0079      */
0080     static void registerReceiver(NetMsg::Category categorie, NetMsg::Action action, QObject* receiver);
0081 
0082     /**
0083      * @brief hasNetworkReceiverFor
0084      * @param categorie
0085      * @return
0086      */
0087     static bool hasNetworkReceiverFor(NetMsg::Category categorie);
0088     /**
0089      * @brief getNetWorkReceiverFor
0090      * @param categorie
0091      * @return
0092      */
0093     static QList<NetWorkReceiver*> getNetWorkReceiverFor(NetMsg::Category categorie);
0094     /**
0095      * @brief registerNetworkReceiver
0096      * @param categorie
0097      * @param receiver
0098      */
0099     static void registerNetworkReceiver(NetMsg::Category categorie, NetWorkReceiver* receiver);
0100 
0101     static void removeNetworkReceiver(NetMsg::Category categorie, NetWorkReceiver* receiver);
0102 
0103 private:
0104     NetworkMessageReader m_data;
0105     NetworkLink* m_link;
0106     quint8 m_repost;
0107 
0108     static QMap<quint16, QObject*> s_receiverMap;
0109     static QMultiMap<NetMsg::Category, NetWorkReceiver*> ms_netWorkReceiverMap;
0110 };
0111 /**
0112  * @brief The DelayReceiveEvent class
0113  */
0114 class NETWORK_EXPORT DelayReceiveEvent : public QObject
0115 {
0116     Q_OBJECT
0117 
0118 public:
0119     DelayReceiveEvent(const ReceiveEvent& event);
0120     ~DelayReceiveEvent();
0121 
0122 private:
0123     ReceiveEvent* m_event;
0124 
0125 private slots:
0126     void postEvent();
0127 };
0128 
0129 #endif