VirtualBox

Ignore:
Timestamp:
Nov 20, 2012 9:11:14 AM (12 years ago)
Author:
vboxsync
Message:

HostServices/HostChannel: fixes and code cleanup.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostServices/HostChannel/HostChannel.cpp

    r43899 r43917  
    229229static void vhcInstanceDestroy(VBOXHOSTCHINSTANCE *pInstance)
    230230{
    231     /* @todo free u32Handle? */
     231    HOSTCHLOG(("HostChannel: destroy %p\n", pInstance));
    232232}
    233233
    234234static int32_t vhcInstanceAddRef(VBOXHOSTCHINSTANCE *pInstance)
    235235{
     236    HOSTCHLOG(("INST: %p %d addref\n", pInstance, pInstance->cRefs));
    236237    return ASMAtomicIncS32(&pInstance->cRefs);
    237238}
     
    240241{
    241242    int32_t c = ASMAtomicDecS32(&pInstance->cRefs);
     243    HOSTCHLOG(("INST: %p %d release\n", pInstance, pInstance->cRefs));
    242244    Assert(c >= 0);
    243245    if (c == 0)
     
    264266            if (RT_SUCCESS(rc))
    265267            {
     268                /* Used by the client, that is in the list of channels. */
    266269                vhcInstanceAddRef(pInstance);
    267 
     270                /* Add to the list of created channel instances. It is inactive while pClient is 0. */
     271                RTListAppend(&pClient->listChannels, &pInstance->nodeClient);
     272
     273                /* Return to the caller. */
     274                vhcInstanceAddRef(pInstance);
    268275                *ppInstance = pInstance;
    269276            }
     
    296303        RTListForEach(&pClient->listChannels, pIter, VBOXHOSTCHINSTANCE, nodeClient)
    297304        {
    298             if (pIter->u32Handle == u32Handle)
     305            if (   pIter->pClient
     306                && pIter->u32Handle == u32Handle)
    299307            {
    300308                pInstance = pIter;
     
    328336        RTListForEach(&pClient->listChannels, pIter, VBOXHOSTCHINSTANCE, nodeClient)
    329337        {
    330             if (pIter->pvChannel == pvChannel)
     338            if (   pIter->pClient
     339                && pIter->pvChannel == pvChannel)
    331340            {
    332341                pInstance = pIter;
     
    346355static void vhcInstanceDetach(VBOXHOSTCHINSTANCE *pInstance)
    347356{
     357    HOSTCHLOG(("HostChannel: detach %p\n", pInstance));
     358
    348359    if (pInstance->pProvider)
    349360    {
     
    351362        RTListNodeRemove(&pInstance->nodeProvider);
    352363        vhcProviderRelease(pInstance->pProvider);
    353         vhcInstanceRelease(pInstance); /* Not in the list anymore. */
    354     }
    355 
    356     RTListNodeRemove(&pInstance->nodeClient);
    357     vhcInstanceRelease(pInstance); /* Not in the list anymore. */
     364        pInstance->pProvider = NULL;
     365        vhcInstanceRelease(pInstance); /* Not in the provider's list anymore. */
     366    }
     367
     368    int rc = vboxHostChannelLock();
     369
     370    if (RT_SUCCESS(rc))
     371    {
     372        RTListNodeRemove(&pInstance->nodeClient);
     373
     374        vboxHostChannelUnlock();
     375
     376        vhcInstanceRelease(pInstance); /* Not used by the client anymore. */
     377    }
    358378}
    359379
     
    497517void vboxHostChannelClientDisconnect(VBOXHOSTCHCLIENT *pClient)
    498518{
    499     /* Clear the list of contexts. */
     519    /* Clear the list of contexts and prevent acceess to the client. */
    500520    int rc = vboxHostChannelLock();
    501521    if (RT_SUCCESS(rc))
    502522    {
    503523        VBOXHOSTCHCALLBACKCTX *pIter;
    504         RTListForEach(&pClient->listContexts, pIter, VBOXHOSTCHCALLBACKCTX, nodeClient)
     524        VBOXHOSTCHCALLBACKCTX *pNext;
     525        RTListForEachSafe(&pClient->listContexts, pIter, pNext, VBOXHOSTCHCALLBACKCTX, nodeClient)
    505526        {
    506527            pIter->pClient = NULL;
     528            RTListNodeRemove(&pIter->nodeClient);
    507529        }
    508530
     
    558580                    pInstance->pvChannel = pvChannel;
    559581
    560                     vhcInstanceAddRef(pInstance); /* Referenced by the list client's channels. */
    561                     RTListAppend(&pClient->listChannels, &pInstance->nodeClient);
     582                    /* It is already in the channels list of the client. */
    562583
    563584                    vhcInstanceAddRef(pInstance); /* Referenced by the list of provider's channels. */
     
    575596            }
    576597
     598            if (RT_FAILURE(rc))
     599            {
     600                vhcInstanceDetach(pInstance);
     601            }
     602
    577603            vhcInstanceRelease(pInstance);
    578604        }
     
    622648    VBOXHOSTCHINSTANCE *pInstance = vhcInstanceFind(pClient, u32Handle);
    623649
    624     if (   pInstance
    625         && pInstance->pProvider)
    626     {
    627         pInstance->pProvider->iface.HostChannelSend(pInstance->pvChannel, pvData, cbData);
     650    if (pInstance)
     651    {
     652        if (pInstance->pProvider)
     653        {
     654            pInstance->pProvider->iface.HostChannelSend(pInstance->pvChannel, pvData, cbData);
     655        }
    628656
    629657        vhcInstanceRelease(pInstance);
     
    650678    VBOXHOSTCHINSTANCE *pInstance = vhcInstanceFind(pClient, u32Handle);
    651679
    652     if (   pInstance
    653         && pInstance->pProvider)
    654     {
    655         rc = pInstance->pProvider->iface.HostChannelRecv(pInstance->pvChannel, pvData, cbData,
    656                                                          pu32SizeReceived, pu32SizeRemaining);
    657 
    658         HOSTCHLOG(("HostChannel: Recv: (%d) handle %d, rc %Rrc, recv %d, rem %d\n",
    659                         pClient->u32ClientID, u32Handle, rc, cbData, *pu32SizeReceived, *pu32SizeRemaining));
     680    if (pInstance)
     681    {
     682        if (pInstance->pProvider)
     683        {
     684            rc = pInstance->pProvider->iface.HostChannelRecv(pInstance->pvChannel, pvData, cbData,
     685                                                             pu32SizeReceived, pu32SizeRemaining);
     686
     687            HOSTCHLOG(("HostChannel: Recv: (%d) handle %d, rc %Rrc, recv %d, rem %d\n",
     688                            pClient->u32ClientID, u32Handle, rc, cbData, *pu32SizeReceived, *pu32SizeRemaining));
     689        }
    660690
    661691        vhcInstanceRelease(pInstance);
     
    684714    VBOXHOSTCHINSTANCE *pInstance = vhcInstanceFind(pClient, u32Handle);
    685715
    686     if (   pInstance
    687         && pInstance->pProvider)
    688     {
    689         pInstance->pProvider->iface.HostChannelControl(pInstance->pvChannel, u32Code,
    690                                                        pvParm, cbParm,
    691                                                        pvData, cbData, pu32SizeDataReturned);
     716    if (pInstance)
     717    {
     718        if (pInstance->pProvider)
     719        {
     720            pInstance->pProvider->iface.HostChannelControl(pInstance->pvChannel, u32Code,
     721                                                           pvParm, cbParm,
     722                                                           pvData, cbData, pu32SizeDataReturned);
     723        }
    692724
    693725        vhcInstanceRelease(pInstance);
     
    785817}
    786818
    787 
    788819/* @thread provider */
    789820static DECLCALLBACK(void) HostChannelCallbackEvent(void *pvCallbacks, void *pvChannel,
     
    844875    if (!pInstance)
    845876    {
    846         vboxHostChannelUnlock();
    847 
    848 #ifdef DEBUG_sunlover
    849         AssertFailed();
    850 #endif
     877        /* Instance was already detached. Skip the event. */
     878        vboxHostChannelUnlock();
     879
    851880        return;
    852881    }
     
    887916            pEvent->cbEvent = cbEvent;
    888917
    889             if (RT_SUCCESS(rc))
    890             {
    891                 RTListAppend(&pClient->listEvents, &pEvent->NodeEvent);
    892             }
    893             else
    894             {
    895                 RTMemFree(pEvent);
    896             }
     918            RTListAppend(&pClient->listEvents, &pEvent->NodeEvent);
    897919        }
    898920    }
    899921
    900922    vboxHostChannelUnlock();
    901 }
    902 
     923
     924    vhcInstanceRelease(pInstance);
     925}
     926
     927/* @thread provider */
    903928static DECLCALLBACK(void) HostChannelCallbackDeleted(void *pvCallbacks, void *pvChannel)
    904929{
Note: See TracChangeset for help on using the changeset viewer.

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