1 #ifndef RHESUS_NET_PEER_H_
2 #define RHESUS_NET_PEER_H_
8 #include <sys/socket.h>
21 #elif defined(USE_BOOST)
22 #include <boost/thread.hpp>
23 #include <boost/signals2/mutex.hpp>
26 #include "NetBuffer.h"
27 #include "NetConnectionType.h"
28 #include "NetChannel.h"
30 #include "LibraryMessageType.h"
34 #define NET_SLEEP(x) Wait (x / 1000.0)
36 #define NET_SLEEP(x) usleep(x * 1000.0);
39 #define SEND_FAILED_BUFFER_ALREADY_SENT -1000000000
40 #define SEND_FAILED_BUFFER_INVALID -1000000001
41 #define SEND_FAILED_UNKNOWN_ERROR -10000000002
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.
55 enum InternalMessageType
61 struct MessageAwaitingACK
70 NetConnection* recipient;
83 NetPeer(
char * ip,
int port, NetConnectionType::Enum connType);
92 int Open(
int options=0, ...);
108 void SendRaw(
NetBuffer* nb, NetConnection* nc);
109 virtual void CheckMessages();
113 std::vector<NetConnection*> m_netConnections;
114 void InternalPlatformConnectionListSynchronizationEnter();
115 void InternalPlatformConnectionListSynchronizationLeave();
117 static const double kResendPacketTime;
120 static INT32 InternalPlatformUpdateTaskWrapper(UINT32 instance);
121 static INT32 InternalPlatformMessageVerificationTaskWrapper(UINT32 instance);
123 #elif defined(USE_BOOST)
124 static INT32 InternalPlatformUpdateTaskWrapper(
NetPeer* instance);
125 static INT32 InternalPlatformMessageVerificationTaskWrapper(
NetPeer* instance);
128 void InternalPlatformQueueSynchronizationCreate();
129 void InternalPlatformQueueSynchronizationEnter();
131 void InternalPlatformQueueSynchronizationLeave();
133 void InternalPlatformConnectionListSynchronizationCreate();
136 void InternalPlatformReliableUnorderedQueueSynchronizationCreate();
137 void InternalPlatformReliableUnorderedQueueSynchronizationEnter();
138 void InternalPlatformReliableUnorderedQueueSynchronizationLeave();
141 void InternalPlatformReliableSequencedQueueSynchronizationCreate();
142 void InternalPlatformReliableSequencedQueueSynchronizationEnter();
143 void InternalPlatformReliableSequencedQueueSynchronizationLeave();
146 void InternalPlatformReliableInOrderQueueSynchronizationCreate();
147 void InternalPlatformReliableInOrderQueueSynchronizationEnter();
148 void InternalPlatformReliableInOrderQueueSynchronizationLeave();
150 void InternalPlatformCreateUpdateTasks();
151 void InternalPlatformRunUpdateTasks();
152 void InternalPlatformDestroyUpdateTasks();
158 SEM_ID m_msgQueueMutex;
160 SEM_ID m_connectionListMutex;
162 SEM_ID m_reliableUnorderedQueueMutex, m_reliableSequencedQueueMutex, m_reliableInOrderQueueMutex;
164 Task* m_internalUpdateTask;
165 Task* m_internalMessageVerificationTask;
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;
172 boost::thread* m_internalUpdateTask;
173 boost::thread* m_internalMessageVerificationTask;
176 std::queue<NetBuffer*> m_receivedMessages;
178 NetConnectionType::Enum m_connType;
183 sockaddr_in m_socketEndpoint;
185 std::map<int, MessageAwaitingACK> m_reliableUnordered[16];
186 std::map<int, MessageAwaitingACK> m_reliableSequenced[16];
187 std::map<int, MessageAwaitingACK> m_reliableOrdered[16];
189 int* m_lastUnreliableSequenced;
190 int* m_lastReliableSequenced;
192 int m_currentReliableUnorderedCounter;
193 int m_currentReliableSequencedCounter;
194 int m_currentReliableOrderedCounter;
195 int m_currentUnreliableSequencedCounter;