1999-06-08 01:33:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
1999-04-19 11:17:37 +04:00
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
#include "stdio.h"
|
1999-05-04 04:31:40 +04:00
|
|
|
|
1999-05-04 22:50:59 +04:00
|
|
|
#ifdef WIN32
|
1999-04-19 11:17:37 +04:00
|
|
|
#include <windows.h>
|
1999-05-04 04:31:40 +04:00
|
|
|
#endif
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
#include "nscore.h"
|
1999-06-29 05:52:30 +04:00
|
|
|
#include "nsCOMPtr.h"
|
1999-04-19 11:17:37 +04:00
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIChannel.h"
|
1999-04-19 11:17:37 +04:00
|
|
|
#include "nsIStreamListener.h"
|
1999-06-09 00:57:32 +04:00
|
|
|
#include "nsIBufferInputStream.h"
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
|
|
|
|
static int gKeepRunning = 1;
|
|
|
|
|
|
|
|
class InputTestConsumer : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
InputTestConsumer();
|
|
|
|
virtual ~InputTestConsumer();
|
|
|
|
|
|
|
|
// ISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// IStreamListener interface...
|
1999-07-07 12:08:40 +04:00
|
|
|
NS_IMETHOD OnStartRequest(nsIChannel* channel, nsISupports* context);
|
1999-04-19 11:17:37 +04:00
|
|
|
|
1999-07-07 12:08:40 +04:00
|
|
|
NS_IMETHOD OnDataAvailable(nsIChannel* channel, nsISupports* context,
|
1999-06-29 00:37:10 +04:00
|
|
|
nsIInputStream *aIStream,
|
1999-04-22 12:47:06 +04:00
|
|
|
PRUint32 aSourceOffset,
|
1999-04-19 11:17:37 +04:00
|
|
|
PRUint32 aLength);
|
|
|
|
|
1999-07-07 12:08:40 +04:00
|
|
|
NS_IMETHOD OnStopRequest(nsIChannel* channel, nsISupports* context,
|
1999-06-08 01:33:30 +04:00
|
|
|
nsresult aStatus,
|
1999-07-01 23:30:20 +04:00
|
|
|
const PRUnichar* aMsg);
|
1999-06-08 01:33:30 +04:00
|
|
|
|
1999-04-19 11:17:37 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
InputTestConsumer::InputTestConsumer()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
InputTestConsumer::~InputTestConsumer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID);
|
|
|
|
NS_IMPL_ISUPPORTS(InputTestConsumer,kIStreamListenerIID);
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
InputTestConsumer::OnStartRequest(nsIChannel* channel, nsISupports* context)
|
1999-04-19 11:17:37 +04:00
|
|
|
{
|
1999-07-01 23:30:20 +04:00
|
|
|
printf("+++ OnStartRequest +++\n");
|
1999-04-19 11:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
InputTestConsumer::OnDataAvailable(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
1999-06-29 00:37:10 +04:00
|
|
|
nsIInputStream *aIStream,
|
1999-04-22 12:47:06 +04:00
|
|
|
PRUint32 aSourceOffset,
|
1999-04-19 11:17:37 +04:00
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
char buf[1025];
|
|
|
|
while (aLength > 0) {
|
|
|
|
PRUint32 amt;
|
1999-07-13 06:37:08 +04:00
|
|
|
aIStream->Read(buf, 1024, &amt);
|
1999-09-10 02:05:05 +04:00
|
|
|
if (amt == 0) break;
|
1999-04-19 11:17:37 +04:00
|
|
|
buf[amt] = '\0';
|
|
|
|
printf(buf);
|
|
|
|
aLength -= amt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
InputTestConsumer::OnStopRequest(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMsg)
|
1999-04-19 11:17:37 +04:00
|
|
|
{
|
|
|
|
gKeepRunning = 0;
|
1999-07-10 15:26:59 +04:00
|
|
|
printf("+++ OnStopRequest status %x +++\n", aStatus);
|
1999-04-19 11:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-22 22:24:02 +04:00
|
|
|
nsresult NS_AutoregisterComponents()
|
|
|
|
{
|
|
|
|
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
1999-04-19 11:17:37 +04:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
1999-06-08 16:24:10 +04:00
|
|
|
if (argc < 2) {
|
1999-04-19 11:17:37 +04:00
|
|
|
printf("usage: %s <host>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int port;
|
|
|
|
char* hostName = argv[1];
|
|
|
|
//nsString portString(argv[2]);
|
|
|
|
|
|
|
|
//port = portString.ToInteger(&rv);
|
|
|
|
port = 13;
|
|
|
|
|
1999-06-22 22:24:02 +04:00
|
|
|
rv = NS_AutoregisterComponents();
|
1999-04-19 11:17:37 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-29 05:52:30 +04:00
|
|
|
nsCOMPtr<nsIEventQueue> eventQ;
|
|
|
|
rv = eventQService->GetThreadEventQueue(PR_CurrentThread(), getter_AddRefs(eventQ));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-04-19 11:17:37 +04:00
|
|
|
NS_WITH_SERVICE(nsISocketTransportService, sts, kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
nsIChannel* transport;
|
1999-04-19 11:17:37 +04:00
|
|
|
|
1999-10-13 14:24:12 +04:00
|
|
|
rv = sts->CreateTransport(hostName, port, nsnull, &transport);
|
1999-04-19 11:17:37 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-07-20 12:46:33 +04:00
|
|
|
transport->AsyncRead(0, -1, nsnull, new InputTestConsumer);
|
1999-04-19 11:17:37 +04:00
|
|
|
|
|
|
|
NS_RELEASE(transport);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
while ( gKeepRunning ) {
|
1999-06-03 22:34:51 +04:00
|
|
|
|
1999-05-04 22:50:59 +04:00
|
|
|
#ifdef WIN32
|
1999-04-19 11:17:37 +04:00
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
1999-06-03 22:34:51 +04:00
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
/* Mac stuff is missing here! */
|
|
|
|
#else
|
1999-06-04 02:12:26 +04:00
|
|
|
PLEvent *gEvent;
|
1999-06-03 22:34:51 +04:00
|
|
|
rv = eventQ->GetEvent(&gEvent);
|
1999-06-08 16:24:10 +04:00
|
|
|
rv = eventQ->HandleEvent(gEvent);
|
1999-06-03 22:34:51 +04:00
|
|
|
#endif
|
1999-04-19 11:17:37 +04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
sts->Shutdown();
|
|
|
|
NS_RELEASE(sts);
|
|
|
|
return 0;
|
|
|
|
}
|
1999-06-22 22:24:02 +04:00
|
|
|
|