BOOL InitializeServerIo(
    PSERVER_CONTEXT pServerContext
)
{
    ULONG Result;
    HTTP_BINDING_INFO HttpBindingInfo = {0};

    Result = HttpCreateRequestQueue(
                 g_HttpApiVersion,
                 L"Test_Http_Server_HTTPAPI_V2",
                 NULL,
                 0,
                 &(pServerContext->hRequestQueue));

    if (Result != NO_ERROR)
    {
        fprintf(stderr, "HttpCreateRequestQueue failed\n");
        return FALSE;
    }

    HttpBindingInfo.Flags.Present       = 1;
    HttpBindingInfo.RequestQueueHandle  = pServerContext->hRequestQueue;

    Result = HttpSetUrlGroupProperty(
                 pServerContext->urlGroupId,
                 HttpServerBindingProperty,
                 &HttpBindingInfo,
                 sizeof(HttpBindingInfo));

    if (Result != NO_ERROR)
    {
        fprintf(stderr, "HttpSetUrlGroupProperty(...HttpServerBindingProperty...) failed\n");
        return FALSE;
    }

    pServerContext->Io = CreateThreadpoolIo(
                             pServerContext->hRequestQueue,
                             IoCompletionCallback,
                             NULL,
                             NULL);

    if (pServerContext->Io == NULL)
    {
        fprintf(stderr, "Creating a new I/O completion object failed\n");
        return FALSE;
    }

    return TRUE;
}
Exemple #2
0
DWORD 
StartHttpListener(
	PHTTP_LISTENER _listener,
	IN int urlCount, 
	IN _TCHAR* urls[]
)
{
	int UrlAdded = 0;	
	DWORD result;
	
	ZeroMemory(&_listener->UrlGroupId, sizeof(HTTP_URL_GROUP_ID));
	result = HttpCreateUrlGroup(
				_listener->SessionId, 
				&_listener->UrlGroupId, 
				NULL);
	if(result)
	{		
		DEBUG_ASSERT(false);
		return result;
	}

	HTTP_BINDING_INFO httpBinding; 
	ZeroMemory(&httpBinding, sizeof(httpBinding));
	httpBinding.RequestQueueHandle = _listener->hRequestQueue;
	httpBinding.Flags.Present = true;
	result = HttpSetUrlGroupProperty(
						_listener->UrlGroupId, 
						HttpServerBindingProperty, 
						&httpBinding, 
						sizeof(httpBinding));

	// Add the urls to the UrlGroup
	for (int i = 1; i < urlCount; i++)
    {
        wprintf(L"we are listening for requests on the following url: %s\n", urls[i]);

        result = HttpAddUrlToUrlGroup(
									_listener->UrlGroupId,
									urls[i],
									NULL,
									NULL);

        if (result != NO_ERROR)
        {
			_listener->errorCode = result;
			wprintf(L"HttpAddUrl failed for %s with %lu \n\t", urls[i], result);			
            return result;
        }
        else
        {
            UrlAdded ++;
        }
    }	
	_listener->urls = urls;
	_listener->urlsCount = UrlAdded;

	// We will always have 10 oustanding requests.
	for(int j = 0 ;j < HTTP_LISTENER_MAX_PENDING_RECEIVES; j++)
	{
		result = EnsurePump(_listener);	
		if(result != NO_ERROR)
		{
			break;
		}
	}

	_listener->TimerQueue = CreateTimerQueue();
	_listener->LookAsideFlushPeriod = 5 *1000;
	if(_listener->TimerQueue != INVALID_HANDLE_VALUE)
	{
		if(CreateTimerQueueTimer(&_listener->FlushTimer, 
						_listener->TimerQueue, 
						HttpListenerFlushLookasideThreadProc, 
						_listener, 
						_listener->LookAsideFlushPeriod, 
						_listener->LookAsideFlushPeriod, 
						NULL) != FALSE)
		{
			result = NO_ERROR;
		}
		else
		{
			result = GetLastError();
		}
	}
	else
	{
		result = GetLastError();
	}

	if(result == NO_ERROR)
	{
		_listener->State = HTTP_LISTENER_STATE_STARTED;		
	}

	return result;
}