1999-06-08 01:33:30 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
1999-05-31 09:18:19 +04:00
|
|
|
*
|
1999-11-06 06:40:37 +03: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
|
|
|
*
|
1999-11-06 06:40:37 +03: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.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
1999-05-31 09:18:19 +04:00
|
|
|
* Communications Corporation. Portions created by Netscape are
|
1999-11-06 06:40:37 +03:00
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
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
|
|
|
|
1999-08-27 14:33:37 +04:00
|
|
|
#define NSPIPE2
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
#include "nsISocketTransportService.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-06-08 01:33:30 +04:00
|
|
|
#include "nsIChannel.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
#include "nsIStreamObserver.h"
|
|
|
|
#include "nsIStreamListener.h"
|
1999-08-27 14:33:37 +04:00
|
|
|
#ifndef NSPIPE2
|
1999-06-09 00:57:32 +04:00
|
|
|
#include "nsIBuffer.h"
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
#include "nsIPipe.h"
|
|
|
|
#endif
|
1999-06-09 00:57:32 +04:00
|
|
|
#include "nsIBufferInputStream.h"
|
|
|
|
#include "nsIBufferOutputStream.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"
|
|
|
|
#include "nsIProgressEventSink.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
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,
|
|
|
|
PRBool aAsyncFlag, PRBool testAsyncRead);
|
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
|
|
|
|
NS_DECL_NSISTREAMOBSERVER
|
|
|
|
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
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
void SetAsyncOpenCompleted() { mAsyncOpenCompleted = PR_TRUE; }
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnProgress(nsIChannel *channel, nsISupports *ctxt, PRUint32 aProgress, PRUint32 aProgressMax) {
|
|
|
|
putc('+', stderr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStatus(nsIChannel *channel, nsISupports *ctxt, const PRUnichar *aMsg) {
|
|
|
|
putc('?', stderr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
protected:
|
1999-08-27 14:33:37 +04:00
|
|
|
#ifndef NSPIPE2
|
1999-07-10 15:26:59 +04:00
|
|
|
nsIBuffer* mBuffer;
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
nsIBufferOutputStream* mOut;
|
|
|
|
#endif
|
1999-07-03 08:14:28 +04:00
|
|
|
nsIBufferInputStream* 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
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
nsIChannel* mTransport;
|
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
|
|
|
|
|
|
|
PRBool mTestAsyncOpen;
|
|
|
|
PRBool mAsyncOpenCompleted;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class TestConnectionOpenObserver : public nsIStreamObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSISTREAMOBSERVER
|
|
|
|
|
|
|
|
TestConnectionOpenObserver(TestConnection* test)
|
|
|
|
: mTestConnection(test) {
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
virtual ~TestConnectionOpenObserver() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
TestConnection* mTestConnection;
|
|
|
|
|
1999-07-03 08:14:28 +04:00
|
|
|
};
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(TestConnectionOpenObserver, nsIStreamObserver);
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestConnectionOpenObserver::OnStartRequest(nsIChannel* channel, nsISupports* context)
|
|
|
|
{
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
|
|
|
printf("\n+++ TestConnectionOpenObserver::OnStartRequest +++. Context = %p\n", context);
|
1999-10-06 12:26:01 +04:00
|
|
|
|
|
|
|
mTestConnection->SetAsyncOpenCompleted();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TestConnectionOpenObserver::OnStopRequest(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMsg)
|
|
|
|
{
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose || NS_FAILED(aStatus))
|
|
|
|
printf("\n+++ TestConnectionOpenObserver::OnStopRequest (status = %x) +++."
|
|
|
|
"\tContext = %p\n",
|
|
|
|
aStatus, context);
|
1999-10-06 12:26:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
TestConnection::OnStartRequest(nsIChannel* channel, nsISupports* context)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
1999-10-06 12:26:01 +04:00
|
|
|
NS_ASSERTION(!mTestAsyncOpen || mAsyncOpenCompleted, "AsyncOpen failed");
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose)
|
|
|
|
printf("\n+++ TestConnection::OnStartRequest +++. Context = %p\n", context);
|
1999-05-31 09:18:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-07-07 12:08:40 +04:00
|
|
|
TestConnection::OnDataAvailable(nsIChannel* channel, 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
|
|
|
NS_ASSERTION(!mTestAsyncOpen || mAsyncOpenCompleted, "AsyncOpen failed");
|
|
|
|
|
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",
|
|
|
|
context, aLength);
|
|
|
|
|
|
|
|
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
|
1999-07-07 12:08:40 +04:00
|
|
|
TestConnection::OnStopRequest(nsIChannel* channel,
|
|
|
|
nsISupports* context,
|
1999-07-03 08:14:28 +04:00
|
|
|
nsresult aStatus,
|
|
|
|
const PRUnichar* aMsg)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
1999-10-06 12:26:01 +04:00
|
|
|
NS_ASSERTION(!mTestAsyncOpen || mAsyncOpenCompleted, "AsyncOpen failed");
|
|
|
|
|
2000-03-06 00:26:01 +03:00
|
|
|
if (gVerbose || NS_FAILED(aStatus))
|
|
|
|
printf("\n+++ TestConnection::OnStopRequest (status = %x) +++."
|
|
|
|
"\tContext = %p\n",
|
|
|
|
aStatus, 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,
|
|
|
|
PRBool aAsyncFlag, PRBool testAsyncRead)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
mIsAsync = aAsyncFlag;
|
1999-07-03 08:14:28 +04:00
|
|
|
|
1999-10-06 12:26:01 +04:00
|
|
|
mTestAsyncOpen = testAsyncRead;
|
|
|
|
mAsyncOpenCompleted = PR_FALSE;
|
|
|
|
|
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
|
|
|
#ifndef NSPIPE2
|
1999-07-10 15:26:59 +04:00
|
|
|
mBuffer = nsnull;
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
mOut = nsnull;
|
|
|
|
#endif
|
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...
|
|
|
|
NS_WITH_SERVICE(nsISocketTransportService, sts, kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-12-04 13:01:32 +03:00
|
|
|
rv = sts->CreateTransport(aHostName, aPort, aHostName, 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.
|
|
|
|
rv = mTransport->SetNotificationCallbacks(this);
|
|
|
|
}
|
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)) {
|
1999-08-27 14:33:37 +04:00
|
|
|
#ifndef NSPIPE2
|
1999-07-10 15:26:59 +04:00
|
|
|
rv = NS_NewBuffer(&mBuffer, 1024, 4096, nsnull);
|
|
|
|
rv = NS_NewBufferInputStream(&mStream, mBuffer);
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
rv = NS_NewPipe(&mStream, &mOut, nsnull, 1024, 4096);
|
|
|
|
#endif
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Synchronous transport...
|
|
|
|
else {
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = mTransport->OpenInputStream(&mInStream);
|
|
|
|
rv = mTransport->OpenOutputStream(&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
|
|
|
#ifndef NSPIPE2
|
1999-07-10 15:26:59 +04:00
|
|
|
NS_IF_RELEASE(mBuffer);
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
NS_IF_RELEASE(mOut);
|
|
|
|
#endif
|
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,
|
|
|
|
nsIStreamObserver,
|
|
|
|
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...
|
|
|
|
NS_WITH_SERVICE(nsIEventQueueService, eventQService, kEventQueueServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = eventQService->CreateThreadEventQueue();
|
|
|
|
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) {
|
1999-10-06 12:26:01 +04:00
|
|
|
|
|
|
|
if (mTestAsyncOpen) {
|
|
|
|
TestConnectionOpenObserver* obs = new TestConnectionOpenObserver(this);
|
|
|
|
if (obs == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(obs);
|
|
|
|
rv = mTransport->AsyncOpen(obs, nsnull);
|
|
|
|
NS_RELEASE(obs);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("Error: AsyncOpen failed...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
//
|
|
|
|
// Initiate an async read...
|
|
|
|
//
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = mTransport->AsyncRead(this, mTransport);
|
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)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
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)
|
|
|
|
printf("\n+++ Request is: %c. Context = %p\n", mBufferChar, 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-07-11 06:43:37 +04:00
|
|
|
#if 0
|
|
|
|
rv = mStream->Fill(buffer, size, &bytesWritten);
|
|
|
|
#else
|
1999-08-27 14:33:37 +04:00
|
|
|
#ifndef NSPIPE2
|
1999-07-10 15:26:59 +04:00
|
|
|
rv = mBuffer->Write(buffer, size, &bytesWritten);
|
1999-08-27 14:33:37 +04:00
|
|
|
#else
|
|
|
|
rv = mOut->Write(buffer, size, &bytesWritten);
|
|
|
|
#endif
|
1999-07-11 06:43:37 +04:00
|
|
|
#endif
|
1999-06-03 09:45:23 +04:00
|
|
|
|
|
|
|
// Write the buffer to the server...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2000-03-29 07:58:50 +04:00
|
|
|
rv = mTransport->SetTransferCount(bytesWritten);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = mTransport->AsyncWrite(mStream, /* mOutputObserver */ 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;
|
|
|
|
|
|
|
|
if (mTransport) {
|
|
|
|
rv = mTransport->Suspend();
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult TestConnection::Resume(void)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (mTransport) {
|
|
|
|
rv = mTransport->Resume();
|
|
|
|
} 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;
|
|
|
|
|
|
|
|
NS_RELEASE(gPeriodicTimer);
|
|
|
|
|
|
|
|
if (NS_OK == NS_NewTimer(&gPeriodicTimer)) {
|
|
|
|
gPeriodicTimer->Init(TimerCallback, nsnull, 1000*5);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* USE_TIMERS */
|
|
|
|
|
1999-08-14 02:18:34 +04:00
|
|
|
nsresult NS_AutoregisterComponents();
|
1999-06-22 22:24:02 +04:00
|
|
|
nsresult NS_AutoregisterComponents()
|
|
|
|
{
|
|
|
|
nsresult rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup, NULL /* default */);
|
|
|
|
return rv;
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Parse the command line args...
|
|
|
|
//
|
|
|
|
// -----
|
|
|
|
/// if (argc < 3) {
|
2000-03-06 00:26:01 +03:00
|
|
|
/// printf("usage: %s [-sync|-asyncopen|-silent] <host> <path>\n", argv[0]);
|
1999-05-31 09:18:19 +04:00
|
|
|
/// return -1;
|
|
|
|
/// }
|
|
|
|
|
1999-07-12 10:22:39 +04:00
|
|
|
PRBool bIsAsync = PR_TRUE;
|
1999-10-06 12:26:01 +04:00
|
|
|
PRBool bTestAsyncOpen = PR_FALSE;
|
1999-07-12 10:22:39 +04:00
|
|
|
char* hostName = nsnull;
|
|
|
|
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;
|
|
|
|
}
|
1999-10-06 12:26:01 +04:00
|
|
|
if (PL_strcasecmp(argv[i], "-asyncopen") == 0) {
|
|
|
|
bTestAsyncOpen = PR_TRUE;
|
|
|
|
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
|
|
|
|
(void) PR_PutEnv("NSPR_LOG_MODULES=nsSocketTransport:5");
|
|
|
|
(void) PR_PutEnv("NSPR_LOG_FILE=nspr.log");
|
|
|
|
PR_Init_Log();
|
|
|
|
#endif
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Initialize XPCom...
|
|
|
|
//
|
|
|
|
// -----
|
1999-06-22 22:24:02 +04:00
|
|
|
|
|
|
|
rv = NS_AutoregisterComponents();
|
1999-05-31 09:18:19 +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-03 09:45:23 +04:00
|
|
|
//
|
|
|
|
// Create the connections and threads...
|
|
|
|
//
|
|
|
|
for (i=0; i<NUM_TEST_THREADS; i++) {
|
1999-10-06 12:26:01 +04:00
|
|
|
gConnections[i] = new TestConnection(hostName, 7, bIsAsync, bTestAsyncOpen);
|
1999-10-02 03:32:12 +04:00
|
|
|
rv = NS_NewThread(&gThreads[i], gConnections[i], 0, PR_JOINABLE_THREAD);
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
|
1999-08-14 02:18:34 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
#if defined(USE_TIMERS)
|
|
|
|
//
|
|
|
|
// Start up the timer to test Suspend/Resume APIs on the transport...
|
|
|
|
//
|
|
|
|
if (NS_OK == NS_NewTimer(&gPeriodicTimer)) {
|
|
|
|
gPeriodicTimer->Init(TimerCallback, nsnull, 1000);
|
|
|
|
}
|
|
|
|
#endif /* USE_TIMERS */
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
// Enter the message pump to allow the URL load to proceed.
|
1999-08-14 02:18:34 +04:00
|
|
|
Pump_PLEvents(eventQService);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
PRTime endTime;
|
|
|
|
endTime = PR_Now();
|
1999-08-14 02:18:34 +04:00
|
|
|
|
1999-12-29 03:20:10 +03:00
|
|
|
// printf("Elapsed time: %d\n", (PRInt32)(endTime/1000UL - gElapsedTime/1000UL));
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|