VirtualBox

Ignore:
Timestamp:
Dec 9, 2013 1:49:46 PM (11 years ago)
Author:
vboxsync
Message:

DHCP/NAT and NAT are acting in the same manner: polling for events on main thread and waiting for packets on internal network on RECV thread. DHCP/Host-Only waits for packets on main thread.

Location:
trunk/src/VBox/NetworkServices/NetLib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NetLib/VBoxNetBaseService.cpp

    r49831 r49842  
    2929#include <VBox/com/ErrorInfo.h>
    3030#include <VBox/com/errorprint.h>
    31 #include <VBox/com/EventQueue.h>
    3231#include <VBox/com/VirtualBox.h>
     32#include <VBox/com/NativeEventQueue.h>
    3333
    3434#include <iprt/alloca.h>
     
    4444#include <iprt/string.h>
    4545#include <iprt/time.h>
     46#include <iprt/thread.h>
    4647#include <iprt/mem.h>
    4748#include <iprt/message.h>
     
    8384      m_pIfBuf(NULL),
    8485      m_cVerbosity(0),
    85       m_fNeedMain(false)
     86      m_fNeedMain(false),
     87      m_EventQ(NULL),
     88      m_hThrRecv(NIL_RTTHREAD),
     89      fShutdown(false)
    8690    {
    8791        int rc = RTCritSectInit(&m_csThis);
     
    113117    /* Controls whether service will connect SVC for runtime needs */
    114118    bool                m_fNeedMain;
     119    /* Event Queue */
     120    com::NativeEventQueue  *m_EventQ;
     121
     122    /** receiving thread, used only if main is used */
     123    RTTHREAD m_hThrRecv;
     124
     125    bool fShutdown;
     126    static int recvLoop(RTTHREAD, void *);
    115127};
    116128
     
    133145
    134146
     147int VBoxNetBaseService::Data::recvLoop(RTTHREAD, void *pvUser)
     148{
     149    VBoxNetBaseService *pThis = static_cast<VBoxNetBaseService *>(pvUser);
     150
     151    HRESULT hrc = com::Initialize();
     152    AssertComRCReturn(hrc, VERR_INTERNAL_ERROR);
     153
     154    pThis->doReceiveLoop();
     155
     156    return VINF_SUCCESS;
     157}
     158
     159
    135160VBoxNetBaseService::VBoxNetBaseService(const std::string& aName, const std::string& aNetworkName):m(NULL)
    136161{
     
    149174    if (m != NULL)
    150175    {
     176        shutdown();
    151177        if (m->m_hIf != INTNET_HANDLE_INVALID)
    152178        {
     
    193219{
    194220    return m->m_fNeedMain;
     221}
     222
     223
     224int VBoxNetBaseService::run()
     225{
     226    /**
     227     * If child class need Main we start receving thread which calls doReceiveLoop and enter to event polling loop
     228     * and for the rest clients we do receiving on the current (main) thread.
     229     */
     230    if (isMainNeeded())
     231        return startReceiveThreadAndEnterEventLoop();
     232    else
     233    {
     234        doReceiveLoop();
     235        return VINF_SUCCESS;
     236    }
    195237}
    196238
     
    414456void VBoxNetBaseService::shutdown(void)
    415457{
     458    syncEnter();
     459    m->fShutdown = true;
     460    syncLeave();
    416461}
    417462
     
    678723
    679724
     725int VBoxNetBaseService::startReceiveThreadAndEnterEventLoop()
     726{
     727    AssertMsgReturn(isMainNeeded(), ("It's expected that we need Main"), VERR_INTERNAL_ERROR);
     728       
     729    /* start receiving thread */
     730    int rc = RTThreadCreate(&m->m_hThrRecv, /* thread handle*/
     731                            &VBoxNetBaseService::Data::recvLoop,  /* routine */
     732                            this, /* user data */
     733                            128 * _1K, /* stack size */
     734                            RTTHREADTYPE_IO, /* type */
     735                            0, /* flags, @todo: waitable ?*/
     736                            "RECV");
     737    AssertRCReturn(rc,rc);
     738
     739    m->m_EventQ = com::NativeEventQueue::getMainEventQueue();
     740    AssertPtrReturn(m->m_EventQ, VERR_INTERNAL_ERROR);
     741
     742    while(true)
     743    {
     744        m->m_EventQ->processEventQueue(0);
     745           
     746        if (m->fShutdown)
     747            break;
     748
     749        m->m_EventQ->processEventQueue(500);
     750    }
     751
     752    return VINF_SUCCESS;
     753}
     754
     755
    680756void VBoxNetBaseService::debugPrint(int32_t iMinLevel, bool fMsg, const char *pszFmt, ...) const
    681757{
  • trunk/src/VBox/NetworkServices/NetLib/VBoxNetBaseService.h

    r49832 r49842  
    3030
    3131
     32class VBoxNetLockee
     33{
     34public:
     35    virtual int  syncEnter() = 0;
     36    virtual int  syncLeave() = 0;
     37};
     38
     39
     40class VBoxNetALock
     41{
     42public:
     43    VBoxNetALock(VBoxNetLockee *a_lck):m_lck(a_lck)
     44    {
     45        if (m_lck)
     46            m_lck->syncEnter();
     47    }
     48
     49    ~VBoxNetALock()
     50    {
     51        if (m_lck)
     52            m_lck->syncLeave();
     53    }
     54
     55private:
     56    VBoxNetLockee *m_lck;
     57};
     58
    3259# ifndef BASE_SERVICES_ONLY
    33 class VBoxNetBaseService: public VBoxNetHlpUDPService
     60class VBoxNetBaseService: public VBoxNetHlpUDPService, public VBoxNetLockee
    3461{
    3562public:
     
    4875                                        void const *pvData, size_t cbData) const;
    4976    virtual void        usage(void) = 0;
    50     virtual int         run(void) = 0;
    5177    virtual int         parseOpt(int rc, const RTGETOPTUNION& getOptVal) = 0;
    5278    virtual int         processFrame(void *, size_t) = 0;
     
    5682
    5783    virtual int         init(void);
     84    virtual int         run(void);
    5885    virtual bool        isMainNeeded() const;
    5986
     
    96123    virtual void debugPrintV(int32_t iMinLevel, bool fMsg, const char *pszFmt, va_list va) const;
    97124
     125    private:
    98126    void doReceiveLoop();
    99127
    100 protected:
     128    /** starts receiving thread and enter event polling loop. */
     129    int startReceiveThreadAndEnterEventLoop();
     130
     131    protected:
    101132    /* VirtualBox instance */
    102133    ComPtr<IVirtualBox> virtualbox;
    103134
    104 private:
     135    private:
    105136    struct Data;
    106137    Data *m;
    107138
    108 private:
     139    private:
    109140    PRTGETOPTDEF getOptionsPtr();
    110141};
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