1 | // |
2 | // Redistribution and use in source and binary forms, with or without |
3 | // modification, are permitted provided that the following conditions |
4 | // are met: |
5 | // * Redistributions of source code must retain the above copyright |
6 | // notice, this list of conditions and the following disclaimer. |
7 | // * Redistributions in binary form must reproduce the above copyright |
8 | // notice, this list of conditions and the following disclaimer in the |
9 | // documentation and/or other materials provided with the distribution. |
10 | // * Neither the name of NVIDIA CORPORATION nor the names of its |
11 | // contributors may be used to endorse or promote products derived |
12 | // from this software without specific prior written permission. |
13 | // |
14 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY |
15 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
17 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
18 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
19 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
20 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
21 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
22 | // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
24 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
25 | // |
26 | // Copyright (c) 2008-2021 NVIDIA Corporation. All rights reserved. |
27 | // Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved. |
28 | // Copyright (c) 2001-2004 NovodeX AG. All rights reserved. |
29 | |
30 | #include "PxPvdDefaultFileTransport.h" |
31 | |
32 | namespace physx |
33 | { |
34 | namespace pvdsdk |
35 | { |
36 | |
37 | PvdDefaultFileTransport::PvdDefaultFileTransport(const char* name) : mConnected(false), mWrittenData(0), mLocked(false) |
38 | { |
39 | mFileBuffer = PX_NEW(PsFileBuffer)(name, PxFileBuf::OPEN_WRITE_ONLY); |
40 | } |
41 | |
42 | PvdDefaultFileTransport::~PvdDefaultFileTransport() |
43 | { |
44 | } |
45 | |
46 | bool PvdDefaultFileTransport::connect() |
47 | { |
48 | PX_ASSERT(mFileBuffer); |
49 | mConnected = mFileBuffer->isOpen(); |
50 | return mConnected; |
51 | } |
52 | |
53 | void PvdDefaultFileTransport::disconnect() |
54 | { |
55 | mConnected = false; |
56 | } |
57 | |
58 | bool PvdDefaultFileTransport::isConnected() |
59 | { |
60 | return mConnected; |
61 | } |
62 | |
63 | bool PvdDefaultFileTransport::write(const uint8_t* inBytes, uint32_t inLength) |
64 | { |
65 | PX_ASSERT(mLocked); |
66 | PX_ASSERT(mFileBuffer); |
67 | if (mConnected) |
68 | { |
69 | uint32_t len = mFileBuffer->write(buffer: inBytes, size: inLength); |
70 | mWrittenData += len; |
71 | return len == inLength; |
72 | } |
73 | else |
74 | return false; |
75 | } |
76 | |
77 | PxPvdTransport& PvdDefaultFileTransport::lock() |
78 | { |
79 | mMutex.lock(); |
80 | PX_ASSERT(!mLocked); |
81 | mLocked = true; |
82 | return *this; |
83 | } |
84 | |
85 | void PvdDefaultFileTransport::unlock() |
86 | { |
87 | PX_ASSERT(mLocked); |
88 | mLocked = false; |
89 | mMutex.unlock(); |
90 | } |
91 | |
92 | void PvdDefaultFileTransport::flush() |
93 | { |
94 | } |
95 | |
96 | uint64_t PvdDefaultFileTransport::getWrittenDataSize() |
97 | { |
98 | return mWrittenData; |
99 | } |
100 | |
101 | void PvdDefaultFileTransport::release() |
102 | { |
103 | if (mFileBuffer) |
104 | { |
105 | mFileBuffer->close(); |
106 | delete mFileBuffer; |
107 | } |
108 | mFileBuffer = NULL; |
109 | PX_DELETE(this); |
110 | } |
111 | |
112 | class NullFileTransport : public physx::PxPvdTransport, public physx::shdfnd::UserAllocated |
113 | { |
114 | PX_NOCOPY(NullFileTransport) |
115 | public: |
116 | NullFileTransport(); |
117 | virtual ~NullFileTransport(); |
118 | |
119 | virtual bool connect(); |
120 | virtual void disconnect(); |
121 | virtual bool isConnected(); |
122 | |
123 | virtual bool write(const uint8_t* inBytes, uint32_t inLength); |
124 | |
125 | virtual PxPvdTransport& lock(); |
126 | virtual void unlock(); |
127 | |
128 | virtual void flush(); |
129 | |
130 | virtual uint64_t getWrittenDataSize(); |
131 | |
132 | virtual void release(); |
133 | |
134 | private: |
135 | bool mConnected; |
136 | uint64_t mWrittenData; |
137 | physx::shdfnd::Mutex mMutex; |
138 | bool mLocked; // for debug, remove it when finished |
139 | }; |
140 | |
141 | NullFileTransport::NullFileTransport() : mConnected(false), mWrittenData(0), mLocked(false) |
142 | { |
143 | } |
144 | |
145 | NullFileTransport::~NullFileTransport() |
146 | { |
147 | } |
148 | |
149 | bool NullFileTransport::connect() |
150 | { |
151 | mConnected = true; |
152 | return true; |
153 | } |
154 | |
155 | void NullFileTransport::disconnect() |
156 | { |
157 | mConnected = false; |
158 | } |
159 | |
160 | bool NullFileTransport::isConnected() |
161 | { |
162 | return mConnected; |
163 | } |
164 | |
165 | bool NullFileTransport::write(const uint8_t* /*inBytes*/, uint32_t inLength) |
166 | { |
167 | PX_ASSERT(mLocked); |
168 | if(mConnected) |
169 | { |
170 | uint32_t len = inLength; |
171 | mWrittenData += len; |
172 | return len == inLength; |
173 | } |
174 | else |
175 | return false; |
176 | } |
177 | |
178 | PxPvdTransport& NullFileTransport::lock() |
179 | { |
180 | mMutex.lock(); |
181 | PX_ASSERT(!mLocked); |
182 | mLocked = true; |
183 | return *this; |
184 | } |
185 | |
186 | void NullFileTransport::unlock() |
187 | { |
188 | PX_ASSERT(mLocked); |
189 | mLocked = false; |
190 | mMutex.unlock(); |
191 | } |
192 | |
193 | void NullFileTransport::flush() |
194 | { |
195 | } |
196 | |
197 | uint64_t NullFileTransport::getWrittenDataSize() |
198 | { |
199 | return mWrittenData; |
200 | } |
201 | |
202 | void NullFileTransport::release() |
203 | { |
204 | PX_DELETE(this); |
205 | } |
206 | |
207 | } // namespace pvdsdk |
208 | |
209 | PxPvdTransport* PxDefaultPvdFileTransportCreate(const char* name) |
210 | { |
211 | if(name) |
212 | return PX_NEW(pvdsdk::PvdDefaultFileTransport)(name); |
213 | else |
214 | return PX_NEW(pvdsdk::NullFileTransport)(); |
215 | } |
216 | |
217 | } // namespace physx |
218 | |
219 | |