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

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

mercurial