2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-05-31 09:18:19 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1999-05-31 09:18:19 +04:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-05-31 09:18:19 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:40:37 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-05-31 09:18:19 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
1999-07-12 10:22:39 +04:00
|
|
|
//#define USE_TIMERS // Only use nsITimer on Windows (for now...)
|
1999-05-31 09:18:19 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2000-02-29 18:40:42 +03:00
|
|
|
#ifdef XP_OS2
|
|
|
|
#include <os2.h>
|
|
|
|
#endif
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
#include "nspr.h"
|
2000-01-28 07:00:28 +03:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#include "pprio.h" // PR_Init_Log
|
|
|
|
#endif
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIRequest.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
2002-01-30 00:22:13 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
2001-04-10 10:01:08 +04:00
|
|
|
#include "nsIRequestObserver.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
#include "nsIStreamListener.h"
|
1999-08-27 14:33:37 +04:00
|
|
|
#include "nsIPipe.h"
|
2000-08-22 11:03:33 +04:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
1999-09-15 13:49:18 +04:00
|
|
|
#include "nsIRunnable.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
#include "nsIThread.h"
|
1999-06-03 09:45:23 +04:00
|
|
|
#include "nsITimer.h"
|
2000-03-06 00:26:01 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-06 01:28:38 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2000-03-06 00:26:01 +03:00
|
|
|
#include "nsIProgressEventSink.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
#include "nsCRT.h"
|
2001-01-24 01:10:34 +03:00
|
|
|
#include "nsNetUtil.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-08-14 02:18:34 +04:00
|
|
|
#if defined(XP_MAC)
|
|
|
|
#include "macstdlibextras.h"
|
|
|
|
// Set up the toolbox and (if DEBUG) the console. Do this in a static initializer,
|
|
|
|
// to make it as unlikely as possible that somebody calls printf() before we get initialized.
|
|
|
|
static struct MacInitializer { MacInitializer() { InitializeMacToolbox(); InitializeSIOUX(true); } } gInitializer;
|
|
|
|
#endif // XP_MAC
|
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
// forward declarations...
|
1999-06-03 09:45:23 +04:00
|
|
|
class TestConnection;
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
|
1999-12-29 03:20:10 +03:00
|
|
|
//static PRTime gElapsedTime;
|
1999-05-31 09:18:19 +04:00
|
|
|
static int gKeepRunning = 1;
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
#define NUM_TEST_THREADS 15
|
|
|
|
#define TRANSFER_AMOUNT 64
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
static TestConnection* gConnections[NUM_TEST_THREADS];
|
|
|
|
static nsIThread* gThreads[NUM_TEST_THREADS];
|
|
|
|
//static nsITimer* gPeriodicTimer;
|
|
|
|
static PRBool gVerbose = PR_TRUE;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
|
1999-08-14 02:18:34 +04:00
|
|
|
void Pump_PLEvents(nsIEventQueueService * eventQService);
|
|
|
|
void Pump_PLEvents(nsIEventQueueService * eventQService)
|
1999-07-03 08:14:28 +04:00
|
|
|
{
|
1999-08-14 02:18:34 +04:00
|
|
|
nsIEventQueue* eventQ = nsnull;
|
1999-11-30 03:32:43 +03:00
|
|
|
eventQService->GetThreadEventQueue(NS_CURRENT_THREAD, &eventQ);
|
1999-08-14 02:18:34 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
while ( gKeepRunning ) {
|
|
|
|
#ifdef WIN32
|
|
|
|
MSG msg;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
if (GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
} else {
|
|
|
|
gKeepRunning = FALSE;
|
|
|
|
}
|
2000-02-29 18:40:42 +03:00
|
|
|
#elif defined(XP_OS2)
|
|
|
|
QMSG qmsg;
|
|
|
|
|
|
|
|
if (WinGetMsg(0, &qmsg, 0, 0, 0))
|
|
|
|
WinDispatchMsg(0, &qmsg);
|
|
|
|
else
|
|
|
|
gKeepRunning = FALSE;
|
1999-07-03 08:14:28 +04:00
|
|
|
#else
|
1999-07-10 15:26:59 +04:00
|
|
|
nsresult rv;
|
1999-07-03 08:14:28 +04:00
|
|
|
PLEvent *gEvent;
|
1999-08-14 02:18:34 +04:00
|
|
|
rv = eventQ->GetEvent(&gEvent);
|
|
|
|
rv = eventQ->HandleEvent(gEvent);
|
1999-07-03 08:14:28 +04:00
|
|
|
#endif /* !WIN32 */
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
1999-07-03 08:14:28 +04:00
|
|
|
// TestConnection class...
|
1999-05-31 09:18:19 +04:00
|
|
|
//
|
|
|
|
// -----
|
1999-07-03 08:14:28 +04:00
|
|
|
|
|
|
|
class TestConnection : public nsIRunnable,
|
2000-03-06 00:26:01 +03:00
|
|
|
public nsIStreamListener,
|
|
|
|
public nsIInterfaceRequestor,
|
|
|
|
public nsIProgressEventSink
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
|
|
|
public:
|
1999-10-06 12:26:01 +04:00
|
|
|
TestConnection(const char* aHostName, PRInt32 aPort,
|
2000-06-16 19:02:14 +04:00
|
|
|
PRBool aAsyncFlag);
|
1999-07-13 06:37:08 +04:00
|
|
|
virtual ~TestConnection();
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
1999-09-16 05:16:22 +04:00
|
|
|
NS_DECL_NSIRUNNABLE
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
1999-09-16 05:16:22 +04:00
|
|
|
NS_DECL_NSISTREAMLISTENER
|
1999-06-08 01:33:30 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
// TestConnection methods...
|
|
|
|
nsresult WriteBuffer(void);
|
|
|
|
nsresult ReadBuffer(void);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
nsresult Process(void);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
nsresult Suspend(void);
|
|
|
|
nsresult Resume(void);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
NS_IMETHOD GetInterface(const nsIID & uuid, void * *result) {
|
|
|
|
if (uuid.Equals(NS_GET_IID(nsIProgressEventSink))) {
|
|
|
|
*result = (nsIProgressEventSink*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHOD OnProgress(nsIRequest *request, nsISupports *ctxt,
|
2000-07-25 09:45:56 +04:00
|
|
|
PRUint32 aProgress, PRUint32 aProgressMax) {
|
2000-03-06 00:26:01 +03:00
|
|
|
putc('+', stderr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHOD OnStatus(nsIRequest *request, nsISupports *ctxt,
|
2000-07-25 09:45:56 +04:00
|
|
|
nsresult aStatus, const PRUnichar* aStatusArg) {
|
2000-03-06 00:26:01 +03:00
|
|
|
putc('?', stderr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
protected:
|
2000-08-22 11:03:33 +04:00
|
|
|
nsIOutputStream* mOut;
|
|
|
|
nsIInputStream* mStream;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
nsIInputStream* mInStream;
|
|
|
|
nsIOutputStream* mOutStream;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsITransport* mTransport;
|
|
|
|
nsCOMPtr<nsIRequest> mReadRequest;
|
|
|
|
nsCOMPtr<nsIRequest> mWriteRequest;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
PRBool mIsAsync;
|
|
|
|
PRInt32 mBufferLength;
|
|
|
|
char mBufferChar;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
PRInt32 mBytesRead;
|
1999-10-06 12:26:01 +04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
class TestConnectionOpenObserver : public nsIRequestObserver
|
1999-10-06 12:26:01 +04:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_DECL_NSIREQUESTOBSERVER
|
1999-10-06 12:26:01 +04:00
|
|
|
|
|
|
|
TestConnectionOpenObserver(TestConnection* test)
|
|
|
|
: mTestConnection(test) {
|
|
|
|
}
|
|
|
|
virtual ~TestConnectionOpenObserver() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
TestConnection* mTestConnection;
|
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
};
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(TestConnectionOpenObserver, nsIRequestObserver);
|
1999-10-06 12:26:01 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConnectionOpenObserver::OnStartRequest(nsIRequest *request, nsISupports* context)
|
1999-10-06 12:26:01 +04:00
|
|
|
{
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
2001-02-21 23:38:08 +03:00
|
|
|
printf("\n+++ TestConnectionOpenObserver::OnStartRequest +++. Context = %p\n", (void*)context);
|
1999-10-06 12:26:01 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConnectionOpenObserver::OnStopRequest(nsIRequest *request, nsISupports* context,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
1999-10-06 12:26:01 +04:00
|
|
|
{
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose || NS_FAILED(aStatus))
|
|
|
|
printf("\n+++ TestConnectionOpenObserver::OnStopRequest (status = %x) +++."
|
|
|
|
"\tContext = %p\n",
|
2001-02-21 23:38:08 +03:00
|
|
|
aStatus, (void*)context);
|
1999-10-06 12:26:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConnection::OnStartRequest(nsIRequest *request, nsISupports* context)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
2001-02-21 23:38:08 +03:00
|
|
|
printf("\n+++ TestConnection::OnStartRequest +++. Context = %p\n", (void*)context);
|
1999-05-31 09:18:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConnection::OnDataAvailable(nsIRequest *request, nsISupports* context,
|
1999-07-03 08:14:28 +04:00
|
|
|
nsIInputStream *aIStream,
|
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
2000-03-06 00:26:01 +03:00
|
|
|
nsresult rv;
|
1999-10-06 12:26:01 +04:00
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
char buf[TRANSFER_AMOUNT];
|
1999-05-31 09:18:19 +04:00
|
|
|
PRUint32 amt;
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
|
|
|
printf("\n+++ TestConnection::OnDavaAvailable +++."
|
|
|
|
"\tContext = %p length = %d\n",
|
2001-02-21 23:38:08 +03:00
|
|
|
(void*)context, aLength);
|
2000-03-06 00:26:01 +03:00
|
|
|
|
|
|
|
while (aLength > 0) {
|
|
|
|
PRInt32 cnt = PR_MIN(TRANSFER_AMOUNT, aLength);
|
|
|
|
rv = aIStream->Read(buf, cnt, &amt);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Read failed");
|
1999-05-31 09:18:19 +04:00
|
|
|
mBytesRead += amt;
|
2000-03-06 00:26:01 +03:00
|
|
|
aLength -= amt;
|
1999-05-31 09:18:19 +04:00
|
|
|
buf[amt] = '\0';
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
|
|
|
puts(buf);
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
if (mBufferLength == mBytesRead) {
|
1999-05-31 09:18:19 +04:00
|
|
|
mBytesRead = 0;
|
1999-07-03 08:14:28 +04:00
|
|
|
WriteBuffer();
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-02-21 23:38:08 +03:00
|
|
|
TestConnection::OnStopRequest(nsIRequest *request, nsISupports* context,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose || NS_FAILED(aStatus))
|
|
|
|
printf("\n+++ TestConnection::OnStopRequest (status = %x) +++."
|
|
|
|
"\tContext = %p\n",
|
2001-02-21 23:38:08 +03:00
|
|
|
aStatus, (void*)context);
|
1999-05-31 09:18:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
TestConnection::TestConnection(const char* aHostName, PRInt32 aPort,
|
2000-06-16 19:02:14 +04:00
|
|
|
PRBool aAsyncFlag)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
mIsAsync = aAsyncFlag;
|
1999-07-03 08:14:28 +04:00
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
mBufferLength = TRANSFER_AMOUNT;
|
1999-06-03 09:45:23 +04:00
|
|
|
mBufferChar = 'a';
|
1999-07-03 08:14:28 +04:00
|
|
|
mBytesRead = 0;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
mTransport = nsnull;
|
1999-08-27 14:33:37 +04:00
|
|
|
mOut = nsnull;
|
1999-05-31 09:18:19 +04:00
|
|
|
mStream = nsnull;
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
mInStream = nsnull;
|
|
|
|
mOutStream = nsnull;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
// Create a socket transport...
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsISocketTransportService> sts =
|
|
|
|
do_GetService(kSocketTransportServiceCID, &rv);
|
1999-05-31 09:18:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-09-13 06:21:05 +04:00
|
|
|
rv = sts->CreateTransport(aHostName, aPort, nsnull, 0, 0, &mTransport);
|
2000-03-06 00:26:01 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Set up the notification callbacks to provide a progress event sink.
|
|
|
|
// That way we exercise the progress notification proxy code.
|
2001-03-06 09:22:58 +03:00
|
|
|
mTransport->SetNotificationCallbacks((nsIInterfaceRequestor*)this, PR_FALSE);
|
2000-03-06 00:26:01 +03:00
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
|
1999-08-14 02:18:34 +04:00
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-03 09:45:23 +04:00
|
|
|
if (mIsAsync) {
|
|
|
|
// Create a stream for the data being written to the server...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = NS_NewPipe(&mStream, &mOut, 1024, 4096);
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Synchronous transport...
|
|
|
|
else {
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mTransport->OpenInputStream(0, -1, 0, &mInStream);
|
|
|
|
rv = mTransport->OpenOutputStream(0, -1, 0, &mOutStream);
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TestConnection::~TestConnection()
|
|
|
|
{
|
1999-06-03 09:45:23 +04:00
|
|
|
NS_IF_RELEASE(mTransport);
|
|
|
|
// Async resources...
|
1999-05-31 09:18:19 +04:00
|
|
|
NS_IF_RELEASE(mStream);
|
1999-08-27 14:33:37 +04:00
|
|
|
NS_IF_RELEASE(mOut);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
// Sync resources...
|
|
|
|
NS_IF_RELEASE(mInStream);
|
|
|
|
NS_IF_RELEASE(mOutStream);
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS5(TestConnection,
|
|
|
|
nsIRunnable,
|
|
|
|
nsIStreamListener,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsIRequestObserver,
|
2000-03-06 00:26:01 +03:00
|
|
|
nsIInterfaceRequestor,
|
|
|
|
nsIProgressEventSink);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestConnection::Run(void)
|
|
|
|
{
|
1999-06-07 11:12:56 +04:00
|
|
|
nsresult rv = NS_OK;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
// Create the Event Queue for this thread...
|
2002-07-04 18:29:25 +04:00
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
2001-07-25 11:54:28 +04:00
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
1999-07-03 08:14:28 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-01-02 04:13:15 +03:00
|
|
|
rv = eventQService->CreateMonitoredThreadEventQueue();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
//
|
|
|
|
// Make sure that all resources were allocated in the constructor...
|
|
|
|
//
|
|
|
|
if (!mTransport) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (mIsAsync) {
|
2002-07-04 18:29:25 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
//
|
|
|
|
// Initiate an async read...
|
|
|
|
//
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = mTransport->AsyncRead(this, mTransport, 0, -1, 0, getter_AddRefs(mReadRequest));
|
1999-06-03 09:45:23 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("Error: AsyncRead failed...");
|
1999-07-03 08:14:28 +04:00
|
|
|
} else {
|
|
|
|
rv = WriteBuffer();
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
1999-08-14 02:18:34 +04:00
|
|
|
Pump_PLEvents(eventQService);
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
1999-07-12 10:22:39 +04:00
|
|
|
else {
|
|
|
|
while (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = WriteBuffer();
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-07-12 10:22:39 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = ReadBuffer();
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
|
|
|
printf("Transport thread exiting...\n");
|
1999-05-31 09:18:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult TestConnection::WriteBuffer(void)
|
|
|
|
{
|
2002-02-18 18:22:56 +03:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
1999-05-31 09:18:19 +04:00
|
|
|
char *buffer;
|
1999-06-03 09:45:23 +04:00
|
|
|
PRInt32 size;
|
1999-05-31 09:18:19 +04:00
|
|
|
PRUint32 bytesWritten;
|
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
if (mBufferChar == 'z') {
|
|
|
|
mBufferChar = 'a';
|
|
|
|
} else {
|
|
|
|
mBufferChar++;
|
|
|
|
}
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
2001-02-21 23:38:08 +03:00
|
|
|
printf("\n+++ Request is: %c. Context = %p\n", mBufferChar, (void*)mTransport);
|
1999-06-11 12:36:40 +04:00
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
// Create and fill a test buffer of data...
|
|
|
|
buffer = (char*)PR_Malloc(mBufferLength + 4);
|
|
|
|
|
|
|
|
if (buffer) {
|
1999-06-03 09:45:23 +04:00
|
|
|
for (size=0; size<mBufferLength-2; size++) {
|
|
|
|
buffer[size] = mBufferChar;
|
|
|
|
}
|
|
|
|
buffer[size++] = '\r';
|
|
|
|
buffer[size++] = '\n';
|
|
|
|
buffer[size] = 0;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Async case...
|
|
|
|
//
|
|
|
|
if (mStream) {
|
1999-08-27 14:33:37 +04:00
|
|
|
rv = mOut->Write(buffer, size, &bytesWritten);
|
1999-06-03 09:45:23 +04:00
|
|
|
|
|
|
|
// Write the buffer to the server...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = NS_AsyncWriteFromStream(
|
|
|
|
getter_AddRefs(mWriteRequest),
|
|
|
|
mTransport, mStream, 0, bytesWritten, 0,
|
|
|
|
nsnull, mTransport);
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
|
|
|
// Wait for the write to complete...
|
1999-07-03 08:14:28 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
1999-06-03 09:45:23 +04:00
|
|
|
printf("Error: AsyncWrite failed...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Synchronous case...
|
|
|
|
//
|
|
|
|
else if (mOutStream) {
|
|
|
|
rv = mOutStream->Write(buffer, size, &bytesWritten);
|
1999-07-30 08:17:23 +04:00
|
|
|
NS_ASSERTION((PRUint32)size == bytesWritten, "Not enough was written...");
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
PR_Free(buffer);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult TestConnection::ReadBuffer(void)
|
|
|
|
{
|
1999-06-03 09:45:23 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Synchronous case...
|
|
|
|
//
|
|
|
|
if (mInStream) {
|
|
|
|
char *buffer;
|
|
|
|
PRUint32 bytesRead;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
buffer = (char*)PR_Malloc(mBufferLength + 4);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
if (buffer) {
|
1999-07-12 10:22:39 +04:00
|
|
|
rv = mInStream->Read(buffer, mBufferLength, &bytesRead);
|
1999-06-03 09:45:23 +04:00
|
|
|
|
1999-09-10 02:05:05 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && bytesRead > 0) {
|
1999-07-12 10:22:39 +04:00
|
|
|
buffer[bytesRead] = '\0';
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
|
|
|
printf("TestConnection::ReadBuffer. Read %d bytes\n", bytesRead);
|
1999-07-12 10:22:39 +04:00
|
|
|
puts(buffer);
|
|
|
|
}
|
1999-06-03 09:45:23 +04:00
|
|
|
PR_Free(buffer);
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TestConnection::Process(void)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
nsresult TestConnection::Suspend(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
if (mReadRequest) {
|
|
|
|
rv = mReadRequest->Suspend();
|
1999-06-03 09:45:23 +04:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TestConnection::Resume(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
if (mReadRequest) {
|
|
|
|
rv = mReadRequest->Resume();
|
1999-06-03 09:45:23 +04:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(USE_TIMERS)
|
|
|
|
|
|
|
|
void TimerCallback(nsITimer* aTimer, void* aClosure)
|
|
|
|
{
|
|
|
|
static PRBool flag = PR_FALSE;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (flag) {
|
|
|
|
printf("Resuming connections...\n");
|
|
|
|
} else {
|
|
|
|
printf("Suspending connections...\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (i=0; i<NUM_TEST_THREADS; i++) {
|
|
|
|
TestConnection* connection;
|
|
|
|
|
|
|
|
connection = gConnections[i];
|
|
|
|
if (connection) {
|
|
|
|
if (flag) {
|
|
|
|
connection->Resume();
|
|
|
|
} else {
|
|
|
|
connection->Suspend();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
flag = !flag;
|
|
|
|
|
2000-09-14 03:57:52 +04:00
|
|
|
gPeriodicTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
2000-05-17 06:49:35 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-03 09:45:23 +04:00
|
|
|
gPeriodicTimer->Init(TimerCallback, nsnull, 1000*5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_TIMERS */
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Parse the command line args...
|
|
|
|
//
|
|
|
|
// -----
|
2002-07-04 18:29:25 +04:00
|
|
|
#if 0
|
|
|
|
if (argc < 3) {
|
|
|
|
printf("usage: %s [-sync|-silent] <host> <path>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
#endif
|
1999-07-12 10:22:39 +04:00
|
|
|
PRBool bIsAsync = PR_TRUE;
|
2001-11-07 09:24:10 +03:00
|
|
|
const char* hostName = nsnull;
|
1999-07-12 10:22:39 +04:00
|
|
|
int i;
|
|
|
|
|
1999-08-14 02:18:34 +04:00
|
|
|
|
1999-07-12 10:22:39 +04:00
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
// Turn on synchronous mode...
|
|
|
|
if (PL_strcasecmp(argv[i], "-sync") == 0) {
|
|
|
|
bIsAsync = PR_FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
2000-03-06 00:26:01 +03:00
|
|
|
if (PL_strcasecmp(argv[i], "-silent") == 0) {
|
|
|
|
gVerbose = PR_FALSE;
|
|
|
|
continue;
|
|
|
|
}
|
1999-07-03 08:14:28 +04:00
|
|
|
|
1999-07-12 10:22:39 +04:00
|
|
|
hostName = argv[i];
|
|
|
|
}
|
|
|
|
if (!hostName) {
|
1999-07-03 08:14:28 +04:00
|
|
|
hostName = "chainsaw";
|
|
|
|
}
|
|
|
|
printf("Using %s as echo server...\n", hostName);
|
1999-08-14 02:18:34 +04:00
|
|
|
|
|
|
|
#ifdef XP_MAC
|
2002-01-07 18:42:08 +03:00
|
|
|
(void) PR_SetEnv("NSPR_LOG_MODULES=nsSocketTransport:5");
|
|
|
|
(void) PR_SetEnv("NSPR_LOG_FILE=nspr.log");
|
1999-08-14 02:18:34 +04:00
|
|
|
PR_Init_Log();
|
|
|
|
#endif
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Initialize XPCom...
|
|
|
|
//
|
|
|
|
// -----
|
2002-07-04 18:29:25 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nsnull, nsnull);
|
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
if (registrar)
|
|
|
|
registrar->AutoRegister(nsnull);
|
|
|
|
|
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
nsCOMPtr<nsIEventQueueService> eventQService =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-22 22:24:02 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
//
|
|
|
|
// Create the connections and threads...
|
|
|
|
//
|
|
|
|
for (i=0; i<NUM_TEST_THREADS; i++) {
|
|
|
|
gConnections[i] = new TestConnection(hostName, 7, bIsAsync);
|
|
|
|
rv = NS_NewThread(&gThreads[i], gConnections[i], 0, PR_JOINABLE_THREAD);
|
|
|
|
}
|
1999-08-14 02:18:34 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
#if defined(USE_TIMERS)
|
2002-07-04 18:29:25 +04:00
|
|
|
//
|
|
|
|
// Start up the timer to test Suspend/Resume APIs on the transport...
|
|
|
|
//
|
|
|
|
gPeriodicTimer = do_CreateInstance("@mozilla.org/timer;1", &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
gPeriodicTimer->Init(TimerCallback, nsnull, 1000);
|
|
|
|
}
|
1999-06-03 09:45:23 +04:00
|
|
|
#endif /* USE_TIMERS */
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
|
|
|
Pump_PLEvents(eventQService);
|
1999-08-14 02:18:34 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
PRTime endTime;
|
|
|
|
endTime = PR_Now();
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2002-07-04 18:29:25 +04:00
|
|
|
// printf("Elapsed time: %d\n", (PRInt32)(endTime/1000UL - gElapsedTime/1000UL));
|
|
|
|
} // this scopes the nsCOMPtrs
|
|
|
|
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
|
|
|
rv = NS_ShutdownXPCOM(nsnull);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
1999-05-31 09:18:19 +04:00
|
|
|
return 0;
|
|
|
|
}
|