netwerk/test/TestSocketInput.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include <stdio.h>
     7 #ifdef WIN32
     8 #include <windows.h>
     9 #endif
    11 #include "nscore.h"
    12 #include "nsCOMPtr.h"
    13 #include "nsISocketTransportService.h"
    14 #include "nsIEventQueueService.h"
    15 #include "nsIServiceManager.h"
    16 #include "nsIComponentRegistrar.h"
    17 #include "nsITransport.h"
    18 #include "nsIRequest.h"
    19 #include "nsIStreamListener.h"
    20 #include "nsIInputStream.h"
    22 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
    23 static NS_DEFINE_CID(kEventQueueServiceCID,      NS_EVENTQUEUESERVICE_CID);
    25 static int gKeepRunning = 1;
    27 class InputTestConsumer : public nsIStreamListener
    28 {
    29 public:
    31   InputTestConsumer();
    32   virtual ~InputTestConsumer();
    34   // ISupports interface...
    35   NS_DECL_ISUPPORTS
    37   // IStreamListener interface...
    38   NS_DECL_NSIREQUESTOBSERVER
    39   NS_DECL_NSISTREAMLISTENER
    40 };
    43 InputTestConsumer::InputTestConsumer()
    44 {
    45 }
    47 InputTestConsumer::~InputTestConsumer()
    48 {
    49 }
    52 NS_IMPL_ISUPPORTS(InputTestConsumer, nsIRequestObserver, nsIStreamListener)
    55 NS_IMETHODIMP
    56 InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context)
    57 {
    58   printf("+++ OnStartRequest +++\n");
    59   return NS_OK;
    60 }
    63 NS_IMETHODIMP
    64 InputTestConsumer::OnDataAvailable(nsIRequest *request, 
    65                                    nsISupports* context,
    66                                    nsIInputStream *aIStream, 
    67                                    uint64_t aSourceOffset,
    68                                    uint32_t aLength)
    69 {
    70   char buf[1025];
    71   while (aLength > 0) {
    72     uint32_t amt;
    73     aIStream->Read(buf, 1024, &amt);
    74     if (amt == 0) break;
    75     buf[amt] = '\0';
    76     printf(buf);
    77     aLength -= amt;
    78   }
    80   return NS_OK;
    81 }
    84 NS_IMETHODIMP
    85 InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context,
    86                                  nsresult aStatus)
    87 {
    88   gKeepRunning = 0;
    89   printf("+++ OnStopRequest status %x +++\n", aStatus);
    90   return NS_OK;
    91 }
    94 int
    95 main(int argc, char* argv[])
    96 {
    97   nsresult rv;
    99   if (argc < 2) {
   100       printf("usage: %s <host>\n", argv[0]);
   101       return -1;
   102   }
   104   int port;
   105   char* hostName = argv[1];
   106 //nsString portString(argv[2]);
   108 //port = portString.ToInteger(&rv);
   109   port = 13;
   110   {
   111     nsCOMPtr<nsIServiceManager> servMan;
   112     NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
   113     nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
   114     NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
   115     if (registrar)
   116       registrar->AutoRegister(nullptr);
   118     // Create the Event Queue for this thread...
   119     nsCOMPtr<nsIEventQueueService> eventQService =
   120              do_GetService(kEventQueueServiceCID, &rv);
   121     if (NS_FAILED(rv)) return rv;
   123     nsCOMPtr<nsIEventQueue> eventQ;
   124     rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eventQ));
   125     if (NS_FAILED(rv)) return rv;
   127     nsCOMPtr<nsISocketTransportService> sts =
   128              do_GetService(kSocketTransportServiceCID, &rv);
   129     if (NS_FAILED(rv)) return rv;
   131     nsITransport* transport;
   133     rv = sts->CreateTransport(hostName, port, nullptr, 0, 0, &transport);
   134     if (NS_SUCCEEDED(rv)) {
   135       nsCOMPtr<nsIRequest> request;
   136       transport->AsyncRead(new InputTestConsumer, nullptr, 0, -1, 0, getter_AddRefs(request));
   138       NS_RELEASE(transport);
   139     }
   141     // Enter the message pump to allow the URL load to proceed.
   142     while ( gKeepRunning ) {
   143       PLEvent *gEvent;
   144       eventQ->WaitForEvent(&gEvent);
   145       eventQ->HandleEvent(gEvent);
   146     }
   148   } // this scopes the nsCOMPtrs
   149   // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
   150   rv = NS_ShutdownXPCOM(nullptr);
   151   NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
   152   return 0;
   153 }

mercurial