/
simxContainer.cpp
317 lines (287 loc) · 12.3 KB
/
simxContainer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "simxContainer.h"
#include "simxUtils.h"
#include "simLib.h"
CSimxContainer::CSimxContainer(bool isInputContainer)
{
_isInputContainer=isInputContainer;
_messageID=-1;
_dataTimeStamp=0;
_dataServerTime=0;
}
CSimxContainer::~CSimxContainer()
{
clearAll();
}
int CSimxContainer::getMessageID()
{
return(_messageID);
}
void CSimxContainer::setMessageID(int id)
{
_messageID=id;
}
void CSimxContainer::setDataTimeStamp(int ts)
{
_dataTimeStamp=ts;
}
void CSimxContainer::setDataServerTimeStamp(int st)
{
_dataServerTime=st;
}
void CSimxContainer::setSceneID(WORD id)
{
_sceneID=id;
}
void CSimxContainer::setServerState(BYTE serverState)
{
_serverState=serverState;
}
void CSimxContainer::clearAll()
{
for (unsigned int i=0;i<_allCommands.size();i++)
delete _allCommands[i];
_allCommands.clear();
for (unsigned int i=0;i<_partialCommands.size();i++)
delete[] _partialCommands[i];
_partialCommands.clear();
_messageID=-1;
}
int CSimxContainer::_arePartialCommandsSame(const char* buff1,const char* buff2,bool otherSideIsBigEndian)
{ // ret value: 0=not same, 1=same cmds, buff1 has same size than buff2, 2=buffers have different sizes
int cmd1=littleEndianIntConversion(((int*)(buff1+simx_cmdheaderoffset_cmd))[0],otherSideIsBigEndian)&simx_cmdmask;
int cmd2=littleEndianIntConversion(((int*)(buff2+simx_cmdheaderoffset_cmd))[0],otherSideIsBigEndian)&simx_cmdmask;
if (cmd1==cmd2)
{ // commands are same. Check the command data now:
if ((cmd1>simx_cmd4bytes_start)&&(cmd1<simx_cmd8bytes_start))
{
if ( ((int*)(buff1+SIMX_SUBHEADER_SIZE))[0]!=((int*)(buff2+SIMX_SUBHEADER_SIZE))[0] )
return(0);
}
if ((cmd1>simx_cmd8bytes_start)&&(cmd1<simx_cmd1string_start))
{
if ( ((int*)(buff1+SIMX_SUBHEADER_SIZE))[0]!=((int*)(buff2+SIMX_SUBHEADER_SIZE))[0] )
return(0);
if ( ((int*)(buff1+SIMX_SUBHEADER_SIZE+4))[0]!=((int*)(buff2+SIMX_SUBHEADER_SIZE+4))[0] )
return(0);
}
if ((cmd1>simx_cmd1string_start)&&(cmd1<simx_cmd4bytes2strings_start))
{
if (strcmp(buff1+SIMX_SUBHEADER_SIZE,buff2+SIMX_SUBHEADER_SIZE)!=0)
return(0);
}
if ((cmd1>simx_cmd4bytes2strings_start)&&(cmd1<simx_cmd4bytes2strings_end))
{
if ( ((int*)(buff1+SIMX_SUBHEADER_SIZE))[0]!=((int*)(buff2+SIMX_SUBHEADER_SIZE))[0] )
return(0);
if (strcmp(buff1+SIMX_SUBHEADER_SIZE+4,buff2+SIMX_SUBHEADER_SIZE+4)!=0)
return(0);
int l=int(strlen(buff1+SIMX_SUBHEADER_SIZE+4));
if (strcmp(buff1+SIMX_SUBHEADER_SIZE+4+l+1,buff2+SIMX_SUBHEADER_SIZE+4+l+1)!=0)
return(0);
}
int size1=littleEndianIntConversion(((int*)(buff1+simx_cmdheaderoffset_full_mem_size))[0],otherSideIsBigEndian);
int size2=littleEndianIntConversion(((int*)(buff2+simx_cmdheaderoffset_full_mem_size))[0],otherSideIsBigEndian);
if (size1==size2)
return(1);
return(2);
}
return(0);
}
char* CSimxContainer::addPartialCommand(const char* buffer,bool otherSideIsBigEndian)
{
for (unsigned int i=0;i<_partialCommands.size();i++)
{
int res=_arePartialCommandsSame(buffer,_partialCommands[i],otherSideIsBigEndian);
if (res==2)
{ // same commands, but somehow different memory footprints. Erase the one we have stored here and break from the loop
delete[] _partialCommands[i];
_partialCommands.erase(_partialCommands.begin()+i);
break;
}
else
{
if (res==1)
{ // same commands and same footprints. Merge the new command with the old:
int fullSize=littleEndianIntConversion(((int*)(buffer+simx_cmdheaderoffset_full_mem_size))[0],otherSideIsBigEndian);
int memSize=littleEndianIntConversion(((int*)(buffer+simx_cmdheaderoffset_mem_size))[0],otherSideIsBigEndian);
WORD pdataOffset0=littleEndianWordConversion(((WORD*)(buffer+simx_cmdheaderoffset_pdata_offset0))[0],otherSideIsBigEndian);
int pdataOffset1=littleEndianIntConversion(((int*)(buffer+simx_cmdheaderoffset_pdata_offset1))[0],otherSideIsBigEndian);
int pdataSize=memSize-SIMX_SUBHEADER_SIZE-pdataOffset0;
for (int cnt=0;cnt<pdataSize;cnt++)
_partialCommands[i][SIMX_SUBHEADER_SIZE+pdataOffset0+pdataOffset1+cnt]=buffer[SIMX_SUBHEADER_SIZE+pdataOffset0+cnt];
// Is that merged command complete yet?
if (SIMX_SUBHEADER_SIZE+pdataOffset0+pdataOffset1+pdataSize>=fullSize)
{ // yes!
char* retVal=_partialCommands[i];
_partialCommands.erase(_partialCommands.begin()+i);
return(retVal);
}
else
return(NULL);
}
}
}
// the partial command is not yet present! We simply add it:
int memSize=littleEndianIntConversion(((int*)(buffer+simx_cmdheaderoffset_mem_size))[0],otherSideIsBigEndian);
int fullSize=littleEndianIntConversion(((int*)(buffer+simx_cmdheaderoffset_full_mem_size))[0],otherSideIsBigEndian);
char* dat=new char[fullSize];
WORD pdataOffset0=littleEndianWordConversion(((WORD*)(buffer+simx_cmdheaderoffset_pdata_offset0))[0],otherSideIsBigEndian);
int pdataOffset1=littleEndianIntConversion(((int*)(buffer+simx_cmdheaderoffset_pdata_offset1))[0],otherSideIsBigEndian);
int pdataSize=memSize-SIMX_SUBHEADER_SIZE-pdataOffset0;
for (int cnt=0;cnt<SIMX_SUBHEADER_SIZE+pdataOffset0;cnt++)
dat[cnt]=buffer[cnt];
// We need to correct for the correct memory footprint here:
((int*)(dat+simx_cmdheaderoffset_mem_size))[0]=littleEndianIntConversion(fullSize,otherSideIsBigEndian);
for (int cnt=0;cnt<pdataSize;cnt++)
dat[SIMX_SUBHEADER_SIZE+pdataOffset0+pdataOffset1+cnt]=buffer[SIMX_SUBHEADER_SIZE+pdataOffset0+cnt];
_partialCommands.push_back(dat);
return(NULL);
}
void CSimxContainer::addCommand(CSimxCmd* cmd,bool doNotOverwriteSameCommand)
{
if (_isInputContainer)
{
int sci=_getIndexOfSimilarCommand(cmd);
if ((sci==-1)||doNotOverwriteSameCommand)
_allCommands.push_back(cmd); // command not yet there. just append it
else
{ // Command already there
if ((cmd->getOperationMode()==simx_opmode_oneshot_split)&&(_allCommands[sci]->getOperationMode()==simx_opmode_oneshot_split))
{ // let the command finish, and only in above case (don't do this with continuous commands!!). Special case!
delete cmd;
}
else
{
delete _allCommands[sci]; // we remove the older command
_allCommands[sci]=cmd; // we replace the old command with the new. Discontinue commands have to be relayed back to the client (which might have a continuous command to clear there too)
}
}
}
else
_allCommands.push_back(cmd);
}
int CSimxContainer::_getIndexOfSimilarCommand(CSimxCmd* cmd)
{
for (unsigned int i=0;i<_allCommands.size();i++)
{
if (_allCommands[i]->areCommandAndCommandDataSame(cmd))
return((int)i);
}
return(-1);
}
void CSimxContainer::executeCommands(CSimxContainer* outputContainer,CSimxSocket* sock)
{
if (outputContainer->getCommandCount()==0)
{ // Make sure the output buffer was already sent and is empty
outputContainer->setMessageID(_messageID);
outputContainer->setDataTimeStamp(_dataTimeStamp);
int simState=simGetSimulationState();
BYTE serverState=0;
outputContainer->setDataServerTimeStamp(int(simGetSystemTime()*1000.1f));
if (simState!=sim_simulation_stopped)
{
serverState|=1; // simulation is not stopped
if (simState==sim_simulation_paused)
serverState|=2; // simulation is paused
}
if (simGetRealTimeSimulation()>0)
serverState|=4;
int editModeType;
simGetIntegerParameter(sim_intparam_edit_mode_type,&editModeType);
serverState|=(editModeType<<3);
int sceneUniqueID;
simGetIntegerParameter(sim_intparam_scene_unique_id,&sceneUniqueID);
outputContainer->setSceneID(WORD(sceneUniqueID));
outputContainer->setServerState(serverState);
// Prepare for correct error reporting:
int errorModeSaved;
simGetIntegerParameter(sim_intparam_error_report_mode,&errorModeSaved);
simSetIntegerParameter(sim_intparam_error_report_mode,sim_api_errormessage_report);
char* err=simGetLastError(); // just clear the last error
if (err!=NULL)
simReleaseBuffer(err);
// Execute pending commands:
for (unsigned int i=0;i<_allCommands.size();i++)
{
CSimxCmd* outputCmd=_allCommands[i]->execute(sock,_otherSideIsBigEndian);
if (outputCmd!=NULL) // gradual or split commands are not added here!
outputContainer->addCommand(outputCmd,false);
}
// Restore previous error report mode:
simSetIntegerParameter(sim_intparam_error_report_mode,errorModeSaved);
_removeNonContinuousCommands(); // simx_opmode_oneshot_split commands are also kept!
}
}
void CSimxContainer::_removeNonContinuousCommands()
{ // this also keeps simx_opmode_oneshot_split commands!!!
for (unsigned int i=0;i<_allCommands.size();i++)
{
if ( (_allCommands[i]->getOperationMode()!=simx_opmode_continuous)&&(_allCommands[i]->getOperationMode()!=simx_opmode_continuous_split)&&(_allCommands[i]->getOperationMode()!=simx_opmode_oneshot_split) )
{
delete _allCommands[i];
_allCommands.erase(_allCommands.begin()+i);
i--; // reprocess this position
}
}
}
int CSimxContainer::getStreamCommandCount()
{
int retVal=0;
if (_isInputContainer)
return(0); // apply only on output containers
for (unsigned int i=0;i<_allCommands.size();i++)
{
if (_allCommands[i]->getOperationMode()==simx_opmode_streaming)
retVal++;
}
return(retVal);
}
int CSimxContainer::getDataString(std::vector<char>& dataString,bool otherSideIsBigEndian)
{ // returns the number of commands fetched
if (_isInputContainer)
return(0); // apply only on output containers
dataString.clear();
// 1. Prepare the header:
char header[SIMX_HEADER_SIZE];
// The CRC is added later
header[simx_headeroffset_version]=SIMX_VERSION;
((int*)(header+simx_headeroffset_message_id))[0]=littleEndianIntConversion(_messageID,otherSideIsBigEndian);
((int*)(header+simx_headeroffset_client_time))[0]=littleEndianIntConversion(_dataTimeStamp,otherSideIsBigEndian);
((int*)(header+simx_headeroffset_server_time))[0]=littleEndianIntConversion(_dataServerTime,otherSideIsBigEndian);
((WORD*)(header+simx_headeroffset_scene_id))[0]=littleEndianWordConversion(_sceneID,otherSideIsBigEndian);
header[simx_headeroffset_server_state]=_serverState;
for (int i=0;i<SIMX_HEADER_SIZE;i++)
dataString.push_back(header[i]);
// 2. Prepare the individual commands (or command replies). Only non-split and non-gradual commands are taken here:
for (unsigned int i=0;i<_allCommands.size();i++)
_allCommands[i]->appendYourData(dataString,_otherSideIsBigEndian);
return(int(_allCommands.size()));
}
int CSimxContainer::getDataStringOfSplitOrGradualCommands(std::vector<char>& dataString,bool otherSideIsBigEndian)
{ // returns the number of commands fetched
if (!_isInputContainer)
return(0); // apply only on input containers
// Take care only of split or gradual commands:
int fetchedCnt=0;
for (unsigned int i=0;i<_allCommands.size();i++)
{
bool removeCommand=false;
if (_allCommands[i]->appendYourMemorizedSplitData(true,dataString,removeCommand,_otherSideIsBigEndian))
fetchedCnt++;
if (removeCommand)
{
delete _allCommands[i];
_allCommands.erase(_allCommands.begin()+i);
i--; // reprocess this position
}
}
return(fetchedCnt);
}
int CSimxContainer::getCommandCount()
{
return((int)_allCommands.size());
}
void CSimxContainer::setOtherSideIsBigEndian(bool bigEndian)
{
_otherSideIsBigEndian=bigEndian;
}