SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PluginInterface2.cpp
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
16
17 #include "slikenet/PluginInterface2.h"
18 #include "slikenet/PacketizedTCP.h"
19 #include "slikenet/peerinterface.h"
20 #include "slikenet/BitStream.h"
21
22 using namespace SLNet;
23
24 PluginInterface2::PluginInterface2()
25 {
26 rakPeerInterface=0;
27 #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
28 tcpInterface=0;
29 #endif
30 }
31 PluginInterface2::~PluginInterface2()
32 {
33
34 }
35 void PluginInterface2::SendUnified( const SLNet::BitStream * bitStream, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast )
36 {
37 if (rakPeerInterface)
38 {
39 rakPeerInterface->Send(bitStream,priority,reliability,orderingChannel,systemIdentifier,broadcast);
40 return;
41 }
42 #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
43 else if (tcpInterface)
44 {
45 tcpInterface->Send((const char*) bitStream->GetData(), bitStream->GetNumberOfBytesUsed(), systemIdentifier.systemAddress, broadcast);
46 return;
47 }
48 #endif
49
50 // Offline mode
51 if (broadcast==false && systemIdentifier.rakNetGuid==GetMyGUIDUnified())
52 {
53 // Packet *packet = AllocatePacketUnified(bitStream->GetNumberOfBytesUsed());
54 // memcpy(packet->data, bitStream->GetData(), bitStream->GetNumberOfBytesUsed());
55 Packet packet;
56 packet.bitSize=bitStream->GetNumberOfBitsUsed();
57 packet.data=bitStream->GetData();
58 packet.deleteData=false;
59 packet.guid=UNASSIGNED_RAKNET_GUID;
60 packet.length=bitStream->GetNumberOfBytesUsed();
61 packet.systemAddress=UNASSIGNED_SYSTEM_ADDRESS;
62 packet.wasGeneratedLocally=false;
63 OnReceive(&packet);
64 // DeallocPacketUnified(packet);
65
66 Update();
67 }
68 }
69 void PluginInterface2::SendUnified( const char * data, const int length, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast )
70 {
71 if (rakPeerInterface)
72 {
73 rakPeerInterface->Send(data, length, priority,reliability,orderingChannel,systemIdentifier,broadcast);
74 return;
75 }
76 #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
77 else if (tcpInterface)
78 {
79 tcpInterface->Send(data, length, systemIdentifier.systemAddress, broadcast);
80 return;
81 }
82 #endif
83
84 // Offline mode
85 if (broadcast==false && systemIdentifier.rakNetGuid==GetMyGUIDUnified())
86 {
87 // Packet *packet = AllocatePacketUnified(bitStream->GetNumberOfBytesUsed());
88 // memcpy(packet->data, bitStream->GetData(), bitStream->GetNumberOfBytesUsed());
89 Packet packet;
90 packet.bitSize=BYTES_TO_BITS(length);
91 packet.data=(unsigned char*) data;
92 packet.deleteData=false;
93 packet.guid=UNASSIGNED_RAKNET_GUID;
94 packet.length=length;
95 packet.systemAddress=UNASSIGNED_SYSTEM_ADDRESS;
96 packet.wasGeneratedLocally=false;
97 OnReceive(&packet);
98 // DeallocPacketUnified(packet);
99
100 Update();
101 }
102 }
103 Packet *PluginInterface2::AllocatePacketUnified(unsigned dataSize)
104 {
105 if (rakPeerInterface)
106 {
107 return rakPeerInterface->AllocatePacket(dataSize);
108 }
109 #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
110 else if (tcpInterface)
111 {
112 return tcpInterface->AllocatePacket(dataSize);
113 }
114 #endif
115
116 Packet *packet = SLNet::OP_NEW<Packet>(_FILE_AND_LINE_);
117 packet->data = (unsigned char*) rakMalloc_Ex(dataSize, _FILE_AND_LINE_);
118 packet->bitSize=BYTES_TO_BITS(dataSize);
119 packet->deleteData=true;
120 packet->guid=UNASSIGNED_RAKNET_GUID;
121 packet->systemAddress=UNASSIGNED_SYSTEM_ADDRESS;
122 packet->wasGeneratedLocally=false;
123 return packet;
124 }
125 void PluginInterface2::PushBackPacketUnified(Packet *packet, bool pushAtHead)
126 {
127 if (rakPeerInterface)
128 {
129 rakPeerInterface->PushBackPacket(packet,pushAtHead);
130 return;
131 }
132 #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
133 else if (tcpInterface)
134 {
135 tcpInterface->PushBackPacket(packet,pushAtHead);
136 return;
137 }
138 #endif
139
140 OnReceive(packet);
141 Update();
142 }
143 void PluginInterface2::DeallocPacketUnified(Packet *packet)
144 {
145 if (rakPeerInterface)
146 {
147 rakPeerInterface->DeallocatePacket(packet);
148 return;
149 }
150 #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
151 else if (tcpInterface)
152 {
153 tcpInterface->DeallocatePacket(packet);
154 return;
155 }
156 #endif
157
158 rakFree_Ex(packet->data, _FILE_AND_LINE_);
159 SLNet::OP_DELETE(packet, _FILE_AND_LINE_);
160 }
161 bool PluginInterface2::SendListUnified( const char **data, const int *lengths, const int numParameters, PacketPriority priority, PacketReliability reliability, char orderingChannel, const AddressOrGUID systemIdentifier, bool broadcast )
162 {
163 if (rakPeerInterface)
164 {
165 return rakPeerInterface->SendList(data,lengths,numParameters,priority,reliability,orderingChannel,systemIdentifier,broadcast)!=0;
166 }
167 #if _RAKNET_SUPPORT_PacketizedTCP==1 && _RAKNET_SUPPORT_TCPInterface==1
168 else if (tcpInterface)
169 {
170 return tcpInterface->SendList(data,(const unsigned int *) lengths,numParameters,systemIdentifier.systemAddress,broadcast );
171 }
172 #endif
173
174 if (broadcast==false && systemIdentifier.rakNetGuid==GetMyGUIDUnified())
175 {
176
177 unsigned int totalLength=0;
178 unsigned int lengthOffset;
179 int i;
180 for (i=0; i < numParameters; i++)
181 {
182 if (lengths[i]>0)
183 totalLength+=lengths[i];
184 }
185 if (totalLength==0)
186 return false;
187
188 char *dataAggregate;
189 dataAggregate = (char*) rakMalloc_Ex( (size_t) totalLength, _FILE_AND_LINE_ );
190 if (dataAggregate==0)
191 {
192 notifyOutOfMemory(_FILE_AND_LINE_);
193 return false;
194 }
195 for (i=0, lengthOffset=0; i < numParameters; i++)
196 {
197 if (lengths[i]>0)
198 {
199 memcpy(dataAggregate+lengthOffset, data[i], lengths[i]);
200 lengthOffset+=lengths[i];
201 }
202 }
203
204 SendUnified(dataAggregate, totalLength, priority, reliability,orderingChannel, systemIdentifier, broadcast);
205 rakFree_Ex(dataAggregate, _FILE_AND_LINE_);
206 return true;
207 }
208
209 return false;
210 }
211 void PluginInterface2::SetRakPeerInterface( RakPeerInterface *ptr )
212 {
213 rakPeerInterface=ptr;
214 }
215 #if _RAKNET_SUPPORT_TCPInterface==1
216 void PluginInterface2::SetTCPInterface( TCPInterface *ptr )
217 {
218 tcpInterface=ptr;
219 }
220 #endif
221 RakNetGUID PluginInterface2::GetMyGUIDUnified(void) const
222 {
223 if (rakPeerInterface)
224 return rakPeerInterface->GetMyGUID();
225 return UNASSIGNED_RAKNET_GUID;
226 }