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
RakNetTransport2.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) 2016-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 #include "slikenet/NativeFeatureIncludes.h"
17 #if _RAKNET_SUPPORT_TelnetTransport==1
18
19 #include "slikenet/transport2.h"
20
21 #include "slikenet/peerinterface.h"
22 #include "slikenet/BitStream.h"
23 #include "slikenet/MessageIdentifiers.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include "slikenet/LinuxStrings.h"
28 #include "slikenet/linux_adapter.h"
29 #include "slikenet/osx_adapter.h"
30
31 using namespace SLNet;
32
33 STATIC_FACTORY_DEFINITIONS(RakNetTransport2,RakNetTransport2);
34
35 RakNetTransport2::RakNetTransport2()
36 {
37 }
38 RakNetTransport2::~RakNetTransport2()
39 {
40 Stop();
41 }
42 bool RakNetTransport2::Start(unsigned short port, bool serverMode)
43 {
44 (void) port;
45 (void) serverMode;
46 return true;
47 }
48 void RakNetTransport2::Stop(void)
49 {
50 newConnections.Clear(_FILE_AND_LINE_);
51 lostConnections.Clear(_FILE_AND_LINE_);
52 for (unsigned int i=0; i < packetQueue.Size(); i++)
53 {
54 rakFree_Ex(packetQueue[i]->data,_FILE_AND_LINE_);
55 SLNet::OP_DELETE(packetQueue[i],_FILE_AND_LINE_);
56 }
57 packetQueue.Clear(_FILE_AND_LINE_);
58 }
59 void RakNetTransport2::Send( SystemAddress systemAddress, const char *data, ... )
60 {
61 if (data==0 || data[0]==0) return;
62
63 char text[REMOTE_MAX_TEXT_INPUT];
64 va_list ap;
65 va_start(ap, data);
66 vsnprintf_s(text, REMOTE_MAX_TEXT_INPUT-1, data, ap);
67 va_end(ap);
68
69 SLNet::BitStream str;
70 str.Write((MessageID)ID_TRANSPORT_STRING);
71 str.Write(text, (int) strlen(text));
72 str.Write((unsigned char) 0); // Null terminate the string
73 rakPeerInterface->Send(&str, MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, systemAddress, (systemAddress==UNASSIGNED_SYSTEM_ADDRESS)!=0);
74 }
75 void RakNetTransport2::CloseConnection( SystemAddress systemAddress )
76 {
77 rakPeerInterface->CloseConnection(systemAddress, true, 0);
78 }
79 Packet* RakNetTransport2::Receive( void )
80 {
81 if (packetQueue.Size()==0)
82 return 0;
83 return packetQueue.Pop();
84 }
85 SystemAddress RakNetTransport2::HasNewIncomingConnection(void)
86 {
87 if (newConnections.Size())
88 return newConnections.Pop();
89 return UNASSIGNED_SYSTEM_ADDRESS;
90 }
91 SystemAddress RakNetTransport2::HasLostConnection(void)
92 {
93 if (lostConnections.Size())
94 return lostConnections.Pop();
95 return UNASSIGNED_SYSTEM_ADDRESS;
96 }
97 void RakNetTransport2::DeallocatePacket( Packet *packet )
98 {
99 rakFree_Ex(packet->data, _FILE_AND_LINE_ );
100 SLNet::OP_DELETE(packet, _FILE_AND_LINE_ );
101 }
102 PluginReceiveResult RakNetTransport2::OnReceive(Packet *packet)
103 {
104 switch (packet->data[0])
105 {
106 case ID_TRANSPORT_STRING:
107 {
108 if (packet->length==sizeof(MessageID))
109 return RR_STOP_PROCESSING_AND_DEALLOCATE;
110
111 Packet *p = SLNet::OP_NEW<Packet>(_FILE_AND_LINE_);
112 *p=*packet;
113 p->bitSize-=8;
114 p->length--;
115 p->data=(unsigned char*) rakMalloc_Ex(p->length,_FILE_AND_LINE_);
116 memcpy(p->data, packet->data+1, p->length);
117 packetQueue.Push(p, _FILE_AND_LINE_ );
118
119 }
120 return RR_STOP_PROCESSING_AND_DEALLOCATE;
121 }
122 return RR_CONTINUE_PROCESSING;
123 }
124 void RakNetTransport2::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
125 {
126 (void) rakNetGUID;
127 (void) lostConnectionReason;
128 lostConnections.Push(systemAddress, _FILE_AND_LINE_ );
129 }
130 void RakNetTransport2::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
131 {
132 (void) rakNetGUID;
133 (void) isIncoming;
134 newConnections.Push(systemAddress, _FILE_AND_LINE_ );
135 }
136
137 #endif // _RAKNET_SUPPORT_*