LRT14  1.0
 All Classes Namespaces Functions Variables Enumerations Enumerator
NetPeer.h
1 #ifndef RHESUS_NET_PEER_H_
2 #define RHESUS_NET_PEER_H_
3 
4 #include <stdio.h>
5 
6 #include <unistd.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <algorithm>
10 
11 #include <iostream>
12 #include <sstream>
13 #include <queue>
14 #include <map>
15 #include <stdarg.h>
16 
17 #include "Includes.h"
18 
19 #ifdef __VXWORKS__
20 #include <Task.h>
21 #elif defined(USE_BOOST)
22 #include <boost/thread.hpp>
23 #include <boost/signals2/mutex.hpp>
24 #endif
25 
26 #include "NetBuffer.h"
27 #include "NetConnectionType.h"
28 #include "NetChannel.h"
29 
30 #include "LibraryMessageType.h"
31 
32 // sleep for X milliseconds
33 #ifdef __VXWORKS__
34 #define NET_SLEEP(x) Wait (x / 1000.0)
35 #else
36 #define NET_SLEEP(x) usleep(x * 1000.0);
37 #endif
38 
39 #define SEND_FAILED_BUFFER_ALREADY_SENT -1000000000
40 #define SEND_FAILED_BUFFER_INVALID -1000000001
41 #define SEND_FAILED_UNKNOWN_ERROR -10000000002
42 
43 #define MAX_RECEIVE_BUFFER_SIZE 1024
44 #define MAX_MESSAGE_TRACK 256 // 256 gives us ample time to wait for an ACK. ~ 5 messages per frame * 50 frames per second gives us 6 extra packets.
45 
46 namespace Rhesus
47 {
48 namespace Messenger
49 {
50  class NetConnection;
51 
55  enum InternalMessageType
56  {
57  LIBRARY_DATA = 0x00,
58  USER_DATA = 0x01,
59  };
60 
61  struct MessageAwaitingACK
62  {
63  bool initialized;
64 
65  NetBuffer* buff;
66  double sentTime;
67 
68  bool acknowledged;
69 
70  NetConnection* recipient;
71  };
72 
77  class NetPeer
78  {
79  public:
83  NetPeer(char * ip, int port, NetConnectionType::Enum connType);
87  virtual ~NetPeer();
88 
92  int Open(int options=0, ...);
96  int Close();
97 
101  int Send(NetBuffer* buff, NetConnection* to, NetChannel::Enum method, int channel, int id=-1);
102 
107  protected:
108  void SendRaw(NetBuffer* nb, NetConnection* nc);
109  virtual void CheckMessages();
110 
111  bool _connected;
112 
113  std::vector<NetConnection*> m_netConnections;
114  void InternalPlatformConnectionListSynchronizationEnter();
115  void InternalPlatformConnectionListSynchronizationLeave();
116  private:
117  static const double kResendPacketTime; // TO-DO: make me configurable
118 
119 #ifdef __VXWORKS__
120  static INT32 InternalPlatformUpdateTaskWrapper(UINT32 instance);
121  static INT32 InternalPlatformMessageVerificationTaskWrapper(UINT32 instance);
122 
123 #elif defined(USE_BOOST)
124  static INT32 InternalPlatformUpdateTaskWrapper(NetPeer* instance);
125  static INT32 InternalPlatformMessageVerificationTaskWrapper(NetPeer* instance);
126 #endif
127 
128  void InternalPlatformQueueSynchronizationCreate();
129  void InternalPlatformQueueSynchronizationEnter();
130  void Update();
131  void InternalPlatformQueueSynchronizationLeave();
132 
133  void InternalPlatformConnectionListSynchronizationCreate();
134 
135  // reliable
136  void InternalPlatformReliableUnorderedQueueSynchronizationCreate();
137  void InternalPlatformReliableUnorderedQueueSynchronizationEnter();
138  void InternalPlatformReliableUnorderedQueueSynchronizationLeave();
139 
140  // reliable sequenced
141  void InternalPlatformReliableSequencedQueueSynchronizationCreate();
142  void InternalPlatformReliableSequencedQueueSynchronizationEnter();
143  void InternalPlatformReliableSequencedQueueSynchronizationLeave();
144 
145  // reliable in order
146  void InternalPlatformReliableInOrderQueueSynchronizationCreate();
147  void InternalPlatformReliableInOrderQueueSynchronizationEnter();
148  void InternalPlatformReliableInOrderQueueSynchronizationLeave();
149 
150  void InternalPlatformCreateUpdateTasks();
151  void InternalPlatformRunUpdateTasks();
152  void InternalPlatformDestroyUpdateTasks();
153 
154  char* m_ip;
155  int m_port;
156 
157 #ifdef __VXWORKS__
158  SEM_ID m_msgQueueMutex;
159 
160  SEM_ID m_connectionListMutex;
161 
162  SEM_ID m_reliableUnorderedQueueMutex, m_reliableSequencedQueueMutex, m_reliableInOrderQueueMutex;
163 
164  Task* m_internalUpdateTask;
165  Task* m_internalMessageVerificationTask;
166 
167 #elif defined(USE_BOOST)
168  boost::signals2::mutex* m_msgQueueMutex;
169  boost::signals2::mutex* m_connectionListMutex;
170  boost::signals2::mutex* m_reliableUnorderedQueueMutex, *m_reliableSequencedQueueMutex, *m_reliableInOrderQueueMutex;
171 
172  boost::thread* m_internalUpdateTask;
173  boost::thread* m_internalMessageVerificationTask;
174 #endif
175 
176  std::queue<NetBuffer*> m_receivedMessages;
177 
178  NetConnectionType::Enum m_connType;
179 
180  bool m_isRunning;
181 
182  int m_socket;
183  sockaddr_in m_socketEndpoint;
184 
185  std::map<int, MessageAwaitingACK> m_reliableUnordered[16];
186  std::map<int, MessageAwaitingACK> m_reliableSequenced[16];
187  std::map<int, MessageAwaitingACK> m_reliableOrdered[16];
188 
189  int* m_lastUnreliableSequenced; // int array of length 16 ints - one for each channel
190  int* m_lastReliableSequenced; // int array of length 16 ints - one for each channel
191 
192  int m_currentReliableUnorderedCounter;
193  int m_currentReliableSequencedCounter;
194  int m_currentReliableOrderedCounter;
195  int m_currentUnreliableSequencedCounter;
196  };
197 }
198 }
199 
200 #endif