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
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
#ifdef WIN32
|
1999-06-03 09:45:23 +04:00
|
|
|
#define USE_TIMERS // Only use nsITimer on Windows (for now...)
|
1999-05-31 09:18:19 +04:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "nspr.h"
|
|
|
|
|
|
|
|
#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-06-09 00:57:32 +04:00
|
|
|
#include "nsIBuffer.h"
|
|
|
|
#include "nsIBufferInputStream.h"
|
|
|
|
#include "nsIBufferOutputStream.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
#include "nsIThread.h"
|
1999-06-03 09:45:23 +04:00
|
|
|
#include "nsITimer.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
#include "nsCRT.h"
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
// Forward declarations...
|
|
|
|
class TestConnection;
|
|
|
|
class InputConsumer;
|
|
|
|
class OutputObserver;
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
static NS_DEFINE_IID(kEventQueueCID, NS_EVENTQUEUE_CID);
|
|
|
|
|
|
|
|
static PRTime gElapsedTime;
|
|
|
|
static int gKeepRunning = 1;
|
|
|
|
static nsIEventQueue* gEventQ = nsnull;
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
#define NUM_TEST_THREADS 5
|
|
|
|
|
|
|
|
static TestConnection* gConnections[NUM_TEST_THREADS];
|
|
|
|
static nsIThread* gThreads[NUM_TEST_THREADS];
|
|
|
|
static nsITimer* gPeriodicTimer;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
class TestConnection : public nsIRunnable
|
|
|
|
{
|
|
|
|
public:
|
1999-06-03 09:45:23 +04:00
|
|
|
TestConnection(const char* aHostName, PRInt32 aPort, PRBool aAsyncFlag);
|
1999-05-31 09:18:19 +04:00
|
|
|
~TestConnection();
|
|
|
|
|
|
|
|
// nsISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIRunnable interface...
|
|
|
|
NS_IMETHOD Run(void);
|
|
|
|
|
|
|
|
|
|
|
|
nsresult WriteBuffer(void);
|
|
|
|
nsresult ReadBuffer(void);
|
|
|
|
|
|
|
|
nsresult Process(void);
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
nsresult Suspend(void);
|
|
|
|
nsresult Resume(void);
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
void Lock(void) { PR_EnterMonitor(mMonitor); }
|
|
|
|
void Notify(void) { PR_Notify(mMonitor); }
|
|
|
|
void Wait(void) { PR_Wait(mMonitor, PR_INTERVAL_NO_TIMEOUT); }
|
|
|
|
void Unlock(void) { PR_ExitMonitor(mMonitor); }
|
|
|
|
|
|
|
|
PRInt32 GetBufferLength(void) { return mBufferLength; }
|
|
|
|
char GetBufferChar(void) { return mBufferChar; }
|
|
|
|
|
|
|
|
protected:
|
1999-06-09 00:57:32 +04:00
|
|
|
nsIBufferInputStream* mStream;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
nsIInputStream* mInStream;
|
|
|
|
nsIOutputStream* mOutStream;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
InputConsumer* mInputConsumer;
|
|
|
|
OutputObserver* mOutputObserver;
|
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
nsIChannel* mTransport;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
PRBool mIsAsync;
|
1999-05-31 09:18:19 +04:00
|
|
|
PRInt32 mBufferLength;
|
|
|
|
char mBufferChar;
|
|
|
|
|
|
|
|
PRMonitor* mMonitor;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// InputConsumer class...
|
|
|
|
//
|
|
|
|
// -----
|
|
|
|
class InputConsumer : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InputConsumer(TestConnection* aConnection);
|
|
|
|
virtual ~InputConsumer();
|
|
|
|
|
|
|
|
// ISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// IStreamListener interface...
|
|
|
|
NS_IMETHOD OnStartBinding(nsISupports* context);
|
|
|
|
|
|
|
|
NS_IMETHOD OnDataAvailable(nsISupports* context,
|
1999-06-09 00:57:32 +04:00
|
|
|
nsIBufferInputStream *aIStream,
|
1999-05-31 09:18:19 +04:00
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength);
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopBinding(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-08 01:33:30 +04:00
|
|
|
NS_IMETHOD OnStartRequest(nsISupports* context) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopRequest(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg) {
|
1999-06-08 01:33:30 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
TestConnection* mConnection;
|
|
|
|
PRInt32 mBytesRead;
|
|
|
|
};
|
|
|
|
|
|
|
|
InputConsumer::InputConsumer(TestConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
|
|
|
mBytesRead = 0;
|
|
|
|
|
|
|
|
mConnection = aConnection;
|
|
|
|
NS_IF_ADDREF(mConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
InputConsumer::~InputConsumer()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(InputConsumer,nsIStreamListener::GetIID());
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputConsumer::OnStartBinding(nsISupports* context)
|
|
|
|
{
|
|
|
|
printf("\n+++ InputConsumer::OnStartBinding +++. Context = %p\n", context);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputConsumer::OnDataAvailable(nsISupports* context,
|
1999-06-09 00:57:32 +04:00
|
|
|
nsIBufferInputStream *aIStream,
|
1999-05-31 09:18:19 +04:00
|
|
|
PRUint32 aSourceOffset,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
char buf[1025];
|
|
|
|
PRUint32 amt;
|
|
|
|
|
1999-06-11 12:36:40 +04:00
|
|
|
printf("\n+++ InputConsumer::OnDavaAvailable +++. Context = %p length = %d\n", context, aLength);
|
1999-05-31 09:18:19 +04:00
|
|
|
mConnection->Lock();
|
|
|
|
do {
|
|
|
|
nsresult rv = aIStream->Read(buf, 1024, &amt);
|
|
|
|
mBytesRead += amt;
|
|
|
|
buf[amt] = '\0';
|
1999-06-15 03:42:12 +04:00
|
|
|
puts(buf);
|
1999-05-31 09:18:19 +04:00
|
|
|
} while (amt != 0);
|
|
|
|
|
|
|
|
if (mConnection->GetBufferLength() == mBytesRead) {
|
|
|
|
mBytesRead = 0;
|
|
|
|
mConnection->Notify();
|
|
|
|
}
|
|
|
|
mConnection->Unlock();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
InputConsumer::OnStopBinding(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
|
|
|
printf("\n+++ InputConsumer::OnStopBinding (status = %x) +++. Context = %p\n", aStatus, context);
|
1999-06-03 09:45:23 +04:00
|
|
|
mConnection->Lock();
|
|
|
|
mConnection->Notify();
|
|
|
|
mConnection->Unlock();
|
1999-05-31 09:18:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// OutputObserver class...
|
|
|
|
//
|
|
|
|
// -----
|
|
|
|
class OutputObserver : public nsIStreamObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
OutputObserver(TestConnection* aConnection);
|
|
|
|
virtual ~OutputObserver();
|
|
|
|
|
|
|
|
// ISupports interface...
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// IStreamObserver interface...
|
|
|
|
NS_IMETHOD OnStartBinding(nsISupports* context);
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopBinding(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg);
|
1999-06-08 01:33:30 +04:00
|
|
|
|
|
|
|
NS_IMETHOD OnStartRequest(nsISupports* context) {
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopRequest(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg) {
|
1999-06-08 01:33:30 +04:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
protected:
|
|
|
|
TestConnection* mConnection;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
OutputObserver::OutputObserver(TestConnection* aConnection)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
|
|
|
mConnection = aConnection;
|
|
|
|
NS_IF_ADDREF(mConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
OutputObserver::~OutputObserver()
|
|
|
|
{
|
|
|
|
NS_IF_RELEASE(mConnection);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(OutputObserver,nsIStreamObserver::GetIID());
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OutputObserver::OnStartBinding(nsISupports* context)
|
|
|
|
{
|
|
|
|
printf("\n+++ OutputObserver::OnStartBinding +++. Context = %p\n", context);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
OutputObserver::OnStopBinding(nsISupports* context,
|
|
|
|
nsresult aStatus,
|
1999-06-11 19:50:14 +04:00
|
|
|
const PRUnichar* aMsg)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
1999-06-03 09:45:23 +04:00
|
|
|
/// mConnection->Lock();
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
printf("\n+++ OutputObserver::OnStopBinding (status = %x) +++. Context = %p\n", aStatus, context);
|
1999-06-03 09:45:23 +04:00
|
|
|
/// mConnection->Notify();
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
/// mConnection->Unlock();
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// TestConnection class...
|
|
|
|
//
|
|
|
|
// -----
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
TestConnection::TestConnection(const char* aHostName, PRInt32 aPort, PRBool aAsyncFlag)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
mIsAsync = aAsyncFlag;
|
1999-05-31 09:18:19 +04:00
|
|
|
mBufferLength = 255;
|
1999-06-03 09:45:23 +04:00
|
|
|
mBufferChar = 'a';
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
mTransport = nsnull;
|
|
|
|
mStream = nsnull;
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
mInStream = nsnull;
|
|
|
|
mOutStream = nsnull;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
mInputConsumer = nsnull;
|
|
|
|
mOutputObserver = nsnull;
|
|
|
|
|
|
|
|
// Create the monitor used for synchronization...
|
|
|
|
mMonitor = PR_NewMonitor();
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
// Create a socket transport...
|
|
|
|
NS_WITH_SERVICE(nsISocketTransportService, sts, kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = sts->CreateTransport(aHostName, aPort, &mTransport);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-03 09:45:23 +04:00
|
|
|
if (mIsAsync) {
|
|
|
|
// Create the input and output stream observers...
|
|
|
|
mInputConsumer = new InputConsumer(this);
|
|
|
|
mOutputObserver = new OutputObserver(this);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
NS_IF_ADDREF(mInputConsumer);
|
|
|
|
NS_IF_ADDREF(mOutputObserver);
|
|
|
|
|
|
|
|
// Create a stream for the data being written to the server...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-09 00:57:32 +04:00
|
|
|
nsIBuffer* buf;
|
1999-06-15 09:18:40 +04:00
|
|
|
rv = NS_NewBuffer(&buf, 1024, 4096, nsnull);
|
1999-06-09 00:57:32 +04:00
|
|
|
rv = NS_NewBufferInputStream(&mStream, buf);
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Synchronous transport...
|
|
|
|
else {
|
1999-06-11 05:37:43 +04:00
|
|
|
rv = mTransport->OpenInputStream(0, -1, &mInStream);
|
|
|
|
rv = mTransport->OpenOutputStream(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(mInputConsumer);
|
|
|
|
NS_IF_RELEASE(mOutputObserver);
|
|
|
|
NS_IF_RELEASE(mStream);
|
|
|
|
|
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
|
|
|
if (mMonitor) {
|
|
|
|
PR_DestroyMonitor(mMonitor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(TestConnection,nsIRunnable::GetIID());
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
//
|
|
|
|
// Make sure that all resources were allocated in the constructor...
|
|
|
|
//
|
|
|
|
if (!mTransport) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
if (mIsAsync && (!mInputConsumer || !mOutputObserver)) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (mIsAsync) {
|
|
|
|
//
|
|
|
|
// Initiate an async read...
|
|
|
|
//
|
1999-06-08 01:33:30 +04:00
|
|
|
rv = mTransport->AsyncRead(0, -1, mTransport, gEventQ, mInputConsumer);
|
1999-06-03 09:45:23 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
printf("Error: AsyncRead failed...");
|
|
|
|
}
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
Lock();
|
1999-06-03 09:45:23 +04:00
|
|
|
while (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = WriteBuffer();
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = ReadBuffer();
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
if (mBufferChar == 'z') {
|
|
|
|
mBufferChar = 'a';
|
|
|
|
} else {
|
|
|
|
mBufferChar++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Unlock();
|
|
|
|
}
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
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-06-11 12:36:40 +04:00
|
|
|
printf("\n+++ Request is: %c. Context = %p\n", mBufferChar, mTransport);
|
|
|
|
|
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) {
|
|
|
|
rv = mStream->Fill(buffer, size, &bytesWritten);
|
|
|
|
|
|
|
|
// Write the buffer to the server...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
1999-06-19 13:42:59 +04:00
|
|
|
rv = mTransport->AsyncWrite(mStream, 0, bytesWritten, mTransport, gEventQ, /* mOutputObserver */ nsnull);
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
|
|
|
// Wait for the write to complete...
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
Wait();
|
|
|
|
} else {
|
|
|
|
printf("Error: AsyncWrite failed...");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Synchronous case...
|
|
|
|
//
|
|
|
|
else if (mOutStream) {
|
|
|
|
rv = mOutStream->Write(buffer, size, &bytesWritten);
|
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) {
|
|
|
|
rv = mInStream->Read(buffer, mBufferLength+2, &bytesRead);
|
|
|
|
|
|
|
|
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-05-31 09:18:19 +04:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Parse the command line args...
|
|
|
|
//
|
|
|
|
// -----
|
|
|
|
/// if (argc < 3) {
|
|
|
|
/// printf("usage: %s <host> <path>\n", argv[0]);
|
|
|
|
/// return -1;
|
|
|
|
/// }
|
|
|
|
|
|
|
|
char* hostName = argv[1];
|
|
|
|
char* fileName = argv[2];
|
|
|
|
int port = 80;
|
|
|
|
|
|
|
|
// -----
|
|
|
|
//
|
|
|
|
// Initialize XPCom...
|
|
|
|
//
|
|
|
|
// -----
|
|
|
|
// XXX why do I have to do this?!
|
|
|
|
rv = nsComponentManager::AutoRegister(nsIComponentManager::NS_Startup,
|
|
|
|
"components");
|
|
|
|
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;
|
|
|
|
|
|
|
|
eventQService->GetThreadEventQueue(PR_CurrentThread(), &gEventQ);
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
1999-06-03 09:45:23 +04:00
|
|
|
//
|
|
|
|
// Create the connections and threads...
|
|
|
|
//
|
|
|
|
for (i=0; i<NUM_TEST_THREADS; i++) {
|
|
|
|
gConnections[i] = new TestConnection("chainsaw", 7, PR_TRUE);
|
|
|
|
rv = NS_NewThread(&gThreads[i], gConnections[i]);
|
1999-05-31 09:18:19 +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.
|
|
|
|
while ( gKeepRunning ) {
|
|
|
|
#ifdef WIN32
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
if (GetMessage(&msg, NULL, 0, 0)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
} else {
|
|
|
|
gKeepRunning = FALSE;
|
|
|
|
}
|
1999-06-04 13:03:45 +04:00
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
/* Mac stuff is missing here! */
|
|
|
|
#else
|
|
|
|
PLEvent *gEvent;
|
|
|
|
rv = gEventQ->GetEvent(&gEvent);
|
1999-06-08 16:24:10 +04:00
|
|
|
rv = gEventQ->HandleEvent(gEvent);
|
1999-06-04 13:03:45 +04:00
|
|
|
#endif /* XP_UNIX */
|
|
|
|
#endif /* !WIN32 */
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PRTime endTime;
|
|
|
|
endTime = PR_Now();
|
|
|
|
printf("Elapsed time: %ld\n", (PRInt32)(endTime/1000UL-gElapsedTime/1000UL));
|
|
|
|
|
|
|
|
NS_RELEASE(eventQService);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|