Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Functions | Variables
TestSocketIO.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 TestSocketIO.cpp.

Function Documentation

int main ( int  argc,
char *  argv[] 

Definition at line 259 of file TestSocketIO.cpp.

    nsresult rv;

    signal(SIGSEGV, sighandler);

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

    if (argc < 3)

    PRIntn i=0;
    PRBool sync = PR_FALSE;
    if (nsCRT::strcasecmp(argv[1], "-sync") == 0) {
        if (argc < 4)
        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
                                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 ) {
            PLEvent *gEvent;
    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)));

    return 0;

Here is the call graph for this function:

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

Definition at line 155 of file TestSocketIO.cpp.

    NS_NewByteArrayInputStream(getter_AddRefs(mData), data, strlen(data));
    LOG(("Constructing TestProvider [this=%x]\n", this));

Here is the call graph for this function:

Definition at line 226 of file TestSocketIO.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:

void sighandler ( int  sig)

Definition at line 245 of file TestSocketIO.cpp.

    LOG(("got signal: %d\n", sig));
void usage ( char **  argv)

Definition at line 252 of file TestSocketIO.cpp.

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

Here is the call graph for this function:

nsresult WriteRequest ( nsIOutputStream os,
const char *  request 

Definition at line 218 of file TestSocketIO.cpp.

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

Variable Documentation

PRTime gElapsedTime [static]

Definition at line 73 of file TestSocketIO.cpp.

Definition at line 75 of file TestSocketIO.cpp.

int gKeepRunning = 1 [static]

Definition at line 74 of file TestSocketIO.cpp.