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
318
319
|
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Copyright (c) 2018 NVIDIA Corporation. All rights reserved.
#include "PsCommStream.h"
#include "PsUserAllocated.h"
#include "RenderDebug.h"
#include "PsString.h"
#include "PxIntrinsics.h"
#include "PsIntrinsics.h"
#include <stdio.h>
#define MAX_STREAM_RECORD (1024*1024*2047) // no more than one gig
#define STREAM_VERSION_NUMBER 100
static char magic[4] = { 'N', 'V', 'C', 'S' };
class CommStreamImpl : public CommStream, public physx::shdfnd::UserAllocated
{
public:
CommStreamImpl(const char *streamFile,bool recordFile,CommLayer *c)
{
mCommLayer = c;
mIsRecord = recordFile;
mFirstFrame = true;
mIsValid = true;
mMaxPacketLength = (1024*1024)*16;
mPacketLength = 0;
mPacketLoc = 0;
mPacketData = static_cast<uint8_t *>(PX_ALLOC(mMaxPacketLength,"PacketData"));
mPacketBigEndian = false;
physx::shdfnd::strlcpy(mFilename,256,streamFile);
if ( mIsRecord )
{
mFph = NULL;
}
else
{
mFph = fopen(streamFile,"rb");
mIsValid = false;
if ( mFph )
{
char id[4] = { 0, 0, 0, 0};
uint32_t v1=0;
uint32_t v2=0;
uint32_t v3=0;
size_t res = fread(id,sizeof(id),1,mFph);
res = fread(&v1,sizeof(v1),1,mFph);
res = fread(&v2,sizeof(v2),1,mFph);
res = fread(&v3,sizeof(v3),1,mFph);
PX_UNUSED(res);
if ( id[0] == magic[0] &&
id[1] == magic[1] &&
id[2] == magic[2] &&
id[3] == magic[3] &&
v1 == STREAM_VERSION_NUMBER &&
v2 == RENDER_DEBUG_VERSION &&
v3 == RENDER_DEBUG_COMM_VERSION )
{
mIsValid = true;
readNextPacket();
}
}
}
}
virtual ~CommStreamImpl(void)
{
close();
}
void close(void)
{
PX_FREE(mPacketData);
mPacketData = NULL;
if ( mFph )
{
fclose(mFph);
mFph = NULL;
}
mPacketLength = 0;
mPacketLoc = 0;
mIsValid = false;
}
FILE *getFile(void)
{
if ( mFirstFrame )
{
mFph = fopen(mFilename,"wb");
if ( mFph )
{
uint32_t version = STREAM_VERSION_NUMBER;
fwrite(magic,sizeof(magic),1,mFph);
fwrite(&version,sizeof(version),1,mFph);
version = RENDER_DEBUG_VERSION;
fwrite(&version,sizeof(version),1,mFph);
version = RENDER_DEBUG_COMM_VERSION;
fwrite(&version,sizeof(version),1,mFph);
fflush(mFph);
}
mFirstFrame = false;
}
return mFph;
}
virtual void release(void)
{
delete this;
}
virtual CommLayer *getCommLayer(void)
{
return mCommLayer;
}
bool isValid(void) const
{
return mIsValid;
}
virtual bool isServer(void) const // return true if we are in server mode.
{
return mCommLayer->isServer();
}
virtual bool hasClient(void) const // return true if a client connection is currently established
{
bool ret = false;
if ( mIsRecord )
{
ret = mCommLayer->hasClient();
}
else
{
ret = mIsValid;
}
return ret;
}
virtual bool isConnected(void) const // return true if we are still connected to the server. The server is always in a 'connected' state.
{
return mCommLayer->isConnected();
}
virtual int32_t getPendingReadSize(void) const // returns the number of bytes of data which is pending to be read.
{
int32_t ret = 0;
if ( mIsRecord )
{
ret = mCommLayer->getPendingReadSize();
}
else
{
ret = int32_t(mPacketLength-mPacketLoc);
}
return ret;
}
virtual int32_t getPendingSendSize(void) const // return the number of bytes of data pending to be sent. This can be used for flow control
{
return mCommLayer->getPendingSendSize();
}
virtual bool sendMessage(const void *msg,uint32_t len)
{
bool ret = true;
if ( mIsRecord )
{
ret = mCommLayer->sendMessage(msg,len);
}
return ret;
}
virtual uint32_t peekMessage(bool &isBigEndianPacket) // return the length of the next pending message
{
uint32_t ret = 0;
if ( mIsRecord )
{
ret = mCommLayer->peekMessage(isBigEndianPacket);
}
else
{
ret = mPacketLength - mPacketLoc;
isBigEndianPacket = mPacketBigEndian;
}
return ret;
}
virtual uint32_t getMessage(void *msg,uint32_t maxLength,bool &isBigEndianPacket) // receives a pending message
{
uint32_t ret = 0;
if ( mIsRecord )
{
ret = mCommLayer->getMessage(msg,maxLength,isBigEndianPacket);
if ( ret && mIsRecord )
{
getFile();
if ( mFph )
{
uint32_t be = isBigEndianPacket ? 1U : 0U;
fwrite(&ret,sizeof(ret),1,mFph);
fwrite(&be,sizeof(be),1,mFph);
fwrite(msg,ret,1,mFph);
fflush(mFph);
size_t len = size_t(ftell(mFph));
if ( len > MAX_STREAM_RECORD )
{
fclose(mFph);
mFph = NULL;
}
}
}
}
else if ( mIsValid )
{
ret = mPacketLength - mPacketLoc;
if ( ret <= maxLength )
{
physx::intrinsics::memCopy(msg,&mPacketData[mPacketLoc],ret);
readNextPacket();
}
else
{
ret = maxLength;
physx::intrinsics::memCopy(msg,&mPacketData[mPacketLoc],ret);
mPacketLoc+=ret;
}
}
return ret;
}
void readNextPacket(void)
{
if ( mFph && !mIsRecord )
{
mPacketLoc = 0;
uint32_t be;
size_t r1 = fread(&mPacketLength,sizeof(mPacketLength),1,mFph);
size_t r2 = fread(&be,sizeof(be),1,mFph);
if ( r1 == 1 && r2 == 1 )
{
if ( mPacketLength > mMaxPacketLength )
{
mMaxPacketLength = mPacketLength + (1024*1024*10);
PX_FREE(mPacketData);
mPacketData = static_cast<uint8_t *>(PX_ALLOC(mMaxPacketLength,"PacketData"));
}
if ( mPacketData )
{
size_t r3 = fread(mPacketData,mPacketLength,1,mFph);
if ( r3 == 1 )
{
mPacketBigEndian = be ? true : false;
}
else
{
close();
}
}
else
{
close();
}
}
else
{
close();
}
}
}
uint32_t mMaxPacketLength;
uint32_t mPacketLoc;
uint32_t mPacketLength;
uint8_t *mPacketData;
bool mPacketBigEndian;
char mFilename[256];
FILE *mFph;
bool mFirstFrame;
bool mIsValid;
bool mIsRecord;
CommLayer *mCommLayer;
};
CommStream *createCommStream(const char *streamFile,bool recordFile,CommLayer *cl)
{
CommStreamImpl *c = PX_NEW(CommStreamImpl)(streamFile,recordFile,cl);
if ( !c->isValid() )
{
c->release();
c = NULL;
}
return static_cast< CommStream *>(c);
}
|