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/. */
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
32 virtual ~InputTestConsumer();
34 // ISupports interface...
37 // IStreamListener interface...
38 NS_DECL_NSIREQUESTOBSERVER
39 NS_DECL_NSISTREAMLISTENER
43 InputTestConsumer::InputTestConsumer()
47 InputTestConsumer::~InputTestConsumer()
52 NS_IMPL_ISUPPORTS(InputTestConsumer
, nsIRequestObserver
, nsIStreamListener
)
56 InputTestConsumer::OnStartRequest(nsIRequest
*request
, nsISupports
* context
)
58 printf("+++ OnStartRequest +++\n");
64 InputTestConsumer::OnDataAvailable(nsIRequest
*request
,
66 nsIInputStream
*aIStream
,
67 uint64_t aSourceOffset
,
73 aIStream
->Read(buf
, 1024, &amt
);
85 InputTestConsumer::OnStopRequest(nsIRequest
*request
, nsISupports
* context
,
89 printf("+++ OnStopRequest status %x +++\n", aStatus
);
95 main(int argc
, char* argv
[])
100 printf("usage: %s <host>\n", argv
[0]);
105 char* hostName
= argv
[1];
106 //nsString portString(argv[2]);
108 //port = portString.ToInteger(&rv);
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");
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
);
141 // Enter the message pump to allow the URL load to proceed.
142 while ( gKeepRunning
) {
144 eventQ
->WaitForEvent(&gEvent
);
145 eventQ
->HandleEvent(gEvent
);
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");