⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scmain.cxx

📁 Windows CE 6.0 Server 源码
💻 CXX
📖 第 1 页 / 共 3 页
字号:

    WCHAR szHostName[MSMQ_SC_SMALLBUFFER];
    dwSize = sizeof(szHostName);

    hr = RegQueryValueEx (hKey, L"HostName", NULL, &dwType, (LPBYTE)szHostName, &dwSize);
    if (hr == ERROR_SUCCESS) {
        if (dwType != REG_SZ) {
            scerror_Complain (MSMQ_SC_ERRMSG_INVALIDKEY, TEXT("HostName"), hr);
            RegCloseKey (hKey);
            delete gMachine;
            gMachine = NULL;
            gMem->Unlock();
            return FALSE;
        }
        gMachine->lpszHostName = svsutil_wcsdup (szHostName);
        
        fIPSelected = TRUE;
    }

    hr = RegQueryValueEx (hKey, L"MessageID", NULL, &dwType, (LPBYTE)&gMachine->uiStartID, &dwSize);

    if (hr != ERROR_SUCCESS)
        gMachine->uiStartID = 1;
    else
        gMachine->uiStartID += SCQMAN_ID_SAVE_FREQ_MASK + 1;

    RegSetValueEx (hKey, L"MessageID", 0, REG_DWORD, (BYTE *)&gMachine->uiStartID, sizeof(DWORD));

    WCHAR szTrackNet[4];
    szTrackNet[0] = L'\0';
    dwSize = sizeof (szTrackNet);

    hr = RegQueryValueEx (hKey, L"CETrackNetwork", NULL, &dwType, (LPBYTE)szTrackNet, &dwSize);
    if ((hr == ERROR_SUCCESS) && ((szTrackNet[0] == L'y') || (szTrackNet[0] == L'Y')))
        gMachine->fNetworkTracking = TRUE;
    else
        gMachine->fNetworkTracking = FALSE;

    WCHAR szYN[4];
    szYN[0] = L'\0';
    dwSize = sizeof (szYN);

    hr = RegQueryValueEx (hKey, L"UntrustedNetwork", NULL, &dwType, (LPBYTE)szYN, &dwSize);
    if ((hr != ERROR_SUCCESS) || ((szYN[0] != L'n') && (szYN[0] != L'N')))
        gMachine->fUntrustedNetwork = TRUE;
    else
        gMachine->fUntrustedNetwork = FALSE;

    szYN[0] = L'\0';
    dwSize = sizeof (szYN);

    hr = RegQueryValueEx (hKey, L"AllowResponse", NULL, &dwType, (LPBYTE)szYN, &dwSize);
    if ((hr == ERROR_SUCCESS) && ((szYN[0] == L'y') || (szYN[0] == L'Y')))
        gMachine->fResponseByIp = TRUE;
    else
        gMachine->fResponseByIp = FALSE;

    szYN[0] = L'\0';
    dwSize = sizeof (szYN);

    hr = RegQueryValueEx (hKey, L"SrmpEnabled", NULL, &dwType, (LPBYTE)szYN, &dwSize);
    if ((hr == ERROR_SUCCESS) && ((szYN[0] == L'y') || (szYN[0] == L'Y')))
        gMachine->fUseSRMP = g_fHaveSRMP;
    else
        gMachine->fUseSRMP = FALSE;

    szYN[0] = L'\0';
    dwSize = sizeof (szYN);

    hr = RegQueryValueEx (hKey, L"BinaryEnabled", NULL, &dwType, (LPBYTE)szYN, &dwSize);
    if ((hr == ERROR_SUCCESS) && ((szYN[0] == L'y') || (szYN[0] == L'Y')))
        gMachine->fUseBinary = TRUE;
    else if (hr != ERROR_SUCCESS)
        gMachine->fUseBinary = (! gMachine->fUntrustedNetwork);
	else
        gMachine->fUseBinary = FALSE;

    dwSize = sizeof(gMachine->asRetrySchedule);
    hr = RegQueryValueEx (hKey, L"RetrySchedule", NULL, &dwType, (LPBYTE)gMachine->asRetrySchedule, &dwSize);
    if ((hr == ERROR_SUCCESS) && (dwSize <= sizeof(gMachine->asRetrySchedule)) && (dwSize > 0) && ((dwSize & 1) == 0) && (dwType == REG_BINARY))
        gMachine->uiRetrySchedule = dwSize / sizeof(short);
    else {
        gMachine->uiRetrySchedule = SCSESSION_ATTEMPT_NUM;

        gMachine->asRetrySchedule[0] = SCSESSION_ATTEMPT_FIRST;
        gMachine->asRetrySchedule[1] = SCSESSION_ATTEMPT_SECOND;
        gMachine->asRetrySchedule[2] = SCSESSION_ATTEMPT_THIRD;
        gMachine->asRetrySchedule[3] = SCSESSION_ATTEMPT_FOURTH;
        gMachine->asRetrySchedule[4] = SCSESSION_ATTEMPT_FIFTHANDUP;
    }

    dwSize = sizeof(gMachine->uiLanOffDelay);
    hr = RegQueryValueEx (hKey, L"LanRetrySchedule", NULL, &dwType, (LPBYTE)&gMachine->uiLanOffDelay, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiLanOffDelay)) && (dwType == REG_DWORD)))
        gMachine->uiLanOffDelay = SCSESSION_LANAUP_DELAY;

    dwSize = sizeof(gMachine->uiConnectionTimeout);
    hr = RegQueryValueEx (hKey, L"ConnectionTimeout", NULL, &dwType, (LPBYTE)&gMachine->uiConnectionTimeout, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiConnectionTimeout)) && (dwType == REG_DWORD)))
        gMachine->uiConnectionTimeout = SCSESSION_INITIAL_TIMEOUT;

    dwSize = sizeof(gMachine->uiIdleTimeout);
    hr = RegQueryValueEx (hKey, L"IdleTimeout", NULL, &dwType, (LPBYTE)&gMachine->uiIdleTimeout, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiIdleTimeout)) && (dwType == REG_DWORD)))
        gMachine->uiIdleTimeout = SCSESSION_IDLE_TIMEOUT;

    dwSize = sizeof(gMachine->uiMinAckTimeout);
    hr = RegQueryValueEx (hKey, L"MinAckTimeout", NULL, &dwType, (LPBYTE)&gMachine->uiMinAckTimeout, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiMinAckTimeout)) && (dwType == REG_DWORD)))
        gMachine->uiMinAckTimeout = MSMQ_MIN_ACKTIMEOUT;

    dwSize = sizeof(gMachine->uiMaxAckTimeout);
    hr = RegQueryValueEx (hKey, L"MaxAckTimeout", NULL, &dwType, (LPBYTE)&gMachine->uiMaxAckTimeout, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiMaxAckTimeout)) && (dwType == REG_DWORD)))
        gMachine->uiMaxAckTimeout = MSMQ_MAX_ACKTIMEOUT;

    dwSize = sizeof(gMachine->uiMinStoreAckTimeout);
    hr = RegQueryValueEx (hKey, L"MinStoreAckTimeout", NULL, &dwType, (LPBYTE)&gMachine->uiMinStoreAckTimeout, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiMinStoreAckTimeout)) && (dwType == REG_DWORD)))
        gMachine->uiMinStoreAckTimeout = MSMQ_MIN_STORE_ACKTIMEOUT;


    if (gMachine->uiMaxAckTimeout <= gMachine->uiMinAckTimeout) {
        gMachine->uiMinAckTimeout = MSMQ_MIN_ACKTIMEOUT;
        gMachine->uiMaxAckTimeout = MSMQ_MAX_ACKTIMEOUT;
    }

    dwSize = sizeof(gMachine->uiSeqTimeout);
    hr = RegQueryValueEx (hKey, L"SequenceTimeout", NULL, &dwType, (LPBYTE)&gMachine->uiSeqTimeout, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiSeqTimeout)) && (dwType == REG_DWORD)))
        gMachine->uiSeqTimeout = SCSEQUENCE_S_SEQTHRESH;

    dwSize = sizeof(gMachine->uiOrderQueueTimeout);
    hr = RegQueryValueEx (hKey, L"OrderQueueTimeout", NULL, &dwType, (LPBYTE)&gMachine->uiOrderQueueTimeout, &dwSize);
    if (! ((hr == ERROR_SUCCESS) && (dwSize == sizeof(gMachine->uiOrderQueueTimeout)) && (dwType == REG_DWORD)))
        gMachine->uiOrderQueueTimeout = SCSEQUENCE_S_SEQTHRESH_Q;

    RegCloseKey (hKey);

    //
    //    Second, set IP parameters
    //
    if (! fIPSelected) {
#if defined (SC_VERBOSE)
        scerror_DebugOut (VERBOSE_MASK_INIT, L"IP parameters are not set in registry. Querying network for host name and IP address...\n");
#endif

        char cszBuffer[MSMQ_SC_SMALLBUFFER];

        if (gethostname (cszBuffer, MSMQ_SC_SMALLBUFFER)) {
            scerror_Complain (MSMQ_SC_ERRMSG_NETWORKERROR);
            delete gMachine;
            gMachine = NULL;
            gMem->Unlock();
            return FALSE;
        }

        if (gMachine->lpszHostName)
            free (gMachine->lpszHostName);

        int iWStrLen = MultiByteToWideChar (CP_ACP, 0, cszBuffer, -1, NULL, 0) + 1;
        SVSUTIL_ASSERT (iWStrLen > 0);
        gMachine->lpszHostName = (WCHAR *)g_funcAlloc (iWStrLen * sizeof(WCHAR), g_pvAllocData);
        MultiByteToWideChar (CP_ACP, 0, cszBuffer, -1, gMachine->lpszHostName, iWStrLen);

        HOSTENT *phe = gethostbyname (cszBuffer);
        if (! phe) {
            scerror_Complain (MSMQ_SC_ERRMSG_NETWORKERROR);
            delete gMachine;
            gMachine = NULL;
            gMem->Unlock();
            return FALSE;
        }

        if ((phe->h_addrtype != AF_INET) || (phe->h_length != sizeof (IN_ADDR)) ||
            (*phe->h_addr_list == NULL)) {
            scerror_Complain (MSMQ_SC_ERRMSG_NONIPNETWORK, phe->h_addrtype);
            delete gMachine;
            gMachine = NULL;
            gMem->Unlock();
            return FALSE;
        }
    }

    if (! scmain_InitializeNetworkTracker ()) {
        scerror_Complain (MSMQ_SC_ERRMSG_NONETWORKTRACK);
        delete gMachine;
        gMachine = NULL;
        gMem->Unlock();
        return FALSE;
    }

    if (gMachine->fUseSRMP)
    	SrmpInit();

    return TRUE;
}

void scmain_GeneralCleanup (BOOL fReInitialize) {
    SVSUTIL_ASSERT (gMem && gMem->IsLocked());

#if defined (SC_VERBOSE)
    scerror_DebugOut (VERBOSE_MASK_INIT, L"Queue manager cleanup...\n");
#endif

    fApiInitialized = FALSE;

    //
    //    The only guarantee that thread termination will not leave the system
    //    in inconsistent state is obtaining all the locks.
    //
    if (gQueueMan)
        gQueueMan->Stop ();

    if (gSessionMan)
        gSessionMan->Stop ();

    //
    //    Then, we need to delete all queues
    //
    if (gQueueMan) {
        gQueueMan->pQueueDLQ      = NULL;
        gQueueMan->pQueueJournal  = NULL;
        gQueueMan->pQueueOrderAck = NULL;
        gQueueMan->pQueueOutFRS   = NULL;

#if defined (SC_VERBOSE)
        scerror_DebugOut (VERBOSE_MASK_INIT, L"Deleting queue memory images...\n");
#endif

        while (gQueueMan->pqlIncoming)
            gQueueMan->DeleteQueue (gQueueMan->pqlIncoming->pQueue, FALSE);

        while (gQueueMan->pqlOutgoing)
            gQueueMan->DeleteQueue (gQueueMan->pqlOutgoing->pQueue, FALSE);

    }
    //
    //    Finally, the only thing we need is to 
    //
    if (gSessionMan)
        delete gSessionMan;

    gSessionMan = NULL;

    if (gQueueMan)
        delete gQueueMan;

    gQueueMan = NULL;

    if (gSeqMan)
        delete gSeqMan;

    gSeqMan = NULL;

    if (gOverlappedSupport)
        delete gOverlappedSupport;

    gOverlappedSupport = NULL;

    if (gMachine)
        delete gMachine;

    gMachine = NULL;

    gMem->Cycle (fReInitialize);
}

int scmain_Init (void) {
    if (fApiInitialized)
        return FALSE;

#if defined (SC_VERBOSE)
    scerror_DebugInitialize ();
#endif

    //
    //    Record network parameters etc.
    //
    if (! scmain_LoadGlobalParameters ())
        return FALSE;


#if defined (SC_VERBOSE)
    scerror_DebugOut (VERBOSE_MASK_INIT, L"Initializing queue manager for local machine...\n");
#endif

    gQueueMan = new ScQueueManager (gMachine->uiStartID);

    if (! gQueueMan) {
        scerror_Complain (MSMQ_SC_ERRMSG_FAILQM);
        scmain_GeneralCleanup (TRUE);
        gMem->Unlock ();
        return FALSE;
    }

    gSessionMan = new ScSessionManager ();

    if ((! gSessionMan) || (! gSessionMan->fInitialized)) {
        scerror_Complain (MSMQ_SC_ERRMSG_FAILSM);
        scmain_GeneralCleanup (TRUE);
        gMem->Unlock ();
        return FALSE;
    }

    gOverlappedSupport = new ScOverlappedSupport;

    if (! gOverlappedSupport) {
        scerror_Complain (MSMQ_SC_ERRMSG_FAILOV);
        scmain_GeneralCleanup (TRUE);
        gMem->Unlock ();
        return FALSE;
    }

    gSeqMan = new ScSequenceCollection;

    if (! gSeqMan) {
        scerror_Complain (MSMQ_SC_ERRMSG_FAILORDER);
        scmain_GeneralCleanup (TRUE);
        gMem->Unlock ();
        return FALSE;
    }

    //
    //    Load whatever is in a directory
    //
    WIN32_FIND_DATA    wfd;
    WCHAR szFullName[_MAX_PATH];
    int iDirLen = wcslen (gMachine->lpszDirName);

    SVSUTIL_ASSERT (iDirLen > 0);
    if (iDirLen > _MAX_PATH - 4) {
        scerror_Complain (MSMQ_SC_ERRMSG_NAMETOOLONG, gMachine->lpszDirName);
        scmain_GeneralCleanup (TRUE);

        gMem->Unlock ();

        return FALSE;
    }

    memcpy (szFullName, gMachine->lpszDirName, sizeof(WCHAR) * iDirLen);

    szFullName[iDirLen] = L'\\';
    ++iDirLen;

    wcscpy (&szFullName[iDirLen], MSMQ_SC_SEARCH_PATTERN);

#if defined (SC_VERBOSE)
    scerror_DebugOut (VERBOSE_MASK_INIT, L"Querying directory %s for queue storage files.\n", szFullName);
#endif

    HANDLE hSearch = FindFirstFile (szFullName, &wfd);

    int fError = FALSE;

    if (hSearch != INVALID_HANDLE_VALUE) {
        do {
            int iFileNameLen = wcslen (wfd.cFileName) + 1;
            if (iDirLen + iFileNameLen >= _MAX_PATH) {
                scerror_Complain (MSMQ_SC_ERRMSG_NAMETOOLONG, wfd.cFileName);
                fError = TRUE;
                break;
            }
            memcpy (&szFullName[iDirLen], wfd.cFileName, sizeof(WCHAR) * (iFileNameLen + 1));

#if defined (SC_VERBOSE)
            scerror_DebugOut (VERBOSE_MASK_INIT, L"Processing %s...\n", szFullName);
#endif

            ScQueue *pQueue = gQueueMan->MakeQueueFromFile (szFullName, NULL);

            if (! pQueue) {
                scerror_Complain (MSMQ_SC_ERRMSG_FILECORRUPT, szFullName);

                fError = TRUE;
                break;
            }

            if (pQueue->qp.bIsDeadLetter) {
                if (gQueueMan->pQueueDLQ  || pQueue->qp.bHasJournal || pQueue->qp.bTransactional || pQueue->qp.bAuthenticate || 
                    pQueue->qp.bIsInternal || (! pQueue->qp.bIsProtected) || (! pQueue->qp.bIsIncoming) ||
                    pQueue->qp.bIsOrderAck || pQueue->qp.bIsOutFRS || pQueue->qp.bIsJournal || pQueue->qp.bIsMachineJournal) {
                    scerror_Complain (MSMQ_SC_ERRMSG_FILECORRUPT, szFullName);

                    fError = TRUE;
                    break;
                }

                gQueueMan->pQueueDLQ = pQueue;

            } else if (pQueue->qp.bIsOrderAck && pQueue->qp.bIsIncoming) {
                if (gQueueMan->pQueueOrderAck  || pQueue->qp.bHasJournal || pQueue->qp.bTransactional || pQueue->qp.bAuthenticate || 
                    (! pQueue->qp.bIsInternal) || (! pQueue->qp.bIsProtected) ||
                    pQueue->qp.bIsDeadLetter || pQueue->qp.bIsOutFRS || pQueue->qp.bIsJournal || pQueue->qp.bIsMachineJournal || 
                    (! pQueue->qp.bIsIncoming)) {
                    scerror_Complain (MSMQ_SC_ERRMSG_FILECORRUPT, szFullName);

                    fError = TRUE;
                    break;
                }

                gQueueMan->pQueueOrderAck = pQueue;

            } else if (pQueue->qp.bIsOutFRS) {
                if (gQueueMan->pQueueOutFRS  || pQueue->qp.bHasJournal || pQueue->qp.bTransactional || pQueue->qp.bAuthenticate || 
                    pQueue->qp.bIsInternal || (! pQueue->qp.bIsProtected) || pQueue->qp.bIsIncoming ||
                    pQueue->qp.bIsOrderAck || pQueue->qp.bIsDeadLetter || pQueue->qp.bIsJournal || pQueue->qp.bIsMachineJournal) {
                    scerror_Complain (MSMQ_SC_ERRMSG_FILECORRUPT, szFullName);

                    fError = TRUE;
                    break;
                }

                gQueueMan->pQueueOutFRS = pQueue;
            } else if (pQueue->qp.bIsMachineJournal) {
                if (gQueueMan->pQueueJournal  || pQueue->qp.bHasJournal || pQueue->qp.bTransactional || pQueue->qp.bAuthenticate || 
                    pQueue->qp.bIsInternal || (! pQueue->qp.bIsProtected) || (! pQueue->qp.bIsIncoming) ||
                    pQueue->qp.bIsOrderAck || pQueue->qp.bIsDeadLetter || pQueue->qp.bIsJournal || pQueue->qp.bIsOutFRS) {
                    scerror_Complain (MSMQ_SC_ERRMSG_FILECORRUPT, szFullName);

                    fError = TRUE;
                    break;
                }

                gQueueMan->pQueueJournal = pQueue;
            }

        } while (FindNextFile (hSearch, &wfd));
        FindClose (hSearch);
    }

    if ((! fError) && (! gQueueMan->pQueueDLQ)) {
        ScQueueParms    qp;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -