Bug 752461 - Hide click-to-play overlays when choosing "never activate plugins.....
[gecko.git] / netwerk / test / TestOverlappedIO.cpp
blob5ccd7dd46c9b4387b26d6e12e9f003cc48860067
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 #include <stdio.h>
38 #include <signal.h>
40 #ifdef WIN32
41 #include <windows.h>
42 #endif
43 #ifdef OS2
44 #include <os2.h>
45 #endif
47 #include "nspr.h"
48 #include "nscore.h"
49 #include "nsISocketTransportService.h"
50 #include "nsIEventQueueService.h"
51 #include "nsIServiceManager.h"
52 #include "nsITransport.h"
53 #include "nsIRequest.h"
54 #include "nsIStreamProvider.h"
55 #include "nsIStreamListener.h"
56 #include "nsIPipe.h"
57 #include "nsIOutputStream.h"
58 #include "nsIInputStream.h"
59 #include "nsCRT.h"
60 #include "nsCOMPtr.h"
61 #include "nsIByteArrayInputStream.h"
63 #if defined(PR_LOGGING)
64 static PRLogModuleInfo *gTestSocketIOLog;
65 #define LOG(args) PR_LOG(gTestSocketIOLog, PR_LOG_DEBUG, args)
66 #else
67 #define LOG(args)
68 #endif
70 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
71 static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
73 static PRTime gElapsedTime;
74 static int gKeepRunning = 1;
75 static nsIEventQueue* gEventQ = nsnull;
78 //----------------------------------------------------------------------------
79 // Test Listener
80 //----------------------------------------------------------------------------
83 class TestListener : public nsIStreamListener
85 public:
86 TestListener() { }
87 virtual ~TestListener() {}
89 NS_DECL_ISUPPORTS
90 NS_DECL_NSIREQUESTOBSERVER
91 NS_DECL_NSISTREAMLISTENER
94 NS_IMPL_ISUPPORTS2(TestListener,
95 nsIRequestObserver,
96 nsIStreamListener);
98 NS_IMETHODIMP
99 TestListener::OnStartRequest(nsIRequest* request, nsISupports* context)
101 LOG(("TestListener::OnStartRequest\n"));
102 return NS_OK;
105 NS_IMETHODIMP
106 TestListener::OnDataAvailable(nsIRequest* request,
107 nsISupports* context,
108 nsIInputStream *aIStream,
109 PRUint32 aSourceOffset,
110 PRUint32 aLength)
112 LOG(("TestListener::OnDataAvailable [offset=%u length=%u]\n",
113 aSourceOffset, aLength));
114 char buf[1025];
115 PRUint32 amt;
116 while (1) {
117 aIStream->Read(buf, 1024, &amt);
118 if (amt == 0)
119 break;
120 buf[amt] = '\0';
121 //puts(buf);
122 printf("read %d bytes\n", amt);
124 return NS_OK;
127 NS_IMETHODIMP
128 TestListener::OnStopRequest(nsIRequest* request, nsISupports* context,
129 nsresult aStatus)
131 LOG(("TestListener::OnStopRequest [aStatus=%x]\n", aStatus));
132 //gKeepRunning = 0;
133 return NS_OK;
137 //----------------------------------------------------------------------------
138 // Test Provider
139 //----------------------------------------------------------------------------
142 class TestProvider : public nsIStreamProvider
144 public:
145 TestProvider(char *data);
146 virtual ~TestProvider();
148 NS_DECL_ISUPPORTS
149 NS_DECL_NSIREQUESTOBSERVER
150 NS_DECL_NSISTREAMPROVIDER
152 protected:
153 char *mData;
154 PRUint32 mOffset;
155 PRUint32 mDataLen;
156 PRUint32 mRequestCount;
159 NS_IMPL_ISUPPORTS2(TestProvider,
160 nsIStreamProvider,
161 nsIRequestObserver)
163 TestProvider::TestProvider(char *data)
165 mData = data;
166 mDataLen = strlen(data);
167 mOffset = 0;
168 mRequestCount = 0;
169 LOG(("Constructing TestProvider [this=%x]\n", this));
172 TestProvider::~TestProvider()
174 LOG(("Destroying TestProvider [this=%x]\n", this));
177 NS_IMETHODIMP
178 TestProvider::OnStartRequest(nsIRequest* request, nsISupports* context)
180 LOG(("TestProvider::OnStartRequest [this=%x]\n", this));
181 return NS_OK;
184 NS_IMETHODIMP
185 TestProvider::OnStopRequest(nsIRequest* request, nsISupports* context,
186 nsresult aStatus)
188 LOG(("TestProvider::OnStopRequest [status=%x]\n", aStatus));
189 return NS_OK;
192 NS_IMETHODIMP
193 TestProvider::OnDataWritable(nsIRequest *request, nsISupports *context,
194 nsIOutputStream *output, PRUint32 offset, PRUint32 count)
196 LOG(("TestProvider::OnDataWritable [offset=%u, count=%u]\n", offset, count));
198 // Stop at 5 requests
199 if (mRequestCount == 5)
200 return NS_BASE_STREAM_CLOSED;
202 PRUint32 writeCount, amount;
203 amount = NS_MIN(count, mDataLen - mOffset);
204 nsresult rv = output->Write(mData + mOffset, amount, &writeCount);
205 if (NS_SUCCEEDED(rv)) {
206 printf("wrote %u bytes\n", writeCount);
207 mOffset += writeCount;
208 if (mOffset == mDataLen) {
209 printf("done sending packet %u\n", mRequestCount);
210 mOffset = 0;
211 mRequestCount++;
214 return NS_OK;
218 //----------------------------------------------------------------------------
219 // Synchronous IO
220 //----------------------------------------------------------------------------
222 nsresult
223 WriteRequest(nsIOutputStream *os, const char *request)
225 LOG(("WriteRequest [request=%s]\n", request));
226 PRUint32 n;
227 return os->Write(request, strlen(request), &n);
230 nsresult
231 ReadResponse(nsIInputStream *is)
233 PRUint32 bytesRead;
234 char buf[2048];
235 do {
236 is->Read(buf, sizeof(buf), &bytesRead);
237 if (bytesRead > 0)
238 fwrite(buf, 1, bytesRead, stdout);
239 } while (bytesRead > 0);
240 return NS_OK;
244 //----------------------------------------------------------------------------
245 // Startup...
246 //----------------------------------------------------------------------------
249 void
250 sighandler(int sig)
252 LOG(("got signal: %d\n", sig));
253 NS_BREAK();
256 void
257 usage(char **argv)
259 printf("usage: %s <host> <path>\n", argv[0]);
260 exit(1);
264 main(int argc, char* argv[])
266 nsresult rv;
268 signal(SIGSEGV, sighandler);
270 #if defined(PR_LOGGING)
271 gTestSocketIOLog = PR_NewLogModule("TestSocketIO");
272 #endif
274 if (argc < 3)
275 usage(argv);
277 char *hostName = argv[1];
278 char *fileName = argv[2];
279 int port = 80;
281 // Create the Event Queue for this thread...
282 nsCOMPtr<nsIEventQueueService> eventQService =
283 do_GetService(kEventQueueServiceCID, &rv);
284 if (NS_FAILED(rv)) {
285 NS_WARNING("failed to create: event queue service!");
286 return rv;
289 rv = eventQService->CreateMonitoredThreadEventQueue();
290 if (NS_FAILED(rv)) {
291 NS_WARNING("failed to create: thread event queue!");
292 return rv;
295 eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
297 // Create the Socket transport service...
298 nsCOMPtr<nsISocketTransportService> sts =
299 do_GetService(kSocketTransportServiceCID, &rv);
300 if (NS_FAILED(rv)) {
301 NS_WARNING("failed to create: socket transport service!");
302 return rv;
305 char *buffer = PR_smprintf("GET %s HTTP/1.1" CRLF
306 "host: %s" CRLF
307 "user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF
308 "accept: */*" CRLF
309 "accept-language: en" CRLF
310 "accept-encoding: gzip,deflate,compress,identity" CRLF
311 "keep-alive: 300" CRLF
312 "connection: keep-alive" CRLF
313 CRLF,
314 fileName, hostName);
315 LOG(("Request [\n%s]\n", buffer));
317 // Create the socket transport...
318 nsCOMPtr<nsITransport> transport;
319 rv = sts->CreateTransport(hostName, port, nsnull, -1, 0, 0, getter_AddRefs(transport));
320 if (NS_FAILED(rv)) {
321 NS_WARNING("failed to create: socket transport!");
322 return rv;
325 gElapsedTime = PR_Now();
327 nsCOMPtr<nsIRequest> writeRequest, readRequest;
329 rv = transport->AsyncWrite(new TestProvider(buffer), nsnull, 0, 0, 0, getter_AddRefs(writeRequest));
330 if (NS_FAILED(rv)) {
331 NS_WARNING("failed calling: AsyncWrite!");
332 return rv;
334 rv = transport->AsyncRead(new TestListener(), nsnull, 0, 0, 0, getter_AddRefs(readRequest));
335 if (NS_FAILED(rv)) {
336 NS_WARNING("failed calling: AsyncWrite!");
337 return rv;
340 // Enter the message pump
341 while ( gKeepRunning ) {
342 PLEvent *gEvent;
343 gEventQ->WaitForEvent(&gEvent);
344 gEventQ->HandleEvent(gEvent);
347 PRTime endTime;
348 endTime = PR_Now();
349 LOG(("Elapsed time: %d\n", (PRInt32)(endTime/1000UL - gElapsedTime/1000UL)));
351 sts->Shutdown();
352 return 0;