rv = gEventQ->WaitForEvent(&gEvent); rv = gEventQ->HandleEvent(gEvent); #endif
gEventQ->WaitForEvent(&gEvent); gEventQ->HandleEvent(gEvent);
main(int argc, char* argv[]){    nsresult rv;#ifdef XP_OS2_VACPP    signal(SIGSEGV, (_SigFunc)sighandler);#else    signal(SIGSEGV, sighandler);#endif#if defined(PR_LOGGING)    gTestSocketIOLog = PR_NewLogModule("TestSocketIO");#endif    if (argc < 3)        usage(argv);    PRIntn i=0;    PRBool sync = PR_FALSE;    if (nsCRT::strcasecmp(argv[1], "-sync") == 0) {        if (argc < 4)            usage(argv);        sync = PR_TRUE;        i = 1;    }    char *hostName = argv[1+i];    char *fileName = argv[2+i];    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                                CRLF,                                fileName, hostName);    LOG(("Request [\n%s]\n", buffer));    // Create the socket transport...    nsCOMPtr<nsITransport> transport;    rv = sts->CreateTransport(hostName, port, nsnull, 0, 0, getter_AddRefs(transport));    if (NS_FAILED(rv)) {        NS_WARNING("failed to create: socket transport!");        return rv;    }    gElapsedTime = PR_Now();    if (!sync) {        nsCOMPtr<nsIRequest> request;        rv = transport->AsyncWrite(new TestProvider(buffer), nsnull, 0, 0, 0, getter_AddRefs(request));        if (NS_FAILED(rv)) {            NS_WARNING("failed calling: AsyncWrite!");            return rv;        }        // Enter the message pump to allow the URL load to proceed.        while ( gKeepRunning ) {#ifdef WIN32            MSG msg;            if (GetMessage(&msg, NULL, 0, 0)) {                TranslateMessage(&msg);                DispatchMessage(&msg);            }            else                gKeepRunning = FALSE;#elif XP_MAC            /* Mac stuff is missing here! */#elif XP_OS2            QMSG qmsg;            if (WinGetMsg(0, &qmsg, 0, 0, 0))                WinDispatchMsg(0, &qmsg);            else                gKeepRunning = FALSE;#else            PLEvent *gEvent;            rv = gEventQ->WaitForEvent(&gEvent);            rv = gEventQ->HandleEvent(gEvent);#endif        }    }    else {        // synchronous write        {            nsCOMPtr<nsIOutputStream> os;            rv = transport->OpenOutputStream(0, 0, 0, getter_AddRefs(os));            if (NS_FAILED(rv)) {                LOG(("OpenOutputStream failed [rv=%x]\n", rv));                return rv;            }            rv = WriteRequest(os, buffer);            if (NS_FAILED(rv)) {                LOG(("WriteRequest failed [rv=%x]\n", rv));                return rv;            }        }        // synchronous read        {            nsCOMPtr<nsIInputStream> is;            rv = transport->OpenInputStream(0, 0, 0, getter_AddRefs(is));            if (NS_FAILED(rv)) {                LOG(("OpenInputStream failed [rv=%x]\n", rv));                return rv;            }            rv = ReadResponse(is);            if (NS_FAILED(rv)) {                LOG(("ReadResponse failed [rv=%x]\n", rv));                return rv;            }        }    }    PRTime endTime;     endTime = PR_Now();    LOG(("Elapsed time: %d\n", (PRInt32)(endTime/1000UL - gElapsedTime/1000UL)));    sts->Shutdown();    return 0;}