VirtualBox

Changeset 45434 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Apr 9, 2013 1:51:55 PM (12 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
84865
Message:

GuestCtrl: More event handling code for IGuestFile and IGuestSession.

Location:
trunk/src/VBox/Main
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/GuestFileImpl.h

    r45426 r45434  
    9494    static HRESULT  setErrorExternal(VirtualBoxBase *pInterface, int guestRc);
    9595    int             setFileStatus(FileStatus_T fileStatus, int fileRc);
     96    int             waitForEvents(uint32_t uTimeoutMS, ComSafeArrayIn(VBoxEventType_T, pEvents), VBoxEventType_T *pType, IEvent **ppEvent);
    9697    int             waitForOffsetChange(uint32_t uTimeoutMS, uint64_t *puOffset);
    97     int             waitForRead(uint32_t uTimeoutMS, void* pvData, size_t cbData, uint32_t *pcbRead);
     98    int             waitForRead(uint32_t uTimeoutMS, void *pvData, size_t cbData, uint32_t *pcbRead);
    9899    int             waitForStatusChange(uint32_t uTimeoutMS, FileStatus_T *pFileStatus);
    99100    int             waitForWrite(uint32_t uTimeoutMS, uint32_t *pcbWritten);
  • trunk/src/VBox/Main/src-client/GuestFileImpl.cpp

    r45426 r45434  
    7474 * @param   openInfo                File opening information.
    7575 */
    76 int GuestFile::init(Console *pConsole, GuestSession *pSession, ULONG uFileID, const GuestFileOpenInfo &openInfo)
     76int GuestFile::init(Console *pConsole, GuestSession *pSession,
     77                    ULONG uFileID, const GuestFileOpenInfo &openInfo)
    7778{
    7879    LogFlowThisFunc(("pConsole=%p, pSession=%p, uFileID=%RU32, strPath=%s\n",
     
    8687    AssertReturn(autoInitSpan.isOk(), VERR_OBJECT_DESTROYED);
    8788
     89#ifndef VBOX_WITH_GUEST_CONTROL
     90    autoInitSpan.setSucceeded();
     91    return VINF_SUCCESS;
     92#else
    8893    int vrc = bindToSession(pConsole, pSession, uFileID /* Object ID */);
    8994    if (RT_SUCCESS(vrc))
     
    109114    LogFlowFuncLeaveRC(vrc);
    110115    return vrc;
     116#endif /* VBOX_WITH_GUEST_CONTROL */
    111117}
    112118
     
    173179STDMETHODIMP GuestFile::COMGETTER(EventSource)(IEventSource ** aEventSource)
    174180{
     181#ifndef VBOX_WITH_GUEST_CONTROL
     182    ReturnComNotImplemented();
     183#else
    175184    CheckComArgOutPointerValid(aEventSource);
    176185
     
    182191
    183192    return S_OK;
     193#endif /* VBOX_WITH_GUEST_CONTROL */
    184194}
    185195
     
    605615                        void* pvData, uint32_t cbData, uint32_t* pcbRead)
    606616{
     617    AssertPtrReturn(pvData, VERR_INVALID_POINTER);
     618    AssertReturn(cbData, VERR_INVALID_PARAMETER);
     619
    607620    LogFlowThisFunc(("uSize=%RU32, uTimeoutMS=%RU32, pvData=%p, cbData=%zu\n",
    608621                     uSize, uTimeoutMS, pvData, cbData));
     
    739752}
    740753
    741 int GuestFile::waitForOffsetChange(uint32_t uTimeoutMS, uint64_t *puOffset)
    742 {
    743     return VINF_SUCCESS;
    744 }
    745 
    746 int GuestFile::waitForRead(uint32_t uTimeoutMS, void* pvData, size_t cbData, uint32_t *pcbRead)
    747 {
    748     return VINF_SUCCESS;
    749 }
    750 
    751 int GuestFile::waitForStatusChange(uint32_t uTimeoutMS, FileStatus_T *pFileStatus)
    752 {
     754int GuestFile::waitForEvents(uint32_t uTimeoutMS, ComSafeArrayIn(VBoxEventType_T, pEvents),
     755                             VBoxEventType_T *pType, IEvent **ppEvent)
     756{
     757    AssertPtrReturn(pType, VERR_INVALID_POINTER);
     758    AssertPtrReturn(ppEvent, VERR_INVALID_POINTER);
     759
    753760    int vrc;
    754761
    755762    /** @todo Parameter validation. */
     763
     764    com::SafeArray <VBoxEventType_T> arrEventTypes(ComSafeArrayInArg(pEvents));
    756765
    757766    ComPtr<IEventListener> pListener;
     
    759768    if (SUCCEEDED(hr))
    760769    {
    761         com::SafeArray <VBoxEventType_T> eventTypes(1);
    762         eventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
    763         hr = mEventSource->RegisterListener(pListener, ComSafeArrayAsInParam(eventTypes), false);
     770        arrEventTypes.push_back(VBoxEventType_OnGuestFileStateChanged);
     771        hr = mEventSource->RegisterListener(pListener, ComSafeArrayAsInParam(arrEventTypes),
     772                                            TRUE /* Passive listener */);
    764773    }
    765774    else
     
    768777    if (SUCCEEDED(hr))
    769778    {
    770         LogFlowThisFunc(("Waiting for guest file status change event (timeout=%RU32ms) ...\n",
    771                          uTimeoutMS));
     779        LogFlowThisFunc(("Waiting for guest file event(s) (timeout=%RU32ms, %zu events) ...\n",
     780                         uTimeoutMS, arrEventTypes.size()));
    772781
    773782        vrc = VINF_SUCCESS;
     
    788797            }
    789798
    790             ComPtr<IEvent> pEvent;
    791             hr = mEventSource->GetEvent(pListener, cMsWait, pEvent.asOutParam());
     799            ComPtr<IEvent> pThisEvent;
     800            hr = mEventSource->GetEvent(pListener, cMsWait, pThisEvent.asOutParam());
    792801            if (   SUCCEEDED(hr)
    793                 && !pEvent.isNull())
     802                && !pThisEvent.isNull())
    794803            {
    795                 VBoxEventType_T aType;
    796                 hr = pEvent->COMGETTER(Type)(&aType);
     804                VBoxEventType_T type;
     805                hr = pThisEvent->COMGETTER(Type)(&type);
    797806                ComAssertComRC(hr);
    798                 switch (aType)
     807
     808                for (size_t i = 0; i < arrEventTypes.size() && !fSignalled; i++)
    799809                {
    800                     case VBoxEventType_OnGuestFileStateChanged:
     810                    if (type == arrEventTypes[i])
    801811                    {
    802                         ComPtr<IGuestFileStateChangedEvent> pChangedEvent = pEvent;
    803                         Assert(!pChangedEvent.isNull());
    804 
    805                         ComPtr<IGuestFile> pFile;
    806                         pChangedEvent->COMGETTER(File)(pFile.asOutParam());
    807                         Assert(!pFile.isNull());
    808 
    809                         if (pFile != this)
    810                             continue;
     812                        switch (type)
     813                        {
     814                            case VBoxEventType_OnGuestFileStateChanged:
     815                            case VBoxEventType_OnGuestFileOffsetChanged:
     816                            case VBoxEventType_OnGuestFileRead:
     817                            case VBoxEventType_OnGuestFileWrite:
     818                            {
     819                                ComPtr<IGuestFileEvent> pFileEvent = pThisEvent;
     820                                Assert(!pFileEvent.isNull());
     821
     822                                ComPtr<IGuestFile> pFile;
     823                                pFileEvent->COMGETTER(File)(pFile.asOutParam());
     824                                Assert(!pFile.isNull());
     825
     826                                fSignalled = (pFile == this);
     827                                break;
     828                            }
     829
     830                            default:
     831                                AssertMsgFailed(("Unhandled event %ld\n", type));
     832                                break;
     833                        }
     834
     835                        if (fSignalled)
     836                        {
     837                            if (pType)
     838                                *pType = type;
     839                            if (ppEvent)
     840                                pThisEvent.queryInterfaceTo(ppEvent);
     841                            if (   type == VBoxEventType_OnGuestFileStateChanged
     842                                && RT_SUCCESS(vrc))
     843                                vrc = VWRN_GSTCTL_OBJECTSTATE_CHANGED;
     844                            break;
     845                        }
    811846                    }
    812 
    813                     default:
    814                         AssertMsgFailed(("Unhandled event type %ld\n", aType));
    815                         break;
    816847                }
    817 
    818                 fSignalled = true;
    819                 break;
    820848            }
     849            else
     850                vrc = VERR_COM_UNEXPECTED;
    821851
    822852        } while (!fSignalled);
     
    828858        }
    829859
    830         mEventSource->UnregisterListener(pListener);
     860        hr = mEventSource->UnregisterListener(pListener);
     861        ComAssertComRC(hr);
    831862    }
    832863    else
     
    837868}
    838869
     870int GuestFile::waitForOffsetChange(uint32_t uTimeoutMS, uint64_t *puOffset)
     871{
     872    VBoxEventType_T evtType;
     873    ComPtr<IEvent> pEvent;
     874    com::SafeArray<VBoxEventType_T> eventTypes;
     875    eventTypes.push_back(VBoxEventType_OnGuestFileOffsetChanged);
     876    int vrc = waitForEvents(uTimeoutMS, ComSafeArrayAsInParam(eventTypes),
     877                           &evtType, pEvent.asOutParam());
     878    if (   vrc == VINF_SUCCESS /* Can also return VWRN_GSTCTL_OBJECTSTATE_CHANGED. */
     879        && puOffset)
     880    {
     881        Assert(evtType == VBoxEventType_OnGuestFileOffsetChanged);
     882        ComPtr<IGuestFileOffsetChangedEvent> pFileEvent = pEvent;
     883        Assert(!pFileEvent.isNull());
     884
     885        HRESULT hr = pFileEvent->COMGETTER(Offset)((LONG64*)puOffset);
     886        ComAssertComRC(hr);
     887    }
     888
     889    return vrc;
     890}
     891
     892int GuestFile::waitForRead(uint32_t uTimeoutMS, void *pvData, size_t cbData, uint32_t *pcbRead)
     893{
     894    VBoxEventType_T evtType;
     895    ComPtr<IEvent> pEvent;
     896    com::SafeArray<VBoxEventType_T> eventTypes;
     897    eventTypes.push_back(VBoxEventType_OnGuestFileRead);
     898    int vrc = waitForEvents(uTimeoutMS, ComSafeArrayAsInParam(eventTypes),
     899                           &evtType, pEvent.asOutParam());
     900    if (vrc == VINF_SUCCESS) /* Can also return VWRN_GSTCTL_OBJECTSTATE_CHANGED. */
     901    {
     902        Assert(evtType == VBoxEventType_OnGuestFileRead);
     903        ComPtr<IGuestFileReadEvent> pFileEvent = pEvent;
     904        Assert(!pFileEvent.isNull());
     905
     906        HRESULT hr;
     907        if (pvData)
     908        {
     909            com::SafeArray <BYTE> data;
     910            hr = pFileEvent->COMGETTER(Data)(ComSafeArrayAsOutParam(data));
     911            ComAssertComRC(hr);
     912            size_t cbRead = data.size();
     913            if (   cbRead
     914                && cbRead <= cbData)
     915            {
     916                memcpy(pvData, data.raw(), data.size());
     917            }
     918            else
     919                vrc = VERR_BUFFER_OVERFLOW;
     920        }
     921        if (pcbRead)
     922        {
     923            hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbRead);
     924            ComAssertComRC(hr);
     925        }
     926    }
     927
     928    return vrc;
     929}
     930
     931int GuestFile::waitForStatusChange(uint32_t uTimeoutMS, FileStatus_T *pFileStatus)
     932{
     933    VBoxEventType_T evtType;
     934    ComPtr<IEvent> pEvent;
     935    com::SafeArray<VBoxEventType_T> eventTypes;
     936    /* No own event types needed. VBoxEventType_OnGuestFileStateChanged already will
     937     * part of the array when processed in waitForEvents. */
     938    int vrc = waitForEvents(uTimeoutMS, ComSafeArrayAsInParam(eventTypes),
     939                           &evtType, pEvent.asOutParam());
     940    if (   vrc == VINF_SUCCESS /* Can also return VWRN_GSTCTL_OBJECTSTATE_CHANGED. */
     941        && pFileStatus)
     942    {
     943        Assert(evtType == VBoxEventType_OnGuestFileStateChanged);
     944        ComPtr<IGuestFileStateChangedEvent> pFileEvent = pEvent;
     945        Assert(!pFileEvent.isNull());
     946
     947        HRESULT hr = pFileEvent->COMGETTER(Status)(pFileStatus);
     948        ComAssertComRC(hr);
     949    }
     950
     951    return vrc;
     952}
     953
    839954int GuestFile::waitForWrite(uint32_t uTimeoutMS, uint32_t *pcbWritten)
    840955{
    841     return VINF_SUCCESS;
     956    VBoxEventType_T evtType;
     957    ComPtr<IEvent> pEvent;
     958    com::SafeArray<VBoxEventType_T> eventTypes;
     959    eventTypes.push_back(VBoxEventType_OnGuestFileWrite);
     960    int vrc = waitForEvents(uTimeoutMS, ComSafeArrayAsInParam(eventTypes),
     961                           &evtType, pEvent.asOutParam());
     962    if (   vrc == VINF_SUCCESS /* Can also return VWRN_GSTCTL_OBJECTSTATE_CHANGED. */
     963        && pcbWritten)
     964    {
     965        Assert(evtType == VBoxEventType_OnGuestFileWrite);
     966        ComPtr<IGuestFileWriteEvent> pFileEvent = pEvent;
     967        Assert(!pFileEvent.isNull());
     968
     969        HRESULT hr = pFileEvent->COMGETTER(Processed)((ULONG*)pcbWritten);
     970        ComAssertComRC(hr);
     971    }
     972
     973    return vrc;
    842974}
    843975
     
    9041036        paParms[i++].setPointer(pvData, cbData);
    9051037
     1038        uint32_t cbWritten;
    9061039        vrc = sendCommand(HOST_FILE_WRITE_AT, i, paParms);
    9071040        if (RT_SUCCESS(vrc))
    908         {
    909             uint32_t cbWritten;
    910             vrc = sendCommand(HOST_FILE_WRITE, i, paParms);
    911             if (RT_SUCCESS(vrc))
    912                 vrc = waitForWrite(uTimeoutMS, &cbWritten);
    913 
    914             if (RT_SUCCESS(vrc))
    915             {
    916                 LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
    917 
    918                 if (cbWritten)
    919                     *pcbWritten = cbWritten;
    920             }
     1041            vrc = waitForWrite(uTimeoutMS, &cbWritten);
     1042
     1043        if (RT_SUCCESS(vrc))
     1044        {
     1045            LogFlowThisFunc(("cbWritten=%RU32\n", cbWritten));
     1046
     1047            if (cbWritten)
     1048                *pcbWritten = cbWritten;
    9211049        }
    9221050    }
     
    10211149    }
    10221150
    1023     LogFlowThisFunc(("rc=%Rrc, cbRead=%RU64\n", vrc, cbRead));
    1024 
    10251151    LogFlowFuncLeaveRC(vrc);
    10261152    return hr;
     
    10661192    }
    10671193
    1068     LogFlowThisFunc(("rc=%Rrc, cbRead=%RU64\n", vrc, cbRead));
    1069 
    10701194    LogFlowFuncLeaveRC(vrc);
    10711195    return hr;
     
    11551279            default:
    11561280                hr = setError(VBOX_E_IPRT_ERROR,
    1157                               tr("Writing to file \"%s\" failed: %Rrc"),
    1158                               mData.mOpenInfo.mFileName.c_str(), vrc);
     1281                              tr("Writing %zubytes to file \"%s\" failed: %Rrc"),
     1282                              data.size(), mData.mOpenInfo.mFileName.c_str(), vrc);
    11591283                break;
    11601284        }
    11611285    }
    1162 
    1163     LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, aWritten));
    11641286
    11651287    LogFlowFuncLeaveRC(vrc);
     
    11911313            default:
    11921314                hr = setError(VBOX_E_IPRT_ERROR,
    1193                               tr("Writing to file \"%s\" (at offset %RU64) failed: %Rrc"),
    1194                               mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
     1315                              tr("Writing %zubytes to file \"%s\" (at offset %RU64) failed: %Rrc"),
     1316                              data.size(), mData.mOpenInfo.mFileName.c_str(), aOffset, vrc);
    11951317                break;
    11961318        }
    11971319    }
    11981320
    1199     LogFlowThisFunc(("rc=%Rrc, aWritten=%RU32\n", vrc, aWritten));
    1200 
    12011321    LogFlowFuncLeaveRC(vrc);
    12021322    return hr;
  • trunk/src/VBox/Main/src-client/GuestSessionImpl.cpp

    r45426 r45434  
    867867
    868868            Assert(mData.mNumObjects);
    869             LogFlowThisFunc(("Removing file \"%s\" (Session: %RU32) (now total %ld files, %ld objects)\n",
     869            LogFlowThisFunc(("Removing guest file \"%s\" (Session: %RU32) (now total %ld files, %ld objects)\n",
    870870                             Utf8Str(strName).c_str(), mData.mSession.mID, mData.mFiles.size() - 1, mData.mNumObjects - 1));
    871 #ifdef DEBUG
    872             ULONG cRefs = pFile->AddRef();
    873             LogFlowThisFunc(("pObject=%p, cRefs=%RU32\n", pFile, cRefs));
    874             pFile->Release();
    875 #endif
     871
    876872            mData.mFiles.erase(itFiles);
    877873            mData.mNumObjects--;
     874
     875            fireGuestFileRegisteredEvent(mEventSource, this, pFile,
     876                                         false /* Unregistered */);
    878877            return VINF_SUCCESS;
    879878        }
     
    974973        Assert(mData.mNumObjects <= VBOX_GUESTCTRL_MAX_OBJECTS);
    975974
    976         LogFlowFunc(("Added new file \"%s\" (Session: %RU32) (now total %ld files, %ld objects)\n",
     975        LogFlowFunc(("Added new guest file \"%s\" (Session: %RU32) (now total %ld files, %ld objects)\n",
    977976                     openInfo.mFileName.c_str(), mData.mSession.mID, mData.mFiles.size(), mData.mNumObjects));
     977
     978        fireGuestFileRegisteredEvent(mEventSource, this, pFile,
     979                                     true /* Registered */);
    978980    }
    979981
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette