VirtualBox

Changeset 48709 in vbox


Ignore:
Timestamp:
Sep 26, 2013 1:43:05 PM (11 years ago)
Author:
vboxsync
Message:

NatNetwork: reference counting. intoroduces natNetworkRef{Inc,Dec},
if counter is 0 on Inc, corresponding network service is starting.
on Dec exceeding counter 0 network service is stoppping.

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

Legend:

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

    r48297 r48709  
    226226                                 IN_BSTR aHostIp, LONG aHostPort,
    227227                                 IN_BSTR aGuestIp, LONG aGuestPort);
     228   
     229    int natNetworkRefInc(IN_BSTR aNetworkName);
     230    int natNetworkRefDec(IN_BSTR aNetworkName);
    228231
    229232    ComObjPtr<GuestOSType> getUnknownOSType();
     
    360363    static Bstr sPackageType;
    361364    static Bstr sAPIVersion;
     365    static std::map<Bstr, int> sNatNetworkNameToRefCount;
     366    static RWLockHandle* spMtxNatNetworkNameToRefCountLock;
    362367
    363368    static DECLCALLBACK(int) AsyncEventHandler(RTTHREAD thread, void *pvUser);
  • trunk/src/VBox/Main/src-server/VirtualBoxImpl.cpp

    r48297 r48709  
    113113Bstr VirtualBox::sAPIVersion;
    114114
     115// static
     116std::map<Bstr, int> VirtualBox::sNatNetworkNameToRefCount;
     117
     118// static leaked (todo: find better place to free it.)
     119RWLockHandle *VirtualBox::spMtxNatNetworkNameToRefCountLock;
    115120////////////////////////////////////////////////////////////////////////////////
    116121//
     
    366371    if (sAPIVersion.isEmpty())
    367372        sAPIVersion = VBOX_API_VERSION_STRING;
     373    if (!spMtxNatNetworkNameToRefCountLock)
     374        spMtxNatNetworkNameToRefCountLock = new RWLockHandle(LOCKCLASS_VIRTUALBOXOBJECT);
     375
    368376    LogFlowThisFunc(("Version: %ls, Package: %ls, API Version: %ls\n", sVersion.raw(), sPackageType.raw(), sAPIVersion.raw()));
    369377
     
    491499            ComObjPtr<NATNetwork> pNATNetwork;
    492500            if (SUCCEEDED(rc = pNATNetwork.createObject()))
     501            {
    493502                rc = pNATNetwork->init(this, net);
    494             if (FAILED(rc)) throw rc;
     503                AssertComRCReturnRC(rc);
     504            }
    495505
    496506            rc = registerNATNetwork(pNATNetwork, false /* aSaveRegistry */);
    497             if (FAILED(rc)) throw rc;
     507            AssertComRCReturnRC(rc);
    498508        }
    499509
     
    30843094}
    30853095
     3096
     3097int VirtualBox::natNetworkRefInc(IN_BSTR aNetworkName)
     3098{
     3099    AutoWriteLock safeLock(*spMtxNatNetworkNameToRefCountLock COMMA_LOCKVAL_SRC_POS);
     3100    Bstr name(aNetworkName);
     3101
     3102    if (!sNatNetworkNameToRefCount[name])
     3103    {
     3104        ComPtr<INATNetwork> nat;
     3105        HRESULT rc = FindNATNetworkByName(aNetworkName, nat.asOutParam());
     3106        AssertComRCReturn(rc, -1);
     3107
     3108        rc = nat->Start(Bstr("whatever").raw());
     3109        AssertComRCReturn(rc, -1);
     3110    }
     3111
     3112    sNatNetworkNameToRefCount[name]++;
     3113   
     3114    return sNatNetworkNameToRefCount[name];
     3115}
     3116
     3117
     3118int VirtualBox::natNetworkRefDec(IN_BSTR aNetworkName)
     3119{
     3120    AutoWriteLock safeLock(*spMtxNatNetworkNameToRefCountLock COMMA_LOCKVAL_SRC_POS);
     3121    Bstr name(aNetworkName);
     3122
     3123    if (!sNatNetworkNameToRefCount[name])
     3124        return 0;
     3125
     3126    sNatNetworkNameToRefCount[name]--;
     3127
     3128    if (!sNatNetworkNameToRefCount[name])
     3129    {
     3130        ComPtr<INATNetwork> nat;
     3131        HRESULT rc = FindNATNetworkByName(aNetworkName, nat.asOutParam());
     3132        AssertComRCReturn(rc, -1);
     3133
     3134        rc = nat->Stop();
     3135        AssertComRCReturn(rc, -1);
     3136    }
     3137
     3138    return sNatNetworkNameToRefCount[name];
     3139}
     3140
     3141
    30863142/**
    30873143 *  @note Locks this object for reading.
     
    51265182
    51275183    AutoCaller autoCaller(this);
    5128     AssertComRCReturn(autoCaller.rc(), autoCaller.rc());
     5184    AssertComRCReturnRC(autoCaller.rc());
    51295185
    51305186    AutoCaller natNetworkCaller(aNATNetwork);
    5131     AssertComRCReturn(natNetworkCaller.rc(), natNetworkCaller.rc());
     5187    AssertComRCReturnRC(natNetworkCaller.rc());
    51325188
    51335189    Bstr name;
    51345190    HRESULT rc;
    51355191    rc = aNATNetwork->COMGETTER(NetworkName)(name.asOutParam());
    5136     if (FAILED(rc)) return rc;
    5137 
    5138     ComPtr<INATNetwork> existing;
    5139     rc = FindNATNetworkByName(name.raw(), existing.asOutParam());
    5140     if (SUCCEEDED(rc))
    5141         return E_INVALIDARG;
     5192    AssertComRCReturnRC(rc);
     5193
     5194    /* returned value isn't 0 and aSaveSettings is true
     5195     * means that we create duplicate, otherwise we just load settings.
     5196     */
     5197    if (   sNatNetworkNameToRefCount[name]
     5198        && aSaveSettings)
     5199        AssertComRCReturnRC(E_INVALIDARG);
    51425200
    51435201    rc = S_OK;
     5202
     5203    sNatNetworkNameToRefCount[name] = 0;
    51445204
    51455205    m->allNATNetworks.addChild(aNATNetwork);
     
    51875247    AutoCaller natNetworkCaller(aNATNetwork);
    51885248    AssertComRCReturn(natNetworkCaller.rc(), natNetworkCaller.rc());
     5249   
     5250    Bstr name;
     5251    HRESULT rc = aNATNetwork->COMGETTER(NetworkName)(name.asOutParam());
     5252    /* Hm, there're still running clients. */
     5253    if (FAILED(rc) || sNatNetworkNameToRefCount[name])
     5254        AssertComRCReturnRC(E_INVALIDARG);
    51895255
    51905256    m->allNATNetworks.removeChild(aNATNetwork);
    5191 
    5192     HRESULT rc = S_OK;
    51935257
    51945258    if (aSaveSettings)
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