michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: #include michael@0: michael@0: #ifdef WIN32 michael@0: #include michael@0: #endif michael@0: michael@0: #include "nscore.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsISocketTransportService.h" michael@0: #include "nsIEventQueueService.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: #include "nsITransport.h" michael@0: #include "nsIRequest.h" michael@0: #include "nsIStreamListener.h" michael@0: #include "nsIInputStream.h" michael@0: michael@0: static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); michael@0: static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); michael@0: michael@0: static int gKeepRunning = 1; michael@0: michael@0: class InputTestConsumer : public nsIStreamListener michael@0: { michael@0: public: michael@0: michael@0: InputTestConsumer(); michael@0: virtual ~InputTestConsumer(); michael@0: michael@0: // ISupports interface... michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: // IStreamListener interface... michael@0: NS_DECL_NSIREQUESTOBSERVER michael@0: NS_DECL_NSISTREAMLISTENER michael@0: }; michael@0: michael@0: michael@0: InputTestConsumer::InputTestConsumer() michael@0: { michael@0: } michael@0: michael@0: InputTestConsumer::~InputTestConsumer() michael@0: { michael@0: } michael@0: michael@0: michael@0: NS_IMPL_ISUPPORTS(InputTestConsumer, nsIRequestObserver, nsIStreamListener) michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context) michael@0: { michael@0: printf("+++ OnStartRequest +++\n"); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: InputTestConsumer::OnDataAvailable(nsIRequest *request, michael@0: nsISupports* context, michael@0: nsIInputStream *aIStream, michael@0: uint64_t aSourceOffset, michael@0: uint32_t aLength) michael@0: { michael@0: char buf[1025]; michael@0: while (aLength > 0) { michael@0: uint32_t amt; michael@0: aIStream->Read(buf, 1024, &amt); michael@0: if (amt == 0) break; michael@0: buf[amt] = '\0'; michael@0: printf(buf); michael@0: aLength -= amt; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: NS_IMETHODIMP michael@0: InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context, michael@0: nsresult aStatus) michael@0: { michael@0: gKeepRunning = 0; michael@0: printf("+++ OnStopRequest status %x +++\n", aStatus); michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: int michael@0: main(int argc, char* argv[]) michael@0: { michael@0: nsresult rv; michael@0: michael@0: if (argc < 2) { michael@0: printf("usage: %s \n", argv[0]); michael@0: return -1; michael@0: } michael@0: michael@0: int port; michael@0: char* hostName = argv[1]; michael@0: //nsString portString(argv[2]); michael@0: michael@0: //port = portString.ToInteger(&rv); michael@0: port = 13; michael@0: { michael@0: nsCOMPtr servMan; michael@0: NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: nsCOMPtr registrar = do_QueryInterface(servMan); michael@0: NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); michael@0: if (registrar) michael@0: registrar->AutoRegister(nullptr); michael@0: michael@0: // Create the Event Queue for this thread... michael@0: nsCOMPtr eventQService = michael@0: do_GetService(kEventQueueServiceCID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCOMPtr eventQ; michael@0: rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eventQ)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCOMPtr sts = michael@0: do_GetService(kSocketTransportServiceCID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsITransport* transport; michael@0: michael@0: rv = sts->CreateTransport(hostName, port, nullptr, 0, 0, &transport); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsCOMPtr request; michael@0: transport->AsyncRead(new InputTestConsumer, nullptr, 0, -1, 0, getter_AddRefs(request)); michael@0: michael@0: NS_RELEASE(transport); michael@0: } michael@0: michael@0: // Enter the message pump to allow the URL load to proceed. michael@0: while ( gKeepRunning ) { michael@0: PLEvent *gEvent; michael@0: eventQ->WaitForEvent(&gEvent); michael@0: eventQ->HandleEvent(gEvent); michael@0: } michael@0: michael@0: } // this scopes the nsCOMPtrs michael@0: // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM michael@0: rv = NS_ShutdownXPCOM(nullptr); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); michael@0: return 0; michael@0: } michael@0: