| 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 "PxPvdDefaultSocketTransport.h" |
| 31 | |
| 32 | namespace physx |
| 33 | { |
| 34 | namespace pvdsdk |
| 35 | { |
| 36 | PvdDefaultSocketTransport::PvdDefaultSocketTransport(const char* host, int port, unsigned int timeoutInMilliseconds) |
| 37 | : mHost(host), mPort(uint16_t(port)), mTimeout(timeoutInMilliseconds), mConnected(false), mWrittenData(0) |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | PvdDefaultSocketTransport::~PvdDefaultSocketTransport() |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | bool PvdDefaultSocketTransport::connect() |
| 46 | { |
| 47 | if(mConnected) |
| 48 | return true; |
| 49 | |
| 50 | if(mSocket.connect(host: mHost, port: mPort, timeout: mTimeout)) |
| 51 | { |
| 52 | mSocket.setBlocking(true); |
| 53 | mConnected = true; |
| 54 | } |
| 55 | return mConnected; |
| 56 | } |
| 57 | |
| 58 | void PvdDefaultSocketTransport::disconnect() |
| 59 | { |
| 60 | mSocket.flush(); |
| 61 | mSocket.disconnect(); |
| 62 | mConnected = false; |
| 63 | } |
| 64 | |
| 65 | bool PvdDefaultSocketTransport::isConnected() |
| 66 | { |
| 67 | return mSocket.isConnected(); |
| 68 | } |
| 69 | |
| 70 | bool PvdDefaultSocketTransport::write(const uint8_t* inBytes, uint32_t inLength) |
| 71 | { |
| 72 | if(mConnected) |
| 73 | { |
| 74 | if(inLength == 0) |
| 75 | return true; |
| 76 | |
| 77 | uint32_t amountWritten = 0; |
| 78 | uint32_t totalWritten = 0; |
| 79 | do |
| 80 | { |
| 81 | // Sockets don't have to write as much as requested, so we need |
| 82 | // to wrap this call in a do/while loop. |
| 83 | // If they don't write any bytes then we consider them disconnected. |
| 84 | amountWritten = mSocket.write(data: inBytes, length: inLength); |
| 85 | inLength -= amountWritten; |
| 86 | inBytes += amountWritten; |
| 87 | totalWritten += amountWritten; |
| 88 | } while(inLength && amountWritten); |
| 89 | |
| 90 | if(amountWritten == 0) |
| 91 | return false; |
| 92 | |
| 93 | mWrittenData += totalWritten; |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | else |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | PxPvdTransport& PvdDefaultSocketTransport::lock() |
| 102 | { |
| 103 | mMutex.lock(); |
| 104 | return *this; |
| 105 | } |
| 106 | |
| 107 | void PvdDefaultSocketTransport::unlock() |
| 108 | { |
| 109 | mMutex.unlock(); |
| 110 | } |
| 111 | |
| 112 | void PvdDefaultSocketTransport::flush() |
| 113 | { |
| 114 | mSocket.flush(); |
| 115 | } |
| 116 | |
| 117 | uint64_t PvdDefaultSocketTransport::getWrittenDataSize() |
| 118 | { |
| 119 | return mWrittenData; |
| 120 | } |
| 121 | |
| 122 | void PvdDefaultSocketTransport::release() |
| 123 | { |
| 124 | PX_DELETE(this); |
| 125 | } |
| 126 | |
| 127 | } // namespace pvdsdk |
| 128 | |
| 129 | PxPvdTransport* PxDefaultPvdSocketTransportCreate(const char* host, int port, unsigned int timeoutInMilliseconds) |
| 130 | { |
| 131 | return PX_NEW(pvdsdk::PvdDefaultSocketTransport)(host, port, timeoutInMilliseconds); |
| 132 | } |
| 133 | |
| 134 | } // namespace physx |
| 135 | |