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

📄 tdaemondlg.cpp

📁 使用环境VC++ 6.0
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        {
            pCallParams->dwBearerMode = LINEBEARERMODE_VOICE;
            pCallParams->dwMediaMode = LINEMEDIAMODE_DATAMODEM;
        
            if( TPENDING(m_call.MakeCall(sDialable,
                                         pno.GetCountryCodeNum(),
                                         this, pCallParams)) )
            {
                const UINT  nTimeOut = 30000;   // 30 seconds
                m_nTimer = SetTimer(1, nTimeOut, 0);
                LogStatus("Placing a call to '%s'...\r\n", (LPCSTR)sDisplayable);
            }

            delete[] pCallParams;
        }
    }
}

void CtDaemonDlg::LogStatus(LPCSTR pszFormat, ...)
{
    char    szOutput[256];
    va_list argList;

    va_start(argList, pszFormat);
    ::wvsprintf(szOutput, pszFormat, argList);
    va_end(argList);

    m_editLog.SetSel(0xffffffff);
    m_editLog.ReplaceSel(szOutput);
}

// Telephony Events

void CtDaemonDlg::OnCallState(
    CtCall* pCall,
    DWORD   nCallState,
    DWORD   dwParam2,
    DWORD   nCallPriviledge)
{
    struct FlagMap { DWORD nFlag; LPCSTR pszFlag; };
    static FlagMap rgFlags[] =
    {
        {LINECALLSTATE_IDLE,        "idle"},
        {LINECALLSTATE_ACCEPTED,    "accepted"},
        {LINECALLSTATE_DIALTONE,    "dial tone detected"},
        {LINECALLSTATE_DIALING,     "dialing"},
        {LINECALLSTATE_RINGBACK,    "ring-back detected"},
        {LINECALLSTATE_BUSY,        "busy detected"},
        {LINECALLSTATE_SPECIALINFO, "error detected"},
        {LINECALLSTATE_CONNECTED,   "connected"},
        {LINECALLSTATE_PROCEEDING,  "proceeding"},
        {LINECALLSTATE_DISCONNECTED,"disconnected"},
    };

    for( int i = 0; i < DIM(rgFlags); i++ )
    {
        if( rgFlags[i].nFlag == nCallState )
        {
            LogStatus("Call %s.\r\n", rgFlags[i].pszFlag);
            break;
        }
    }

    switch( nCallState )
    {
    case LINECALLSTATE_CONNECTED:
        OnConnected();
    break;

    case LINECALLSTATE_DISCONNECTED:
        m_call.Drop();
    break;

    case LINECALLSTATE_IDLE:
        m_call.Deallocate();
        DialNext();
    break;
    }
}

void CtDaemonDlg::ReadData(HANDLE hComm)
{
    // Temporarily boost thread priority
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);

    // Cause read operation to return immediately with
    // the characters that have already been received,
    // even if no characters have been received"
    COMMTIMEOUTS    cto = { 0 };
    cto.ReadIntervalTimeout = MAXDWORD;
    SetCommTimeouts(hComm, &cto);

    const DWORD nTicks = 4000;  // 4 seconds
    const DWORD nTickStart = GetTickCount();
    OVERLAPPED  ol = { 0 };

    while( GetTickCount() - nTickStart < nTicks )
    {
        char    sz[1024];
        DWORD   nBytes;
        if( ReadFile(hComm, sz, sizeof(sz)-1, &nBytes, &ol) && nBytes )
        {
            sz[nBytes] = 0;
            LogStatus(sz);
        }
    }

    // Clear pending bytes and reset thread priority
    PurgeComm(hComm, PURGE_RXCLEAR);
    SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
    LogStatus("\r\n");
}

void CtDaemonDlg::OnConnected()
{
    LogStatus("Got one! Output follows:\r\n");
    
    // Kill the timer
    KillTimer(m_nTimer); m_nTimer = 0;
    
    // Read data
    CtDeviceID  did;
    if( TSUCCEEDED(did.GetIDFromCall("comm/datamodem", m_call.GetHandle())) )
    {
        HANDLE  hComm;
        did.GetHandleAndString(&hComm);

        if( hComm )
        {
            ReadData(hComm);
            CloseHandle(hComm);
        }
    }

    // Drop the call (and dial the next one)
    m_call.Drop();
}


void CtDaemonDlg::OnCallReply(
    CtCall*     pCall,
    TREQUEST    nRequestID,
    TRESULT     nResult,
    DWORD       nRequestType)
{
    switch( nRequestType )
    {
    case CALLREQUEST_MAKECALL:
        if( TSUCCEEDED(nResult) )
        {
            LogStatus("Call placed.\r\n");
        }
        else
        {
            LogStatus("Call cannot be placed.\r\n");
            OnStop();
        }
    break;

    case CALLREQUEST_DROP:
        if( TSUCCEEDED(nResult) )
        {
            LogStatus("Call dropped.\r\n");
        }
        else
        {
            LogStatus("Call cannot be dropped.\r\n");
        }
    break;
    }
}

void CtDaemonDlg::OnLineClose(CtLine* pLine)
{
    LogStatus("Line %d closed.\r\n", pLine->GetDeviceID());
    OnStop();
}

void CtDaemonDlg::OnTimer(UINT nIDEvent) 
{
    // Drop the call
    if( m_call.GetHandle() )
    {
        LogStatus("Connection timed out.\r\n");
        m_call.Drop();
    }
}

// TODO: Remove
void ReadData2(HANDLE hComm)
{
#if 0
            // Temporarily boost thread priority
            SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);

            const DWORD nTicks = 4000;  // 4 seconds
            const DWORD nTickStart = GetTickCount();
            OVERLAPPED  ol = { 0, 0, 0, 0, CreateEvent(0, TRUE, FALSE, 0) };
            //OVERLAPPED  ol = { 0 };
            int n = 0; // TODO: Remove

            char    sz[1024];
            DWORD   nBytes;
            if( ReadFile(hComm, sz, sizeof(sz)-1, &nBytes, &ol) )
            {
                sz[nBytes] = 0;
                LogStatus(sz);
            }
            else if( GetLastError() == ERROR_IO_PENDING )
            {
                if( WaitForSingleObject(ol.hEvent, nTicks) == WAIT_OBJECT_0 &&
                    GetOverlappedResult(hComm, &ol, &nBytes, FALSE) )
                {
                    sz[nBytes] = 0;
                    LogStatus(sz);
                }
            }
#endif
#if 0
            while( GetTickCount() - nTickStart < nTicks )
            {
                TRACE("loops: %d\n", ++n);
                /*
                // Read one byte
                char    ch;
                DWORD   nBytes = 0;
                if( (!ReadFile(hComm, &ch, 1, &nBytes, &ol) &&
                     (GetLastError() != ERROR_IO_PENDING)) ||
                     (!GetOverlappedResult(hComm, &ol, &nBytes, FALSE) &&
                      (GetLastError() != ERROR_IO_INCOMPLETE)) )
                {
                    break;
                }

                if( nBytes )
                {
                    LogStatus("%c", ch);
                }
                */
                /*
                char    ch;
                DWORD   nBytes;

                // Read waiting bytes (synchronously)
                while( ReadFile(hComm, &ch, 1, &nBytes, &ol) )
                {
                    //LogStatus("sync: '%c'\n", ch);
                    TRACE("sync: '%c'\n", ch);
                }

                // Handling asynch. I/O
                if( GetLastError() == ERROR_IO_PENDING )
                {
                    // Wait for I/O to complete
                    DWORD nTimeToWait = GetTickCount() - nTickStart - nTicks;
                    //if( WaitForSingleObject(hComm, nTimeToWait) == WAIT_OBJECT_0 )
                    if( WaitForSingleObject(ol.hEvent, nTimeToWait) == WAIT_OBJECT_0 )
                    {
                        //LogStatus("async: '%c'\n", ch);
                        TRACE("async: '%c'\n", ch);
                    }
                }
                // Stop reading in the event of an error
                else
                {
                    break;
                }
                */

            }

            SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL);
            LogStatus("\r\n");
            CloseHandle(ol.hEvent);
            CloseHandle(hComm);
#endif
}

⌨️ 快捷键说明

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