[74776] | 1 | /* $Id: DataStreamImpl.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[76592] | 3 | * VirtualBox COM class implementation - DataStream
|
---|
[74776] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[96407] | 7 | * Copyright (C) 2018-2022 Oracle and/or its affiliates.
|
---|
[74776] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[74776] | 26 | */
|
---|
| 27 |
|
---|
| 28 |
|
---|
| 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
[76592] | 32 | #define LOG_GROUP LOG_GROUP_MAIN_DATASTREAM
|
---|
[74776] | 33 | #include "DataStreamImpl.h"
|
---|
| 34 |
|
---|
| 35 | #include "AutoCaller.h"
|
---|
[76592] | 36 | #include "LoggingNew.h"
|
---|
[76474] | 37 | #include <iprt/errcore.h>
|
---|
[74776] | 38 |
|
---|
| 39 |
|
---|
| 40 | /*********************************************************************************************************************************
|
---|
| 41 | * Boilerplate constructor & destructor *
|
---|
| 42 | *********************************************************************************************************************************/
|
---|
| 43 |
|
---|
| 44 | DEFINE_EMPTY_CTOR_DTOR(DataStream)
|
---|
| 45 |
|
---|
| 46 | HRESULT DataStream::FinalConstruct()
|
---|
| 47 | {
|
---|
| 48 | LogFlowThisFunc(("\n"));
|
---|
| 49 | return BaseFinalConstruct();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | void DataStream::FinalRelease()
|
---|
| 53 | {
|
---|
| 54 | LogFlowThisFuncEnter();
|
---|
| 55 | uninit();
|
---|
| 56 | BaseFinalRelease();
|
---|
| 57 | LogFlowThisFuncLeave();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | /*********************************************************************************************************************************
|
---|
| 62 | * Initializer & uninitializer *
|
---|
| 63 | *********************************************************************************************************************************/
|
---|
| 64 |
|
---|
| 65 | /**
|
---|
| 66 | * Initializes the DataStream object.
|
---|
| 67 | *
|
---|
| 68 | * @param aBufferSize Size of the intermediate buffer.
|
---|
| 69 | *
|
---|
| 70 | */
|
---|
| 71 | HRESULT DataStream::init(unsigned long aBufferSize)
|
---|
| 72 | {
|
---|
| 73 | LogFlowThisFunc(("cbBuffer=%zu\n", aBufferSize));
|
---|
| 74 |
|
---|
| 75 | /*
|
---|
| 76 | * Enclose the state transition NotReady->InInit->Ready
|
---|
| 77 | */
|
---|
| 78 | AutoInitSpan autoInitSpan(this);
|
---|
| 79 | AssertReturn(autoInitSpan.isOk(), E_FAIL);
|
---|
| 80 |
|
---|
| 81 | /*
|
---|
| 82 | * Allocate data instance.
|
---|
| 83 | */
|
---|
| 84 | HRESULT hrc = S_OK;
|
---|
| 85 |
|
---|
| 86 | m_hSemEvtDataAvail = NIL_RTSEMEVENT;
|
---|
| 87 | m_hSemEvtBufSpcAvail = NIL_RTSEMEVENT;
|
---|
[74823] | 88 | m_pBuffer = NULL;
|
---|
[79582] | 89 | m_fEos = false;
|
---|
[74776] | 90 | int vrc = RTSemEventCreate(&m_hSemEvtDataAvail);
|
---|
| 91 | if (RT_SUCCESS(vrc))
|
---|
| 92 | vrc = RTSemEventCreate(&m_hSemEvtBufSpcAvail);
|
---|
[74823] | 93 | if (RT_SUCCESS(vrc))
|
---|
| 94 | vrc = RTCircBufCreate(&m_pBuffer, aBufferSize);
|
---|
[74776] | 95 |
|
---|
| 96 | if (RT_FAILURE(vrc))
|
---|
| 97 | hrc = setErrorBoth(E_FAIL, vrc, tr("Failed to initialize data stream object (%Rrc)"), vrc);
|
---|
| 98 |
|
---|
| 99 | /*
|
---|
| 100 | * Done. Just update object readiness state.
|
---|
| 101 | */
|
---|
| 102 | if (SUCCEEDED(hrc))
|
---|
| 103 | autoInitSpan.setSucceeded();
|
---|
| 104 | else
|
---|
| 105 | autoInitSpan.setFailed(hrc);
|
---|
| 106 |
|
---|
| 107 | LogFlowThisFunc(("returns %Rhrc\n", hrc));
|
---|
| 108 | return hrc;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Uninitializes the instance (called from FinalRelease()).
|
---|
| 113 | */
|
---|
| 114 | void DataStream::uninit()
|
---|
| 115 | {
|
---|
| 116 | LogFlowThisFuncEnter();
|
---|
| 117 |
|
---|
| 118 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
| 119 | AutoUninitSpan autoUninitSpan(this);
|
---|
| 120 | if (!autoUninitSpan.uninitDone())
|
---|
| 121 | {
|
---|
| 122 | if (m_hSemEvtDataAvail != NIL_RTSEMEVENT)
|
---|
| 123 | RTSemEventDestroy(m_hSemEvtDataAvail);
|
---|
| 124 | if (m_hSemEvtBufSpcAvail != NIL_RTSEMEVENT)
|
---|
| 125 | RTSemEventDestroy(m_hSemEvtBufSpcAvail);
|
---|
[74823] | 126 | if (m_pBuffer != NULL)
|
---|
| 127 | RTCircBufDestroy(m_pBuffer);
|
---|
[74776] | 128 | m_hSemEvtDataAvail = NIL_RTSEMEVENT;
|
---|
| 129 | m_hSemEvtBufSpcAvail = NIL_RTSEMEVENT;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | LogFlowThisFuncLeave();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | /*********************************************************************************************************************************
|
---|
[74829] | 137 | * IDataStream attributes *
|
---|
[74776] | 138 | *********************************************************************************************************************************/
|
---|
| 139 |
|
---|
| 140 | HRESULT DataStream::getReadSize(ULONG *aReadSize)
|
---|
| 141 | {
|
---|
| 142 | AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
[74823] | 143 | *aReadSize = (ULONG)RTCircBufUsed(m_pBuffer);
|
---|
[74776] | 144 | return S_OK;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | /*********************************************************************************************************************************
|
---|
[74829] | 149 | * IDataStream methods *
|
---|
[74776] | 150 | *********************************************************************************************************************************/
|
---|
| 151 |
|
---|
| 152 | HRESULT DataStream::read(ULONG aSize, ULONG aTimeoutMS, std::vector<BYTE> &aData)
|
---|
| 153 | {
|
---|
| 154 | /*
|
---|
| 155 | * Allocate return buffer.
|
---|
| 156 | */
|
---|
| 157 | try
|
---|
| 158 | {
|
---|
| 159 | aData.resize(aSize);
|
---|
| 160 | }
|
---|
| 161 | catch (std::bad_alloc &)
|
---|
| 162 | {
|
---|
| 163 | return E_OUTOFMEMORY;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | /*
|
---|
| 167 | * Do the reading. To play safe we exclusivly lock the object while doing this.
|
---|
| 168 | */
|
---|
| 169 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 170 |
|
---|
| 171 | int vrc = VINF_SUCCESS;
|
---|
[74823] | 172 | while ( !RTCircBufUsed(m_pBuffer)
|
---|
[74776] | 173 | && !m_fEos
|
---|
| 174 | && RT_SUCCESS(vrc))
|
---|
| 175 | {
|
---|
| 176 | /* Wait for something to become available. */
|
---|
| 177 | alock.release();
|
---|
| 178 | vrc = RTSemEventWait(m_hSemEvtDataAvail, aTimeoutMS == 0 ? RT_INDEFINITE_WAIT : aTimeoutMS);
|
---|
| 179 | alock.acquire();
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /*
|
---|
| 183 | * Manage the result.
|
---|
| 184 | */
|
---|
[74777] | 185 | HRESULT hrc = S_OK;
|
---|
[74776] | 186 | if ( RT_SUCCESS(vrc)
|
---|
[74823] | 187 | && RTCircBufUsed(m_pBuffer))
|
---|
[74776] | 188 | {
|
---|
[74823] | 189 |
|
---|
| 190 | size_t off = 0;
|
---|
| 191 | size_t cbCopy = RT_MIN(aSize, RTCircBufUsed(m_pBuffer));
|
---|
[74776] | 192 | if (cbCopy != aSize)
|
---|
| 193 | {
|
---|
| 194 | Assert(cbCopy < aSize);
|
---|
| 195 | aData.resize(cbCopy);
|
---|
| 196 | }
|
---|
[74823] | 197 |
|
---|
| 198 | while (cbCopy)
|
---|
| 199 | {
|
---|
| 200 | void *pvSrc = NULL;
|
---|
| 201 | size_t cbThisCopy = 0;
|
---|
| 202 |
|
---|
| 203 | RTCircBufAcquireReadBlock(m_pBuffer, cbCopy, &pvSrc, &cbThisCopy);
|
---|
| 204 | memcpy(&aData.front() + off, pvSrc, cbThisCopy);
|
---|
| 205 | RTCircBufReleaseReadBlock(m_pBuffer, cbThisCopy);
|
---|
| 206 |
|
---|
| 207 | cbCopy -= cbThisCopy;
|
---|
| 208 | off += cbThisCopy;
|
---|
| 209 | }
|
---|
[74776] | 210 | vrc = RTSemEventSignal(m_hSemEvtBufSpcAvail);
|
---|
| 211 | AssertRC(vrc);
|
---|
| 212 | }
|
---|
| 213 | else
|
---|
| 214 | {
|
---|
| 215 | Assert( RT_FAILURE(vrc)
|
---|
| 216 | || ( m_fEos
|
---|
[74823] | 217 | && !RTCircBufUsed(m_pBuffer)));
|
---|
[74776] | 218 |
|
---|
| 219 | aData.resize(0);
|
---|
| 220 | if (vrc == VERR_TIMEOUT)
|
---|
| 221 | hrc = VBOX_E_TIMEOUT;
|
---|
| 222 | else if (RT_FAILURE(vrc))
|
---|
[91718] | 223 | hrc = setErrorBoth(E_FAIL, vrc, tr("Error reading %u bytes: %Rrc", "", aSize), aSize, vrc);
|
---|
[74776] | 224 | }
|
---|
| 225 |
|
---|
| 226 | return hrc;
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 |
|
---|
| 230 | /*********************************************************************************************************************************
|
---|
[74829] | 231 | * DataStream internal methods *
|
---|
[74776] | 232 | *********************************************************************************************************************************/
|
---|
| 233 |
|
---|
| 234 | /**
|
---|
| 235 | * Writes the given data into the temporary buffer blocking if it is full.
|
---|
| 236 | *
|
---|
| 237 | * @returns IPRT status code.
|
---|
| 238 | * @param pvBuf The data to write.
|
---|
| 239 | * @param cbWrite How much to write.
|
---|
| 240 | * @param pcbWritten Where to store the amount of data written.
|
---|
| 241 | */
|
---|
| 242 | int DataStream::i_write(const void *pvBuf, size_t cbWrite, size_t *pcbWritten)
|
---|
| 243 | {
|
---|
| 244 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 245 | AssertReturn(!m_fEos, VERR_INVALID_STATE);
|
---|
| 246 |
|
---|
[74823] | 247 | *pcbWritten = 0;
|
---|
| 248 |
|
---|
[74776] | 249 | int vrc = VINF_SUCCESS;
|
---|
[74823] | 250 | while ( !RTCircBufFree(m_pBuffer)
|
---|
[74776] | 251 | && RT_SUCCESS(vrc))
|
---|
| 252 | {
|
---|
| 253 | /* Wait for space to become available. */
|
---|
| 254 | alock.release();
|
---|
| 255 | vrc = RTSemEventWait(m_hSemEvtBufSpcAvail, RT_INDEFINITE_WAIT);
|
---|
| 256 | alock.acquire();
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | if (RT_SUCCESS(vrc))
|
---|
| 260 | {
|
---|
[74823] | 261 | const uint8_t *pbBuf = (const uint8_t *)pvBuf;
|
---|
| 262 | size_t cbCopy = RT_MIN(cbWrite, RTCircBufFree(m_pBuffer));
|
---|
| 263 |
|
---|
[74776] | 264 | *pcbWritten = cbCopy;
|
---|
[74823] | 265 |
|
---|
| 266 | while (cbCopy)
|
---|
| 267 | {
|
---|
| 268 | void *pvDst = NULL;
|
---|
| 269 | size_t cbThisCopy = 0;
|
---|
| 270 |
|
---|
| 271 | RTCircBufAcquireWriteBlock(m_pBuffer, cbCopy, &pvDst, &cbThisCopy);
|
---|
| 272 | memcpy(pvDst, pbBuf, cbThisCopy);
|
---|
| 273 | RTCircBufReleaseWriteBlock(m_pBuffer, cbThisCopy);
|
---|
| 274 |
|
---|
| 275 | cbCopy -= cbThisCopy;
|
---|
| 276 | pbBuf += cbThisCopy;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[74776] | 279 | RTSemEventSignal(m_hSemEvtDataAvail);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | return vrc;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | /**
|
---|
| 286 | * Marks the end of the stream.
|
---|
| 287 | *
|
---|
| 288 | * @returns IPRT status code.
|
---|
| 289 | */
|
---|
| 290 | int DataStream::i_close()
|
---|
| 291 | {
|
---|
| 292 | AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
|
---|
| 293 | m_fEos = true;
|
---|
| 294 | RTSemEventSignal(m_hSemEvtDataAvail);
|
---|
| 295 | return VINF_SUCCESS;
|
---|
| 296 | }
|
---|
| 297 |
|
---|