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
CommandParserInterface.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-2018, 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/CommandParserInterface.h"
17 #include "slikenet/TransportInterface.h"
18 #include <string.h>
19 #include "slikenet/assert.h"
20 #include <stdio.h>
21
22
23 #if defined(WINDOWS_STORE_RT)
24 #elif defined(_WIN32)
25 // IP_DONTFRAGMENT is different between winsock 1 and winsock 2. Therefore, Winsock2.h must be linked againt Ws2_32.lib
26 // winsock.h must be linked against WSock32.lib. If these two are mixed up the flag won't work correctly
27 #include <winsock2.h>
28
29 #else
30 #include <sys/socket.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #endif
34
35 #include "slikenet/LinuxStrings.h"
36
37 using namespace SLNet;
38
39 const unsigned char CommandParserInterface::VARIABLE_NUMBER_OF_PARAMETERS=255;
40
41 int SLNet::RegisteredCommandComp( const char* const & key, const RegisteredCommand &data )
42 {
43 return _stricmp(key,data.command);
44 }
45
46 CommandParserInterface::CommandParserInterface() {}
47 CommandParserInterface::~CommandParserInterface() {}
48
49 void CommandParserInterface::ParseConsoleString(char *str, const char delineator, unsigned char delineatorToggle, unsigned *numParameters, char **parameterList, unsigned parameterListLength)
50 {
51 unsigned strIndex, parameterListIndex;
52 unsigned strLen;
53 bool replaceDelineator=true;
54
55 strLen = (unsigned) strlen(str);
56
57 // Replace every instance of delineator, \n, \r with 0
58 for (strIndex=0; strIndex < strLen; strIndex++)
59 {
60 if (str[strIndex]==delineator && replaceDelineator)
61 str[strIndex]=0;
62
63 if (str[strIndex]=='\n' || str[strIndex]=='\r')
64 str[strIndex]=0;
65
66 if (str[strIndex]==delineatorToggle)
67 {
68 str[strIndex]=0;
69 replaceDelineator=!replaceDelineator;
70 }
71 }
72
73 // Fill up parameterList starting at each non-0
74 for (strIndex=0, parameterListIndex=0; strIndex < strLen; )
75 {
76 if (str[strIndex]!=0)
77 {
78 parameterList[parameterListIndex]=str+strIndex;
79 parameterListIndex++;
80 RakAssert(parameterListIndex < parameterListLength);
81 if (parameterListIndex >= parameterListLength)
82 break;
83
84 strIndex++;
85 while (str[strIndex]!=0 && strIndex < strLen)
86 strIndex++;
87 }
88 else
89 strIndex++;
90 }
91
92 parameterList[parameterListIndex]=0;
93 *numParameters=parameterListIndex;
94 }
95 void CommandParserInterface::SendCommandList(TransportInterface *transport, const SystemAddress &systemAddress)
96 {
97 unsigned i;
98 if (commandList.Size())
99 {
100 for (i=0; i < commandList.Size(); i++)
101 {
102 transport->Send(systemAddress, "%s", commandList[i].command);
103 if (i < commandList.Size()-1)
104 transport->Send(systemAddress, ", ");
105 }
106 transport->Send(systemAddress, "\r\n");
107 }
108 else
109 transport->Send(systemAddress, "No registered commands\r\n");
110 }
111 void CommandParserInterface::RegisterCommand(unsigned char parameterCount, const char *command, const char *commandHelp)
112 {
113 RegisteredCommand rc;
114 rc.command=command;
115 rc.commandHelp=commandHelp;
116 rc.parameterCount=parameterCount;
117 commandList.Insert( command, rc, true, _FILE_AND_LINE_);
118 }
119 bool CommandParserInterface::GetRegisteredCommand(const char *command, RegisteredCommand *rc)
120 {
121 bool objectExists;
122 unsigned index;
123 index=commandList.GetIndexFromKey(command, &objectExists);
124 if (objectExists)
125 *rc=commandList[index];
126 return objectExists;
127 }
128 void CommandParserInterface::OnTransportChange(TransportInterface *transport)
129 {
130 (void) transport;
131 }
132 void CommandParserInterface::OnNewIncomingConnection(const SystemAddress &systemAddress, TransportInterface *transport)
133 {
134 (void) systemAddress;
135 (void) transport;
136 }
137 void CommandParserInterface::OnConnectionLost(const SystemAddress &systemAddress, TransportInterface *transport)
138 {
139 (void) systemAddress;
140 (void) transport;
141 }
142 void CommandParserInterface::ReturnResult(bool res, const char *command,TransportInterface *transport, const SystemAddress &systemAddress)
143 {
144 if (res)
145 transport->Send(systemAddress, "%s returned true.\r\n", command);
146 else
147 transport->Send(systemAddress, "%s returned false.\r\n", command);
148 }
149 void CommandParserInterface::ReturnResult(int res, const char *command,TransportInterface *transport, const SystemAddress &systemAddress)
150 {
151 transport->Send(systemAddress, "%s returned %i.\r\n", command, res);
152 }
153 void CommandParserInterface::ReturnResult(const char *command, TransportInterface *transport, const SystemAddress &systemAddress)
154 {
155 transport->Send(systemAddress, "Successfully called %s.\r\n", command);
156 }
157 void CommandParserInterface::ReturnResult(char *res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress)
158 {
159 transport->Send(systemAddress, "%s returned %s.\r\n", command, res);
160 }
161 void CommandParserInterface::ReturnResult(SystemAddress res, const char *command, TransportInterface *transport, const SystemAddress &systemAddress)
162 {
163 char addr[128];
164 systemAddress.ToString(false,addr,static_cast<size_t>(128));
165 char addr2[128];
166 res.ToString(false,addr2,static_cast<size_t>(128));
167 transport->Send(systemAddress, "%s returned %s %s:%i\r\n", command,addr,addr2,res.GetPort());
168 }