Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestOverlappedIO.cpp File Reference
#include <stdio.h>
#include <signal.h>
#include "nspr.h"
#include "nscore.h"
#include "nsISocketTransportService.h"
#include "nsIEventQueueService.h"
#include "nsIServiceManager.h"
#include "nsITransport.h"
#include "nsIRequest.h"
#include "nsIStreamProvider.h"
#include "nsIStreamListener.h"
#include "nsIPipe.h"
#include "nsIOutputStream.h"
#include "nsIInputStream.h"
#include "nsCRT.h"
#include "nsCOMPtr.h"
#include "nsIByteArrayInputStream.h"

Go to the source code of this file.


class  TestListener
class  TestProvider


#define LOG(args)


 NS_IMPL_ISUPPORTS2 (TestListener, nsIRequestObserver, nsIStreamListener)
 NS_IMPL_ISUPPORTS2 (TestProvider, nsIStreamProvider, nsIRequestObserver) TestProvider
nsresult WriteRequest (nsIOutputStream *os, const char *request)
nsresult ReadResponse (nsIInputStream *is)
void sighandler (int sig)
void usage (char **argv)
int main (int argc, char *argv[])


static PRTime gElapsedTime
static int gKeepRunning = 1
static nsIEventQueuegEventQ = nsnull

Define Documentation

#define LOG (   args)

Definition at line 67 of file TestOverlappedIO.cpp.

Function Documentation

int main ( int  argc,
char *  argv[] 

Definition at line 264 of file TestOverlappedIO.cpp.

    nsresult rv;

    signal(SIGSEGV, sighandler);

#if defined(PR_LOGGING)
    gTestSocketIOLog = PR_NewLogModule("TestSocketIO");

    if (argc < 3)

    char *hostName = argv[1];
    char *fileName = argv[2];
    int port = 80;

    // Create the Event Queue for this thread...
    nsCOMPtr<nsIEventQueueService> eventQService = 
             do_GetService(kEventQueueServiceCID, &rv);
    if (NS_FAILED(rv)) {
        NS_WARNING("failed to create: event queue service!");
        return rv;

    rv = eventQService->CreateMonitoredThreadEventQueue();
    if (NS_FAILED(rv)) {
        NS_WARNING("failed to create: thread event queue!");
        return rv;

    eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);

    // Create the Socket transport service...
    nsCOMPtr<nsISocketTransportService> sts = 
             do_GetService(kSocketTransportServiceCID, &rv);
    if (NS_FAILED(rv)) {
        NS_WARNING("failed to create: socket transport service!");
        return rv;

    char *buffer = PR_smprintf("GET %s HTTP/1.1" CRLF
                               "host: %s" CRLF
                               "user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF
                               "accept: */*" CRLF
                               "accept-language: en" CRLF
                               "accept-encoding: gzip,deflate,compress,identity" CRLF
                               "keep-alive: 300" CRLF
                               "connection: keep-alive" CRLF
                                fileName, hostName);
    LOG(("Request [\n%s]\n", buffer));

    // Create the socket transport...
    nsCOMPtr<nsITransport> transport;
    rv = sts->CreateTransport(hostName, port, nsnull, -1, 0, 0, getter_AddRefs(transport));
    if (NS_FAILED(rv)) {
        NS_WARNING("failed to create: socket transport!");
        return rv;

    gElapsedTime = PR_Now();

    nsCOMPtr<nsIRequest> writeRequest, readRequest;

    rv = transport->AsyncWrite(new TestProvider(buffer), nsnull, 0, 0, 0, getter_AddRefs(writeRequest));
    if (NS_FAILED(rv)) {
        NS_WARNING("failed calling: AsyncWrite!");
        return rv;
    rv = transport->AsyncRead(new TestListener(), nsnull, 0, 0, 0, getter_AddRefs(readRequest));
    if (NS_FAILED(rv)) {
        NS_WARNING("failed calling: AsyncWrite!");
        return rv;

    // Enter the message pump
    while ( gKeepRunning ) {
        PLEvent *gEvent;

    PRTime endTime; 
    endTime = PR_Now();
    LOG(("Elapsed time: %d\n", (PRInt32)(endTime/1000UL - gElapsedTime/1000UL)));

    return 0;

Here is the call graph for this function:

static NS_DEFINE_CID ( kSocketTransportServiceCID  ,
) [static]
NS_IMPL_ISUPPORTS2 ( TestProvider  ,
nsIStreamProvider  ,

Definition at line 159 of file TestOverlappedIO.cpp.

    mData = data;
    mDataLen = strlen(data);
    mOffset = 0;
    mRequestCount = 0;
    LOG(("Constructing TestProvider [this=%x]\n", this));

Definition at line 231 of file TestOverlappedIO.cpp.

    PRUint32 bytesRead;
    char buf[2048];
    do {
        is->Read(buf, sizeof(buf), &bytesRead);
        if (bytesRead > 0)
            fwrite(buf, 1, bytesRead, stdout);
    } while (bytesRead > 0);
    return NS_OK;

Here is the call graph for this function:

Here is the caller graph for this function:

void sighandler ( int  sig)

Definition at line 250 of file TestOverlappedIO.cpp.

    LOG(("got signal: %d\n", sig));

Here is the caller graph for this function:

void usage ( char **  argv)

Definition at line 257 of file TestOverlappedIO.cpp.

    printf("usage: %s <host> <path>\n", argv[0]);

Here is the call graph for this function:

nsresult WriteRequest ( nsIOutputStream os,
const char *  request 

Definition at line 223 of file TestOverlappedIO.cpp.

    LOG(("WriteRequest [request=%s]\n", request));
    PRUint32 n;
    return os->Write(request, strlen(request), &n);

Here is the caller graph for this function:

Variable Documentation

PRTime gElapsedTime [static]

Definition at line 73 of file TestOverlappedIO.cpp.

Definition at line 75 of file TestOverlappedIO.cpp.

int gKeepRunning = 1 [static]

Definition at line 74 of file TestOverlappedIO.cpp.