Bagikan melalui


Metode IHttpCompletionInfo::GetCompletionBytes

Mengembalikan jumlah byte yang diselesaikan untuk operasi asinkron.

Sintaks

virtual DWORD GetCompletionBytes(  
   VOID  
) const = 0;  

Parameter

Metode ini tidak memerlukan parameter.

Tampilkan Nilai

yang DWORD berisi jumlah byte.

Keterangan

Metode ini GetCompletionBytes memungkinkan Anda untuk mengambil jumlah byte yang selesai selama operasi asinkron. Saat operasi selesai secara asinkron, IIS membuat antarmuka IHttpCompletionInfo dan meneruskan antarmuka tersebut ke metode CHttpModule::OnAsyncCompletion modul Anda untuk memproses hasil operasi asinkron. Anda kemudian dapat menggunakan GetCompletionBytes untuk mengambil byte yang telah selesai untuk operasi asinkron.

Misalnya, jika modul Anda meminta metode IHttpRequest::ReadEntityBody untuk diselesaikan secara asinkron, GetCompletionBytes akan mengembalikan jumlah byte yang dibaca. Demikian pula, jika modul Anda meminta penyelesaian asinkron metode IIHttpResponse::Flush , GetCompletionBytes akan mengembalikan jumlah byte yang disiram ke klien. Selain itu, GetCompletionBytes mengembalikan jumlah byte yang Anda tentukan saat Anda memanggil metode IHttpContext::P ostCompletion .

Contoh

Contoh kode berikut menunjukkan cara membuat modul HTTP yang melakukan tugas berikut:

  1. Modul ini mendaftar untuk pemberitahuan RQ_BEGIN_REQUEST dan RQ_MAP_REQUEST_HANDLER .

  2. Modul membuat kelas CHttpModule yang berisi OnBeginRequest, OnMapRequestHandler, dan OnAsyncCompletion metode.

  3. Saat klien Web meminta URL, IIS memanggil metode modul OnBeginRequest . Metode ini melakukan tugas-tugas berikut:

    1. Menghapus buffer respons yang ada dan mengatur jenis MIME untuk respons.

    2. Membuat contoh string dan mengembalikannya ke klien Web secara asinkron.

    3. Pengujian untuk kesalahan atau penyelesaian asinkron. Jika penyelesaian asinkron tertunda, modul mengembalikan status pemberitahuan yang tertunda ke alur pemrosesan permintaan terintegrasi.

  4. IIS kemudian memanggil metode modul OnMapRequestHandler . Metode ini melakukan tugas-tugas berikut:

    1. Menghapus buffer respons saat ini ke klien Web.

    2. Pengujian untuk kesalahan atau penyelesaian asinkron. Jika penyelesaian asinkron tertunda, modul mengembalikan status pemberitahuan yang tertunda ke alur.

  5. Jika penyelesaian asinkron diperlukan, IIS memanggil metode modul OnAsyncCompletion . Metode ini melakukan tugas-tugas berikut:

    1. Pengujian untuk antarmuka yang valid IHttpCompletionInfo . Jika antarmuka yang valid IHttpCompletionInfo diteruskan, metode memanggil GetCompletionBytes metode dan GetCompletionStatus , masing-masing, untuk mengambil byte yang telah selesai dan mengembalikan status untuk operasi asinkron.

    2. Membuat string yang berisi informasi penyelesaian dan menulis informasi sebagai peristiwa ke log aplikasi Penampil Peristiwa.

  6. Modul menghapus CHttpModule kelas dari memori lalu keluar.

#define _WINSOCKAPI_
#include <windows.h>
#include <sal.h>
#include <httpserv.h>
#include <wchar.h>

// Create the module class.
class MyHttpModule : public CHttpModule
{

public:

    REQUEST_NOTIFICATION_STATUS
    OnBeginRequest(
        IN IHttpContext * pHttpContext,
        IN IHttpEventProvider * pProvider
    )
    {
        UNREFERENCED_PARAMETER( pProvider );

        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        // Buffer to store the byte count.
        DWORD cbSent = 0;
        // Buffer to store if asyncronous completion is pending.
        BOOL fCompletionExpected = false;
        // Create an example string to return to the Web client.
        char szBuffer[] = "Hello World!";
        
        // Clear the existing response.
        pHttpContext->GetResponse()->Clear();
        // Set the MIME type to plain text.
        pHttpContext->GetResponse()->SetHeader(
            HttpHeaderContentType,"text/plain",
            (USHORT)strlen("text/plain"),TRUE);
        
        // Create a data chunk.
        HTTP_DATA_CHUNK dataChunk;
        // Set the chunk to a chunk in memory.
        dataChunk.DataChunkType = HttpDataChunkFromMemory;
        // Set the chunk to the buffer.
        dataChunk.FromMemory.pBuffer =
            (PVOID) szBuffer;
        // Set the chunk size to the buffer size.
        dataChunk.FromMemory.BufferLength =
            (USHORT) strlen(szBuffer);
        // Insert the data chunk into the response.
        hr = pHttpContext->GetResponse()->WriteEntityChunks(
            &dataChunk,1,TRUE,TRUE,&cbSent,&fCompletionExpected);

        // Test for a failure.
        if (FAILED(hr))
        {
            // Set the HTTP status.
            pHttpContext->GetResponse()->SetStatus(
                500,"Server Error",0,hr);
            // End additional processing.
            return RQ_NOTIFICATION_FINISH_REQUEST;
        }
        
        // Test for pending asynchronous operations.
        if (fCompletionExpected)
        {
            return RQ_NOTIFICATION_PENDING;
        }

        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }
    
    REQUEST_NOTIFICATION_STATUS
    OnMapRequestHandler(
        IN IHttpContext * pHttpContext,
        IN IMapHandlerProvider * pProvider
    )
    {
        // Create an HRESULT to receive return values from methods.
        HRESULT hr;
        // Buffer to store the byte count.
        DWORD cbSent = 0;
        // Buffer to store if asyncronous completion is pending.
        BOOL fCompletionExpected = false;

        // Flush the response to the client.
        hr = pHttpContext->GetResponse()->Flush(
            TRUE,FALSE,&cbSent,&fCompletionExpected);

        // Test for a failure.
        if (FAILED(hr))
        {
            // Set the HTTP status.
            pHttpContext->GetResponse()->SetStatus(
                500,"Server Error",0,hr);
        }

        // Test for pending asynchronous operations.
        if (fCompletionExpected)
        {
            return RQ_NOTIFICATION_PENDING;
        }

        // End additional processing.
        return RQ_NOTIFICATION_CONTINUE;
    }

    REQUEST_NOTIFICATION_STATUS
        OnAsyncCompletion(
        IN IHttpContext * pHttpContext,
        IN DWORD dwNotification,
        IN BOOL fPostNotification,
        IN IHttpEventProvider * pProvider,
        IN IHttpCompletionInfo * pCompletionInfo
        )
    {        
        if ( NULL != pCompletionInfo )
        {
            // Create strings for completion information.
            char szNotification[256] = "";
            char szBytes[256] = "";
            char szStatus[256] = "";

            // Retrieve and format the completion information.
            sprintf_s(szNotification,255,"Notification: %u",
                dwNotification);
            sprintf_s(szBytes,255,"Completion Bytes: %u",
                pCompletionInfo->GetCompletionBytes());
            sprintf_s(szStatus,255,"Completion Status: 0x%08x",
                pCompletionInfo->GetCompletionStatus());

            // Create an array of strings.
            LPCSTR szBuffer[3] = {szNotification,szBytes,szStatus};
            // Write the strings to the Event Viewer.
            WriteEventViewerLog(szBuffer,3);
        }
        
        // Return processing to the pipeline.
        return RQ_NOTIFICATION_CONTINUE;
    }

    MyHttpModule(void)
    {
        // Open a handle to the Event Viewer.
        m_hEventLog = RegisterEventSource( NULL,"IISADMIN" );
    }

    ~MyHttpModule(void)
    {
        // Test if the handle for the Event Viewer is open.
        if (NULL != m_hEventLog)
        {
            // Close the handle to the Event Viewer.
            DeregisterEventSource( m_hEventLog );
            m_hEventLog = NULL;
        }
    }

private:

    // Handle for the Event Viewer.
    HANDLE m_hEventLog;

    // Define a method that writes to the Event Viewer.
    BOOL WriteEventViewerLog(LPCSTR * lpStrings, WORD wNumStrings)
    {
        // Test whether the handle for the Event Viewer is open.
        if (NULL != m_hEventLog)
        {
            // Write any strings to the Event Viewer and return.
            return ReportEvent(
                m_hEventLog, EVENTLOG_INFORMATION_TYPE,
                0, 0, NULL, wNumStrings, 0, lpStrings, NULL );
        }
        return FALSE;
    }
};

// Create the module's class factory.
class MyHttpModuleFactory : public IHttpModuleFactory
{
public:
    HRESULT
    GetHttpModule(
        OUT CHttpModule ** ppModule, 
        IN IModuleAllocator * pAllocator
    )
    {
        UNREFERENCED_PARAMETER( pAllocator );

        // Create a new instance.
        MyHttpModule * pModule = new MyHttpModule;

        // Test for an error.
        if (!pModule)
        {
            // Return an error if we cannot create the instance.
            return HRESULT_FROM_WIN32( ERROR_NOT_ENOUGH_MEMORY );
        }
        else
        {
            // Return a pointer to the module.
            *ppModule = pModule;
            pModule = NULL;
            // Return a success status.
            return S_OK;
        }            
    }

    void Terminate()
    {
        // Remove the class from memory.
        delete this;
    }
};

// Create the module's exported registration function.
HRESULT
__stdcall
RegisterModule(
    DWORD dwServerVersion,
    IHttpModuleRegistrationInfo * pModuleInfo,
    IHttpServer * pGlobalInfo
)
{
    UNREFERENCED_PARAMETER( dwServerVersion );
    UNREFERENCED_PARAMETER( pGlobalInfo );

    return pModuleInfo->SetRequestNotifications(
        new MyHttpModuleFactory,
        RQ_BEGIN_REQUEST | RQ_MAP_REQUEST_HANDLER,
        0
    );
}

Modul Anda harus mengekspor fungsi RegisterModule . Anda dapat mengekspor fungsi ini dengan membuat file definisi modul (.def) untuk proyek Anda, atau Anda dapat mengkompilasi modul dengan menggunakan sakelar /EXPORT:RegisterModule . Untuk informasi selengkapnya, lihat Panduan: Membuat Modul HTTP Request-Level Dengan Menggunakan Kode Asli.

Anda dapat secara opsional mengkompilasi kode dengan menggunakan __stdcall (/Gz) konvensi panggilan alih-alih secara eksplisit mendeklarasikan konvensi panggilan untuk setiap fungsi.

Persyaratan

Jenis Deskripsi
Klien - IIS 7.0 di Windows Vista
- IIS 7.5 di Windows 7
- IIS 8.0 di Windows 8
- IIS 10.0 pada Windows 10
Server - IIS 7.0 di Windows Server 2008
- IIS 7.5 di Windows Server 2008 R2
- IIS 8.0 di Windows Server 2012
- IIS 8.5 di Windows Server 2012 R2
- IIS 10.0 di Windows Server 2016
Produk - IIS 7.0, IIS 7.5, IIS 8.0, IIS 8.5, IIS 10.0
- IIS Express 7.5, IIS Express 8.0, IIS Express 10.0
Header Httpserv.h

Lihat juga

Antarmuka IHttpCompletionInfo
Metode IHttpCompletionInfo::GetCompletionStatus