1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestSocketInput.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,154 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +#include <stdio.h> 1.9 + 1.10 +#ifdef WIN32 1.11 +#include <windows.h> 1.12 +#endif 1.13 + 1.14 +#include "nscore.h" 1.15 +#include "nsCOMPtr.h" 1.16 +#include "nsISocketTransportService.h" 1.17 +#include "nsIEventQueueService.h" 1.18 +#include "nsIServiceManager.h" 1.19 +#include "nsIComponentRegistrar.h" 1.20 +#include "nsITransport.h" 1.21 +#include "nsIRequest.h" 1.22 +#include "nsIStreamListener.h" 1.23 +#include "nsIInputStream.h" 1.24 + 1.25 +static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); 1.26 +static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); 1.27 + 1.28 +static int gKeepRunning = 1; 1.29 + 1.30 +class InputTestConsumer : public nsIStreamListener 1.31 +{ 1.32 +public: 1.33 + 1.34 + InputTestConsumer(); 1.35 + virtual ~InputTestConsumer(); 1.36 + 1.37 + // ISupports interface... 1.38 + NS_DECL_ISUPPORTS 1.39 + 1.40 + // IStreamListener interface... 1.41 + NS_DECL_NSIREQUESTOBSERVER 1.42 + NS_DECL_NSISTREAMLISTENER 1.43 +}; 1.44 + 1.45 + 1.46 +InputTestConsumer::InputTestConsumer() 1.47 +{ 1.48 +} 1.49 + 1.50 +InputTestConsumer::~InputTestConsumer() 1.51 +{ 1.52 +} 1.53 + 1.54 + 1.55 +NS_IMPL_ISUPPORTS(InputTestConsumer, nsIRequestObserver, nsIStreamListener) 1.56 + 1.57 + 1.58 +NS_IMETHODIMP 1.59 +InputTestConsumer::OnStartRequest(nsIRequest *request, nsISupports* context) 1.60 +{ 1.61 + printf("+++ OnStartRequest +++\n"); 1.62 + return NS_OK; 1.63 +} 1.64 + 1.65 + 1.66 +NS_IMETHODIMP 1.67 +InputTestConsumer::OnDataAvailable(nsIRequest *request, 1.68 + nsISupports* context, 1.69 + nsIInputStream *aIStream, 1.70 + uint64_t aSourceOffset, 1.71 + uint32_t aLength) 1.72 +{ 1.73 + char buf[1025]; 1.74 + while (aLength > 0) { 1.75 + uint32_t amt; 1.76 + aIStream->Read(buf, 1024, &amt); 1.77 + if (amt == 0) break; 1.78 + buf[amt] = '\0'; 1.79 + printf(buf); 1.80 + aLength -= amt; 1.81 + } 1.82 + 1.83 + return NS_OK; 1.84 +} 1.85 + 1.86 + 1.87 +NS_IMETHODIMP 1.88 +InputTestConsumer::OnStopRequest(nsIRequest *request, nsISupports* context, 1.89 + nsresult aStatus) 1.90 +{ 1.91 + gKeepRunning = 0; 1.92 + printf("+++ OnStopRequest status %x +++\n", aStatus); 1.93 + return NS_OK; 1.94 +} 1.95 + 1.96 + 1.97 +int 1.98 +main(int argc, char* argv[]) 1.99 +{ 1.100 + nsresult rv; 1.101 + 1.102 + if (argc < 2) { 1.103 + printf("usage: %s <host>\n", argv[0]); 1.104 + return -1; 1.105 + } 1.106 + 1.107 + int port; 1.108 + char* hostName = argv[1]; 1.109 +//nsString portString(argv[2]); 1.110 + 1.111 +//port = portString.ToInteger(&rv); 1.112 + port = 13; 1.113 + { 1.114 + nsCOMPtr<nsIServiceManager> servMan; 1.115 + NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); 1.116 + nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan); 1.117 + NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); 1.118 + if (registrar) 1.119 + registrar->AutoRegister(nullptr); 1.120 + 1.121 + // Create the Event Queue for this thread... 1.122 + nsCOMPtr<nsIEventQueueService> eventQService = 1.123 + do_GetService(kEventQueueServiceCID, &rv); 1.124 + if (NS_FAILED(rv)) return rv; 1.125 + 1.126 + nsCOMPtr<nsIEventQueue> eventQ; 1.127 + rv = eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(eventQ)); 1.128 + if (NS_FAILED(rv)) return rv; 1.129 + 1.130 + nsCOMPtr<nsISocketTransportService> sts = 1.131 + do_GetService(kSocketTransportServiceCID, &rv); 1.132 + if (NS_FAILED(rv)) return rv; 1.133 + 1.134 + nsITransport* transport; 1.135 + 1.136 + rv = sts->CreateTransport(hostName, port, nullptr, 0, 0, &transport); 1.137 + if (NS_SUCCEEDED(rv)) { 1.138 + nsCOMPtr<nsIRequest> request; 1.139 + transport->AsyncRead(new InputTestConsumer, nullptr, 0, -1, 0, getter_AddRefs(request)); 1.140 + 1.141 + NS_RELEASE(transport); 1.142 + } 1.143 + 1.144 + // Enter the message pump to allow the URL load to proceed. 1.145 + while ( gKeepRunning ) { 1.146 + PLEvent *gEvent; 1.147 + eventQ->WaitForEvent(&gEvent); 1.148 + eventQ->HandleEvent(gEvent); 1.149 + } 1.150 + 1.151 + } // this scopes the nsCOMPtrs 1.152 + // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM 1.153 + rv = NS_ShutdownXPCOM(nullptr); 1.154 + NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); 1.155 + return 0; 1.156 +} 1.157 +