i6engine  1.0
NetworkManager.h
Go to the documentation of this file.
1 /*
2  * i6engine
3  * Copyright (2016) Daniel Bonrath, Michael Baer, All rights reserved.
4  *
5  * This file is part of i6engine; i6engine 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 02110-1301 USA
18  */
19 
25 #ifndef __I6ENGINE_MODULES_NETWORKMANAGER_H__
26 #define __I6ENGINE_MODULES_NETWORKMANAGER_H__
27 
28 #include <map>
29 
31 
33 
35 
37 
38 #include "boost/thread/mutex.hpp"
39 
40 #include "m2etis/message/M2Message.h"
41 #include "m2etis/pubsub/DeliverCallbackInterface.h"
42 
43 namespace m2etis {
44 namespace pubsub {
45  enum ChannelName : uint16_t;
46  class PubSubSystem;
47 } /* namespace pubsub */
48 } /* namespace m2etis */
49 
50 namespace i6e {
51 namespace modules {
52 
53  class NetworkController;
54  class NetworkMailbox;
55 
56  class NetworkManager : public m2etis::pubsub::BasicDeliverCallbackInterface<api::GameMessage> {
57  friend class NetworkController;
58  friend class NetworkErrors;
59  friend class NetworkMailbox;
60 
61  private:
65  NetworkMailbox * _mailbox;
66 
67  bool _sendPing;
68  int32_t _counterSent;
69  int32_t _counterReceived;
70 
71  NetworkErrors _ne;
72 
73  m2etis::pubsub::PubSubSystem * _pubsub;
74 
75  uint64_t _lastTickTime;
76 
77  volatile bool _connectionFailed;
78 
79  std::map<uint16_t, uint32_t> _usedChannels;
80 
81  boost::mutex _pubSubLock;
82 
86  explicit NetworkManager(NetworkMailbox * mailbox);
87 
91  ~NetworkManager();
92 
96  void subscribe(uint16_t channel); // TODO: (Daniel) add second subscribe method registering a filter predicate too
97 
101  void unsubscribe(uint16_t channel); // TODO: (Daniel) add second unsubscribe method registering a filter predicate too
102 
106  void publish(uint16_t channel, const api::GameMessage::Ptr & msg);
107 
111  bool connect(const core::IPKey & remote, const core::IPKey & local);
112 
116  void disconnect();
117 
121  void deliverCallback(const m2etis::message::M2Message<api::GameMessage>::Ptr msg) override;
122 
126  void connectionFailed() { _connectionFailed = true; }
127 
131  void publishInternal(m2etis::pubsub::ChannelName channel, const m2etis::message::M2Message<api::GameMessage>::Ptr & msg) const;
132 
138  void enablePing(bool enabled);
139 
143  void Tick();
144 
148  NetworkManager(const NetworkManager &) = delete;
149 
153  NetworkManager & operator=(const NetworkManager &) = delete;
154 
156  };
157 
158 } /* namespace modules */
159 } /* namespace i6e */
160 
161 #endif /* __I6ENGINE_MODULES_NETWORKMANAGER_H__ */
162 
boost::shared_ptr< Message > Ptr
Definition: Message.h:66
#define ASSERT_THREAD_SAFETY_HEADER
Extracts the contents of incoming messages on the "Network" channel and calls the corresponding metho...
This class performs and provides various methods to the engine for the network.