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
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.
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 ***** */
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"
57 #include "nsIOutputStream.h"
58 #include "nsIInputStream.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)
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 //----------------------------------------------------------------------------
80 //----------------------------------------------------------------------------
83 class TestListener
: public nsIStreamListener
87 virtual ~TestListener() {}
90 NS_DECL_NSIREQUESTOBSERVER
91 NS_DECL_NSISTREAMLISTENER
94 NS_IMPL_ISUPPORTS2(TestListener
,
99 TestListener::OnStartRequest(nsIRequest
* request
, nsISupports
* context
)
101 LOG(("TestListener::OnStartRequest\n"));
106 TestListener::OnDataAvailable(nsIRequest
* request
,
107 nsISupports
* context
,
108 nsIInputStream
*aIStream
,
109 PRUint32 aSourceOffset
,
112 LOG(("TestListener::OnDataAvailable [offset=%u length=%u]\n",
113 aSourceOffset
, aLength
));
117 aIStream
->Read(buf
, 1024, &amt
);
122 printf("read %d bytes\n", amt
);
128 TestListener::OnStopRequest(nsIRequest
* request
, nsISupports
* context
,
131 LOG(("TestListener::OnStopRequest [aStatus=%x]\n", aStatus
));
137 //----------------------------------------------------------------------------
139 //----------------------------------------------------------------------------
142 class TestProvider
: public nsIStreamProvider
145 TestProvider(char *data
);
146 virtual ~TestProvider();
149 NS_DECL_NSIREQUESTOBSERVER
150 NS_DECL_NSISTREAMPROVIDER
156 PRUint32 mRequestCount
;
159 NS_IMPL_ISUPPORTS2(TestProvider
,
163 TestProvider::TestProvider(char *data
)
166 mDataLen
= strlen(data
);
169 LOG(("Constructing TestProvider [this=%x]\n", this));
172 TestProvider::~TestProvider()
174 LOG(("Destroying TestProvider [this=%x]\n", this));
178 TestProvider::OnStartRequest(nsIRequest
* request
, nsISupports
* context
)
180 LOG(("TestProvider::OnStartRequest [this=%x]\n", this));
185 TestProvider::OnStopRequest(nsIRequest
* request
, nsISupports
* context
,
188 LOG(("TestProvider::OnStopRequest [status=%x]\n", aStatus
));
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
= PR_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
);
218 //----------------------------------------------------------------------------
220 //----------------------------------------------------------------------------
223 WriteRequest(nsIOutputStream
*os
, const char *request
)
225 LOG(("WriteRequest [request=%s]\n", request
));
227 return os
->Write(request
, strlen(request
), &n
);
231 ReadResponse(nsIInputStream
*is
)
236 is
->Read(buf
, sizeof(buf
), &bytesRead
);
238 fwrite(buf
, 1, bytesRead
, stdout
);
239 } while (bytesRead
> 0);
244 //----------------------------------------------------------------------------
246 //----------------------------------------------------------------------------
252 LOG(("got signal: %d\n", sig
));
259 printf("usage: %s <host> <path>\n", argv
[0]);
264 main(int argc
, char* argv
[])
268 signal(SIGSEGV
, sighandler
);
270 #if defined(PR_LOGGING)
271 gTestSocketIOLog
= PR_NewLogModule("TestSocketIO");
277 char *hostName
= argv
[1];
278 char *fileName
= argv
[2];
281 // Create the Event Queue for this thread...
282 nsCOMPtr
<nsIEventQueueService
> eventQService
=
283 do_GetService(kEventQueueServiceCID
, &rv
);
285 NS_WARNING("failed to create: event queue service!");
289 rv
= eventQService
->CreateMonitoredThreadEventQueue();
291 NS_WARNING("failed to create: thread event queue!");
295 eventQService
->GetThreadEventQueue(NS_CURRENT_THREAD
, &gEventQ
);
297 // Create the Socket transport service...
298 nsCOMPtr
<nsISocketTransportService
> sts
=
299 do_GetService(kSocketTransportServiceCID
, &rv
);
301 NS_WARNING("failed to create: socket transport service!");
305 char *buffer
= PR_smprintf("GET %s HTTP/1.1" CRLF
307 "user-agent: Mozilla/5.0 (X11; N; Linux 2.2.16-22smp i686; en-US; m18) Gecko/20001220" CRLF
309 "accept-language: en" CRLF
310 "accept-encoding: gzip,deflate,compress,identity" CRLF
311 "keep-alive: 300" CRLF
312 "connection: keep-alive" CRLF
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
));
321 NS_WARNING("failed to create: socket transport!");
325 gElapsedTime
= PR_Now();
327 nsCOMPtr
<nsIRequest
> writeRequest
, readRequest
;
329 rv
= transport
->AsyncWrite(new TestProvider(buffer
), nsnull
, 0, 0, 0, getter_AddRefs(writeRequest
));
331 NS_WARNING("failed calling: AsyncWrite!");
334 rv
= transport
->AsyncRead(new TestListener(), nsnull
, 0, 0, 0, getter_AddRefs(readRequest
));
336 NS_WARNING("failed calling: AsyncWrite!");
340 // Enter the message pump
341 while ( gKeepRunning
) {
343 gEventQ
->WaitForEvent(&gEvent
);
344 gEventQ
->HandleEvent(gEvent
);
349 LOG(("Elapsed time: %d\n", (PRInt32
)(endTime
/1000UL - gElapsedTime
/1000UL)));