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