2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-08-10 23:18:39 +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-08-10 23:18:39 +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-08-10 23:18:39 +04:00
|
|
|
*
|
1999-11-06 06:43:54 +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:43:54 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Contributor(s):
|
1999-12-01 02:36:32 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* 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-08-10 23:18:39 +04:00
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
#include "nsIPipe.h"
|
2000-08-22 11:03:33 +04:00
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
1999-08-10 23:18:39 +04:00
|
|
|
#include "nsSegmentedBuffer.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "nsIServiceManager.h"
|
1999-09-26 11:26:21 +04:00
|
|
|
#ifdef PAGE_MANAGER
|
1999-08-10 23:18:39 +04:00
|
|
|
#include "nsIPageManager.h"
|
1999-09-26 11:26:21 +04:00
|
|
|
#endif
|
1999-12-22 07:11:13 +03:00
|
|
|
#include "nsCRT.h"
|
2000-01-05 08:07:46 +03:00
|
|
|
#include "nsCOMPtr.h"
|
1999-08-10 23:18:39 +04:00
|
|
|
|
2000-08-02 01:33:36 +04:00
|
|
|
// define to include temporary code which prevents a common startup crash
|
|
|
|
// (Bugzilla bug 46267) without addressing the unerlying issue.
|
|
|
|
#define MASK_PREMATURE_STREAM_RELEASE
|
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
#define GET_INPUTSTREAM_PIPE(_this) \
|
|
|
|
((nsPipe*)((char*)(_this) - offsetof(nsPipe, mInput)))
|
|
|
|
|
|
|
|
#define GET_OUTPUTSTREAM_PIPE(_this) \
|
|
|
|
((nsPipe*)((char*)(_this) - offsetof(nsPipe, mOutput)))
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
class nsPipe : public nsIPipe
|
1999-08-10 23:18:39 +04:00
|
|
|
{
|
|
|
|
public:
|
2000-08-22 11:03:33 +04:00
|
|
|
// We can't inherit from both nsIInputStream and nsIOutputStream
|
1999-08-10 23:18:39 +04:00
|
|
|
// because they collide on their Close method. Consequently we nest their
|
|
|
|
// implementations to avoid the extra object allocation, and speed up method
|
|
|
|
// invocation between them and the nsPipe's buffer manipulation methods.
|
|
|
|
|
2001-04-08 01:29:11 +04:00
|
|
|
class nsPipeInputStream : public nsIInputStream, public nsISearchableInputStream {
|
1999-08-10 23:18:39 +04:00
|
|
|
public:
|
|
|
|
NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr);
|
|
|
|
NS_IMETHOD_(nsrefcnt) AddRef(void);
|
|
|
|
NS_IMETHOD_(nsrefcnt) Release(void);
|
|
|
|
// nsIBaseStream methods:
|
|
|
|
NS_IMETHOD Close(void);
|
|
|
|
// nsIInputStream methods:
|
1999-09-10 02:05:05 +04:00
|
|
|
NS_IMETHOD Available(PRUint32 *result);
|
1999-08-10 23:18:39 +04:00
|
|
|
NS_IMETHOD Read(char* toBuf, PRUint32 bufLen, PRUint32 *readCount);
|
2000-08-22 11:03:33 +04:00
|
|
|
// nsIInputStream methods:
|
1999-08-24 12:45:17 +04:00
|
|
|
NS_IMETHOD ReadSegments(nsWriteSegmentFun writer, void* closure, PRUint32 count,
|
|
|
|
PRUint32 *readCount);
|
1999-08-10 23:18:39 +04:00
|
|
|
NS_IMETHOD Search(const char *forString, PRBool ignoreCase, PRBool *found,
|
|
|
|
PRUint32 *offsetSearchedTo);
|
|
|
|
NS_IMETHOD GetNonBlocking(PRBool *aNonBlocking);
|
|
|
|
NS_IMETHOD SetNonBlocking(PRBool aNonBlocking);
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_IMETHOD GetObserver(nsIInputStreamObserver* *result) {
|
|
|
|
*result = mObserver;
|
|
|
|
NS_IF_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD SetObserver(nsIInputStreamObserver* obs) {
|
|
|
|
mObserver = obs;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
nsPipeInputStream() : mReaderRefCnt(0), mBlocking(PR_TRUE) {}
|
|
|
|
nsresult Fill();
|
2000-08-22 11:03:33 +04:00
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
protected:
|
|
|
|
nsrefcnt mReaderRefCnt; // separate refcnt so that we know when to close the consumer
|
|
|
|
PRBool mBlocking;
|
2000-08-22 11:03:33 +04:00
|
|
|
nsCOMPtr<nsIInputStreamObserver> mObserver;
|
1999-08-10 23:18:39 +04:00
|
|
|
};
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
class nsPipeOutputStream : public nsIOutputStream {
|
1999-08-10 23:18:39 +04:00
|
|
|
public:
|
|
|
|
NS_IMETHOD QueryInterface(const nsIID& aIID, void** aInstancePtr);
|
|
|
|
NS_IMETHOD_(nsrefcnt) AddRef(void);
|
|
|
|
NS_IMETHOD_(nsrefcnt) Release(void);
|
|
|
|
// nsIBaseStream methods:
|
|
|
|
NS_IMETHOD Close(void);
|
|
|
|
NS_IMETHOD Write(const char* fromBuf, PRUint32 bufLen, PRUint32 *writeCount);
|
|
|
|
NS_IMETHOD Flush(void);
|
2000-08-22 11:03:33 +04:00
|
|
|
// nsIOutputStream methods:
|
1999-08-24 12:45:17 +04:00
|
|
|
NS_IMETHOD WriteSegments(nsReadSegmentFun reader, void* closure, PRUint32 count,
|
|
|
|
PRUint32 *writeCount);
|
1999-08-10 23:18:39 +04:00
|
|
|
NS_IMETHOD WriteFrom(nsIInputStream* fromStream, PRUint32 count, PRUint32 *writeCount);
|
|
|
|
NS_IMETHOD GetNonBlocking(PRBool *aNonBlocking);
|
|
|
|
NS_IMETHOD SetNonBlocking(PRBool aNonBlocking);
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_IMETHOD GetObserver(nsIOutputStreamObserver* *result) {
|
|
|
|
*result = mObserver;
|
|
|
|
NS_IF_ADDREF(*result);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD SetObserver(nsIOutputStreamObserver* obs) {
|
|
|
|
mObserver = obs;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
nsPipeOutputStream() : mWriterRefCnt(0), mBlocking(PR_TRUE) {}
|
2000-08-22 11:03:33 +04:00
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
protected:
|
|
|
|
nsrefcnt mWriterRefCnt; // separate refcnt so that we know when to close the producer
|
|
|
|
PRBool mBlocking;
|
2000-08-22 11:03:33 +04:00
|
|
|
nsCOMPtr<nsIOutputStreamObserver> mObserver;
|
1999-08-10 23:18:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
friend class nsPipeInputStream;
|
|
|
|
friend class nsPipeOutputStream;
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
// nsIPipe methods:
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_IMETHOD Initialize(PRUint32 segmentSize, PRUint32 maxSize,
|
|
|
|
PRBool nonBlockingIn, PRBool nonBlockingOut,
|
|
|
|
nsIMemory *segmentAllocator) {
|
1999-08-24 12:45:17 +04:00
|
|
|
nsresult rv;
|
2000-08-22 11:03:33 +04:00
|
|
|
mMonitor = PR_NewMonitor();
|
|
|
|
if (mMonitor == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
1999-08-24 12:45:17 +04:00
|
|
|
rv = mBuffer.Init(segmentSize, maxSize, segmentAllocator);
|
2000-08-22 11:03:33 +04:00
|
|
|
GetInputStream()->SetNonBlocking(nonBlockingIn);
|
|
|
|
GetOutputStream()->SetNonBlocking(nonBlockingOut);
|
1999-08-24 12:45:17 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_IMETHOD GetInputStream(nsIInputStream * *aInputStream) {
|
1999-08-24 12:45:17 +04:00
|
|
|
*aInputStream = &mInput;
|
|
|
|
NS_IF_ADDREF(*aInputStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_IMETHOD GetOutputStream(nsIOutputStream * *aOutputStream) {
|
1999-08-24 12:45:17 +04:00
|
|
|
*aOutputStream = &mOutput;
|
|
|
|
NS_IF_ADDREF(*aOutputStream);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
// nsPipe methods:
|
1999-08-24 12:45:17 +04:00
|
|
|
nsPipe();
|
1999-08-10 23:18:39 +04:00
|
|
|
virtual ~nsPipe();
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
PRMonitor* GetMonitor() { return mMonitor; }
|
1999-08-10 23:18:39 +04:00
|
|
|
nsPipeInputStream* GetInputStream() { return &mInput; }
|
|
|
|
nsPipeOutputStream* GetOutputStream() { return &mOutput; }
|
|
|
|
|
|
|
|
nsresult GetReadSegment(PRUint32 segmentLogicalOffset,
|
|
|
|
const char* *resultSegment,
|
|
|
|
PRUint32 *resultSegmentLen);
|
|
|
|
nsresult GetWriteSegment(char* *resultSegment,
|
|
|
|
PRUint32 *resultSegmentLen);
|
|
|
|
|
|
|
|
protected:
|
2000-08-22 11:03:33 +04:00
|
|
|
PRMonitor* mMonitor;
|
1999-08-10 23:18:39 +04:00
|
|
|
nsPipeInputStream mInput;
|
|
|
|
nsPipeOutputStream mOutput;
|
|
|
|
|
|
|
|
nsSegmentedBuffer mBuffer;
|
|
|
|
|
|
|
|
char* mReadCursor;
|
|
|
|
char* mReadLimit;
|
|
|
|
|
|
|
|
char* mWriteCursor;
|
|
|
|
char* mWriteLimit;
|
|
|
|
|
|
|
|
nsresult mCondition;
|
|
|
|
};
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsPipe methods:
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
nsPipe::nsPipe()
|
2000-08-22 11:03:33 +04:00
|
|
|
: mMonitor(nsnull),
|
|
|
|
mReadCursor(nsnull),
|
1999-08-10 23:18:39 +04:00
|
|
|
mReadLimit(nsnull),
|
|
|
|
mWriteCursor(nsnull),
|
|
|
|
mWriteLimit(nsnull),
|
|
|
|
mCondition(NS_OK)
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPipe::~nsPipe()
|
|
|
|
{
|
2000-08-22 11:03:33 +04:00
|
|
|
if (mMonitor) PR_DestroyMonitor(mMonitor);
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsPipe);
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsPipe);
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::QueryInterface(const nsIID& aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
if (aInstancePtr == nsnull)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2000-08-22 11:03:33 +04:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIInputStream))) {
|
|
|
|
nsIInputStream* in = GetInputStream();
|
1999-08-26 04:25:20 +04:00
|
|
|
NS_ADDREF(in);
|
|
|
|
*aInstancePtr = in;
|
1999-08-10 23:18:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-08-22 11:03:33 +04:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIOutputStream))) {
|
|
|
|
nsIOutputStream* out = GetOutputStream();
|
1999-08-26 04:25:20 +04:00
|
|
|
NS_ADDREF(out);
|
|
|
|
*aInstancePtr = out;
|
1999-08-10 23:18:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-12-01 02:36:32 +03:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsIPipe)) ||
|
|
|
|
aIID.Equals(NS_GET_IID(nsISupports))) {
|
1999-08-10 23:18:39 +04:00
|
|
|
*aInstancePtr = this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPipe::GetReadSegment(PRUint32 segmentLogicalOffset,
|
|
|
|
const char* *resultSegment,
|
|
|
|
PRUint32 *resultSegmentLen)
|
|
|
|
{
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(mMonitor);
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
PRInt32 offset = (PRInt32)segmentLogicalOffset;
|
|
|
|
PRInt32 segCount = mBuffer.GetSegmentCount();
|
|
|
|
for (PRInt32 i = 0; i < segCount; i++) {
|
|
|
|
char* segStart = mBuffer.GetSegment(i);
|
|
|
|
char* segEnd = segStart + mBuffer.GetSegmentSize();
|
|
|
|
if (mReadCursor == nsnull) {
|
|
|
|
mReadCursor = segStart;
|
|
|
|
mReadLimit = segEnd;
|
|
|
|
}
|
|
|
|
else if (segStart <= mReadCursor && mReadCursor < segEnd) {
|
|
|
|
segStart = mReadCursor;
|
|
|
|
NS_ASSERTION(i == 0, "read cursor not in first segment");
|
|
|
|
}
|
|
|
|
if (segStart <= mWriteCursor && mWriteCursor < segEnd) {
|
|
|
|
segEnd = mWriteCursor;
|
|
|
|
NS_ASSERTION(i == segCount - 1, "write cursor not in last segment");
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 amt = segEnd - segStart;
|
|
|
|
if (offset < amt) {
|
|
|
|
// segmentLogicalOffset is in this segment, so read up to its end
|
|
|
|
*resultSegmentLen = amt - offset;
|
|
|
|
*resultSegment = segStart + offset;
|
|
|
|
return *resultSegmentLen == 0 ? mCondition : NS_OK;
|
|
|
|
}
|
|
|
|
offset -= amt;
|
|
|
|
}
|
|
|
|
*resultSegmentLen = 0;
|
|
|
|
*resultSegment = nsnull;
|
|
|
|
return *resultSegmentLen == 0 ? mCondition : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPipe::GetWriteSegment(char* *resultSegment,
|
|
|
|
PRUint32 *resultSegmentLen)
|
|
|
|
{
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(mMonitor);
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
*resultSegment = nsnull;
|
|
|
|
*resultSegmentLen = 0;
|
|
|
|
if (mWriteCursor == nsnull ||
|
|
|
|
mWriteCursor == mWriteLimit) {
|
|
|
|
char* seg = mBuffer.AppendNewSegment();
|
|
|
|
if (seg == nsnull) {
|
|
|
|
// buffer is full
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
mWriteCursor = seg;
|
|
|
|
mWriteLimit = seg + mBuffer.GetSegmentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
*resultSegment = mWriteCursor;
|
|
|
|
*resultSegmentLen = mWriteLimit - mWriteCursor;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsPipeInputStream methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeInputStream::QueryInterface(const nsIID& aIID, void** aInstancePtr)
|
|
|
|
{
|
2001-04-08 01:29:11 +04:00
|
|
|
if (aIID.Equals(NS_GET_IID(nsISearchableInputStream)))
|
|
|
|
{
|
|
|
|
nsISearchableInputStream* in = NS_STATIC_CAST(nsISearchableInputStream *, this);
|
|
|
|
NS_ADDREF(in);
|
|
|
|
*aInstancePtr = in;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
else
|
1999-08-10 23:18:39 +04:00
|
|
|
return GET_INPUTSTREAM_PIPE(this)->QueryInterface(aIID, aInstancePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsPipe::nsPipeInputStream::AddRef(void)
|
|
|
|
{
|
|
|
|
mReaderRefCnt++;
|
|
|
|
return GET_INPUTSTREAM_PIPE(this)->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsPipe::nsPipeInputStream::Release(void)
|
|
|
|
{
|
|
|
|
if (--mReaderRefCnt == 0)
|
|
|
|
Close();
|
|
|
|
return GET_INPUTSTREAM_PIPE(this)->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeInputStream::Close(void)
|
|
|
|
{
|
|
|
|
nsPipe* pipe = GET_INPUTSTREAM_PIPE(this);
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(pipe->GetMonitor());
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
pipe->mCondition = NS_BASE_STREAM_CLOSED;
|
|
|
|
pipe->mBuffer.Empty();
|
|
|
|
pipe->mWriteCursor = nsnull;
|
|
|
|
pipe->mWriteLimit = nsnull;
|
2000-08-22 11:03:33 +04:00
|
|
|
if (mObserver) {
|
|
|
|
nsCOMPtr<nsIInputStreamObserver> obs = mObserver;
|
2000-01-05 08:07:46 +03:00
|
|
|
mon.Exit(); // XXXbe avoid deadlock better
|
2000-08-22 11:03:33 +04:00
|
|
|
nsresult rv = obs->OnClose(this);
|
2000-01-05 08:07:46 +03:00
|
|
|
mon.Enter();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "OnClose failed");
|
|
|
|
// don't return error from OnClose -- its not our problem
|
2000-08-22 11:03:33 +04:00
|
|
|
mObserver = nsnull; // so we don't call OnClose again
|
2000-01-05 08:07:46 +03:00
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-09-10 02:05:05 +04:00
|
|
|
nsPipe::nsPipeInputStream::Available(PRUint32 *result)
|
1999-08-10 23:18:39 +04:00
|
|
|
{
|
|
|
|
nsPipe* pipe = GET_INPUTSTREAM_PIPE(this);
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(pipe->GetMonitor());
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
PRUint32 len = pipe->mBuffer.GetSize();
|
1999-09-01 01:47:30 +04:00
|
|
|
if (pipe->mReadCursor)
|
|
|
|
len -= pipe->mBuffer.GetSegmentSize() - (pipe->mReadLimit - pipe->mReadCursor);
|
|
|
|
if (pipe->mWriteCursor)
|
|
|
|
len -= pipe->mWriteLimit - pipe->mWriteCursor;
|
1999-08-27 23:14:57 +04:00
|
|
|
|
1999-09-01 01:47:30 +04:00
|
|
|
*result = len;
|
1999-08-27 23:14:57 +04:00
|
|
|
return NS_OK;
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeInputStream::ReadSegments(nsWriteSegmentFun writer,
|
1999-09-01 01:47:30 +04:00
|
|
|
void* closure,
|
1999-08-10 23:18:39 +04:00
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *readCount)
|
|
|
|
{
|
|
|
|
nsPipe* pipe = GET_INPUTSTREAM_PIPE(this);
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(pipe->GetMonitor());
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRUint32 readBufferLen;
|
|
|
|
const char* readBuffer;
|
|
|
|
|
|
|
|
*readCount = 0;
|
1999-11-02 08:38:22 +03:00
|
|
|
PRUint32 amt = count;
|
|
|
|
while (amt > 0) {
|
1999-08-10 23:18:39 +04:00
|
|
|
rv = pipe->GetReadSegment(0, &readBuffer, &readBufferLen);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
goto done;
|
|
|
|
if (readBufferLen == 0) {
|
|
|
|
rv = pipe->mCondition;
|
|
|
|
if (*readCount > 0 || NS_FAILED(rv))
|
|
|
|
goto done; // don't Fill if we've got something
|
2000-08-22 11:03:33 +04:00
|
|
|
if (mObserver) {
|
1999-09-06 23:57:40 +04:00
|
|
|
mon.Exit(); // XXXbe avoid deadlock better
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = mObserver->OnEmpty(this);
|
1999-09-06 23:57:40 +04:00
|
|
|
mon.Enter();
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) goto done;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
rv = Fill();
|
1999-09-01 01:47:30 +04:00
|
|
|
if (/*rv == NS_BASE_STREAM_WOULD_BLOCK || */NS_FAILED(rv))
|
1999-08-10 23:18:39 +04:00
|
|
|
goto done;
|
|
|
|
// else we filled the pipe, so go around again
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-11-02 08:38:22 +03:00
|
|
|
readBufferLen = PR_MIN(readBufferLen, amt);
|
1999-08-10 23:18:39 +04:00
|
|
|
while (readBufferLen > 0) {
|
2000-08-22 11:03:33 +04:00
|
|
|
PRUint32 writeCount = 0;
|
2001-02-13 02:33:11 +03:00
|
|
|
mon.Exit(); // XXX avoid deadlock better
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = writer(this, closure, readBuffer, *readCount, readBufferLen, &writeCount);
|
2001-02-13 02:33:11 +03:00
|
|
|
mon.Enter();
|
1999-12-02 03:58:43 +03:00
|
|
|
if (NS_FAILED(rv) && rv != NS_BASE_STREAM_WOULD_BLOCK)
|
|
|
|
goto done;
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_ASSERTION(writeCount <= readBufferLen, "writer returned bad writeCount");
|
2001-01-24 01:10:34 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (writeCount > 0 && rv == NS_BASE_STREAM_WOULD_BLOCK)
|
|
|
|
NS_WARNING("Invalid writer implementation: cannot write data and return WOULD_BLOCK");
|
|
|
|
#endif
|
2000-08-22 11:03:33 +04:00
|
|
|
readBuffer += writeCount;
|
|
|
|
readBufferLen -= writeCount;
|
|
|
|
*readCount += writeCount;
|
|
|
|
amt -= writeCount;
|
|
|
|
pipe->mReadCursor += writeCount;
|
1999-08-24 12:45:17 +04:00
|
|
|
if (writeCount == 0 || rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
1999-08-10 23:18:39 +04:00
|
|
|
rv = pipe->mCondition;
|
|
|
|
if (*readCount > 0 || NS_FAILED(rv))
|
|
|
|
goto done; // don't Fill if we've got something
|
|
|
|
rv = Fill();
|
1999-09-01 01:47:30 +04:00
|
|
|
if (/*rv == NS_BASE_STREAM_WOULD_BLOCK || */NS_FAILED(rv))
|
1999-08-10 23:18:39 +04:00
|
|
|
goto done;
|
|
|
|
// else we filled the pipe, so go around again
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pipe->mReadCursor == pipe->mReadLimit) {
|
|
|
|
pipe->mReadCursor = nsnull;
|
|
|
|
pipe->mReadLimit = nsnull;
|
|
|
|
PRBool empty = pipe->mBuffer.DeleteFirstSegment();
|
2000-08-22 11:03:33 +04:00
|
|
|
if (empty && mObserver) {
|
1999-09-06 23:57:40 +04:00
|
|
|
mon.Exit(); // XXXbe avoid deadlock better
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = mObserver->OnEmpty(this);
|
1999-09-06 23:57:40 +04:00
|
|
|
mon.Enter();
|
1999-10-08 06:03:34 +04:00
|
|
|
mon.Notify(); // wake up writer
|
1999-08-10 23:18:39 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
2001-01-24 01:10:34 +03:00
|
|
|
else if (pipe->mReadCursor == pipe->mWriteCursor) {
|
|
|
|
if (mObserver) {
|
|
|
|
mon.Exit(); // XXXbe avoid deadlock better
|
|
|
|
rv = mObserver->OnEmpty(this);
|
|
|
|
mon.Enter();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
|
|
|
done:
|
1999-10-08 06:03:34 +04:00
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
if (mBlocking && rv == NS_BASE_STREAM_WOULD_BLOCK && *readCount > 0) {
|
|
|
|
mon.Notify(); // wake up writer
|
|
|
|
}
|
1999-10-08 06:03:34 +04:00
|
|
|
|
2000-01-05 08:07:46 +03:00
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) { // EOF
|
2000-08-22 11:03:33 +04:00
|
|
|
if (mObserver) {
|
|
|
|
nsCOMPtr<nsIInputStreamObserver> obs = mObserver;
|
2000-01-05 08:07:46 +03:00
|
|
|
mon.Exit(); // XXXbe avoid deadlock better
|
2000-08-22 11:03:33 +04:00
|
|
|
nsresult rv2 = obs->OnClose(this);
|
2000-01-05 08:07:46 +03:00
|
|
|
mon.Enter();
|
2000-01-12 03:54:10 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv2), "OnClose failed");
|
2000-01-05 08:07:46 +03:00
|
|
|
// don't return error from OnClose -- its not our problem
|
2000-08-22 11:03:33 +04:00
|
|
|
mObserver = nsnull; // so we don't call OnClose again
|
2000-01-05 08:07:46 +03:00
|
|
|
}
|
1999-09-10 02:05:05 +04:00
|
|
|
rv = NS_OK;
|
2000-01-05 08:07:46 +03:00
|
|
|
}
|
1999-11-02 08:38:22 +03:00
|
|
|
NS_ASSERTION(*readCount <= count, "read more than expected");
|
1999-08-10 23:18:39 +04:00
|
|
|
return *readCount == 0 ? rv : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPipe::nsPipeInputStream::Fill()
|
|
|
|
{
|
|
|
|
nsPipe* pipe = GET_INPUTSTREAM_PIPE(this);
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(pipe->GetMonitor());
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
while (PR_TRUE) {
|
|
|
|
// check read buffer again while in the monitor
|
|
|
|
PRUint32 amt;
|
|
|
|
const char* buf;
|
|
|
|
rv = pipe->GetReadSegment(0, &buf, &amt);
|
|
|
|
if (NS_FAILED(rv) || amt > 0) return rv;
|
|
|
|
|
|
|
|
// else notify the writer and wait
|
|
|
|
rv = mon.Notify();
|
|
|
|
if (NS_FAILED(rv)) return rv; // interrupted
|
|
|
|
if (mBlocking) {
|
|
|
|
rv = mon.Wait();
|
|
|
|
if (NS_FAILED(rv)) return rv; // interrupted
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
}
|
|
|
|
// loop again so that we end up exiting on EOF with
|
|
|
|
// the right error
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NS_METHOD
|
2000-08-22 11:03:33 +04:00
|
|
|
nsWriteToRawBuffer(nsIInputStream* inStr,
|
|
|
|
void* closure,
|
1999-08-10 23:18:39 +04:00
|
|
|
const char* fromRawSegment,
|
|
|
|
PRUint32 offset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *writeCount)
|
|
|
|
{
|
|
|
|
char* toBuf = (char*)closure;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(&toBuf[offset], fromRawSegment, count);
|
1999-08-10 23:18:39 +04:00
|
|
|
*writeCount = count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeInputStream::Read(char* toBuf, PRUint32 bufLen, PRUint32 *readCount)
|
|
|
|
{
|
|
|
|
return ReadSegments(nsWriteToRawBuffer, toBuf, bufLen, readCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define COMPARE(s1, s2, i) \
|
|
|
|
(ignoreCase \
|
|
|
|
? nsCRT::strncasecmp((const char *)s1, (const char *)s2, (PRUint32)i) \
|
|
|
|
: nsCRT::strncmp((const char *)s1, (const char *)s2, (PRUint32)i))
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeInputStream::Search(const char *forString,
|
|
|
|
PRBool ignoreCase,
|
|
|
|
PRBool *found,
|
|
|
|
PRUint32 *offsetSearchedTo)
|
|
|
|
{
|
|
|
|
nsPipe* pipe = GET_INPUTSTREAM_PIPE(this);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
const char* bufSeg1;
|
|
|
|
PRUint32 bufSegLen1;
|
|
|
|
PRUint32 segmentPos = 0;
|
|
|
|
PRUint32 strLen = nsCRT::strlen(forString);
|
|
|
|
|
|
|
|
rv = pipe->GetReadSegment(segmentPos, &bufSeg1, &bufSegLen1);
|
|
|
|
if (NS_FAILED(rv) || bufSegLen1 == 0) {
|
|
|
|
*found = PR_FALSE;
|
|
|
|
*offsetSearchedTo = segmentPos;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (PR_TRUE) {
|
|
|
|
PRUint32 i;
|
|
|
|
// check if the string is in the buffer segment
|
|
|
|
for (i = 0; i < bufSegLen1 - strLen + 1; i++) {
|
|
|
|
if (COMPARE(&bufSeg1[i], forString, strLen) == 0) {
|
|
|
|
*found = PR_TRUE;
|
|
|
|
*offsetSearchedTo = segmentPos + i;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the next segment
|
|
|
|
const char* bufSeg2;
|
|
|
|
PRUint32 bufSegLen2;
|
|
|
|
segmentPos += bufSegLen1;
|
|
|
|
rv = pipe->GetReadSegment(segmentPos, &bufSeg2, &bufSegLen2);
|
|
|
|
if (NS_FAILED(rv) || bufSegLen2 == 0) {
|
|
|
|
*found = PR_FALSE;
|
1999-09-10 02:05:05 +04:00
|
|
|
if (NS_FAILED(pipe->mCondition))
|
1999-08-27 14:32:45 +04:00
|
|
|
*offsetSearchedTo = segmentPos;
|
1999-08-10 23:18:39 +04:00
|
|
|
else
|
1999-08-27 14:32:45 +04:00
|
|
|
*offsetSearchedTo = segmentPos - strLen + 1;
|
1999-08-10 23:18:39 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if the string is straddling the next buffer segment
|
|
|
|
PRUint32 limit = PR_MIN(strLen, bufSegLen2 + 1);
|
|
|
|
for (i = 0; i < limit; i++) {
|
|
|
|
PRUint32 strPart1Len = strLen - i - 1;
|
|
|
|
PRUint32 strPart2Len = strLen - strPart1Len;
|
|
|
|
const char* strPart2 = &forString[strLen - strPart2Len];
|
|
|
|
PRUint32 bufSeg1Offset = bufSegLen1 - strPart1Len;
|
|
|
|
if (COMPARE(&bufSeg1[bufSeg1Offset], forString, strPart1Len) == 0 &&
|
|
|
|
COMPARE(bufSeg2, strPart2, strPart2Len) == 0) {
|
|
|
|
*found = PR_TRUE;
|
|
|
|
*offsetSearchedTo = segmentPos - strPart1Len;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// finally continue with the next buffer
|
|
|
|
bufSeg1 = bufSeg2;
|
|
|
|
bufSegLen1 = bufSegLen2;
|
|
|
|
}
|
|
|
|
NS_NOTREACHED("can't get here");
|
|
|
|
return NS_ERROR_FAILURE; // keep compiler happy
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeInputStream::GetNonBlocking(PRBool *aNonBlocking)
|
|
|
|
{
|
|
|
|
*aNonBlocking = !mBlocking;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeInputStream::SetNonBlocking(PRBool aNonBlocking)
|
|
|
|
{
|
|
|
|
mBlocking = !aNonBlocking;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// nsPipeOutputStream methods:
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::QueryInterface(const nsIID& aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
return GET_OUTPUTSTREAM_PIPE(this)->QueryInterface(aIID, aInstancePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsPipe::nsPipeOutputStream::AddRef(void)
|
|
|
|
{
|
|
|
|
mWriterRefCnt++;
|
|
|
|
return GET_OUTPUTSTREAM_PIPE(this)->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(nsrefcnt)
|
|
|
|
nsPipe::nsPipeOutputStream::Release(void)
|
|
|
|
{
|
|
|
|
if (--mWriterRefCnt == 0)
|
|
|
|
Close();
|
|
|
|
return GET_OUTPUTSTREAM_PIPE(this)->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::Close(void)
|
|
|
|
{
|
|
|
|
nsPipe* pipe = GET_OUTPUTSTREAM_PIPE(this);
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(pipe->GetMonitor());
|
2000-03-29 07:58:50 +04:00
|
|
|
if (pipe->mCondition == NS_OK)
|
|
|
|
pipe->mCondition = NS_BASE_STREAM_CLOSED;
|
1999-08-10 23:18:39 +04:00
|
|
|
nsresult rv = mon.Notify(); // wake up the writer
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::WriteSegments(nsReadSegmentFun reader,
|
|
|
|
void* closure,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *writeCount)
|
|
|
|
{
|
1999-09-14 13:33:53 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-08-02 01:33:36 +04:00
|
|
|
#ifdef MASK_PREMATURE_STREAM_RELEASE
|
2000-08-22 11:03:33 +04:00
|
|
|
nsCOMPtr<nsIOutputStream> kungFuDeathGrip(this);
|
2000-08-02 01:33:36 +04:00
|
|
|
#endif
|
1999-08-10 23:18:39 +04:00
|
|
|
nsPipe* pipe = GET_OUTPUTSTREAM_PIPE(this);
|
1999-11-02 08:38:22 +03:00
|
|
|
PRUint32 amt = count;
|
1999-08-10 23:18:39 +04:00
|
|
|
{
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(pipe->GetMonitor());
|
1999-08-10 23:18:39 +04:00
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
*writeCount = 0;
|
2000-02-05 00:45:46 +03:00
|
|
|
if (NS_FAILED(pipe->mCondition)
|
|
|
|
&& pipe->mCondition != NS_BASE_STREAM_CLOSED) {
|
1999-08-10 23:18:39 +04:00
|
|
|
rv = pipe->mCondition;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
1999-11-02 08:38:22 +03:00
|
|
|
while (amt > 0) {
|
1999-08-10 23:18:39 +04:00
|
|
|
PRUint32 writeBufLen;
|
|
|
|
char* writeBuf;
|
|
|
|
rv = pipe->GetWriteSegment(&writeBuf, &writeBufLen);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
goto done;
|
|
|
|
if (writeBufLen == 0) {
|
1999-11-13 09:16:52 +03:00
|
|
|
if (*writeCount > 0 && !mBlocking)
|
1999-10-08 06:03:34 +04:00
|
|
|
goto done;
|
2000-08-22 11:03:33 +04:00
|
|
|
if (mObserver/* && *writeCount == 0*/) {
|
1999-09-06 23:57:40 +04:00
|
|
|
mon.Exit(); // XXXbe avoid deadlock better
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = mObserver->OnFull(this);
|
1999-09-06 23:57:40 +04:00
|
|
|
mon.Enter();
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) goto done;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
rv = Flush();
|
1999-09-01 01:47:30 +04:00
|
|
|
if (/*rv == NS_BASE_STREAM_WOULD_BLOCK || */NS_FAILED(rv))
|
1999-08-10 23:18:39 +04:00
|
|
|
goto done;
|
|
|
|
// else we flushed, so go around again
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-11-02 08:38:22 +03:00
|
|
|
writeBufLen = PR_MIN(writeBufLen, amt);
|
1999-08-10 23:18:39 +04:00
|
|
|
while (writeBufLen > 0) {
|
|
|
|
PRUint32 readCount = 0;
|
2001-02-13 02:33:11 +03:00
|
|
|
mon.Exit(); // XXX avoid deadlock better
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = reader(this, closure, writeBuf, *writeCount, writeBufLen, &readCount);
|
2001-02-13 02:33:11 +03:00
|
|
|
mon.Enter();
|
1999-09-10 02:05:05 +04:00
|
|
|
if (rv == NS_BASE_STREAM_WOULD_BLOCK) {
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_ASSERTION(readCount <= writeBufLen, "reader returned bad readCount");
|
2001-01-27 04:28:00 +03:00
|
|
|
// XXX should not update counters if reader returned WOULD_BLOCK!!
|
2000-08-22 11:03:33 +04:00
|
|
|
writeBuf += readCount;
|
|
|
|
writeBufLen -= readCount;
|
|
|
|
*writeCount += readCount;
|
|
|
|
amt -= readCount;
|
|
|
|
pipe->mWriteCursor += readCount;
|
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
// The reader didn't have anything else to put in the buffer, so
|
|
|
|
// call flush to notify the guy downstream, hoping that he'll somehow
|
|
|
|
// wake up the guy upstream to eventually produce more data for us.
|
|
|
|
nsresult rv2 = Flush();
|
2001-01-27 04:28:00 +03:00
|
|
|
if (NS_FAILED(rv2)) {
|
|
|
|
if (rv2 == NS_BASE_STREAM_WOULD_BLOCK)
|
|
|
|
rv = pipe->mCondition;
|
1999-08-10 23:18:39 +04:00
|
|
|
goto done;
|
2001-01-27 04:28:00 +03:00
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
// else we flushed, so go around again
|
|
|
|
continue;
|
|
|
|
}
|
1999-09-01 01:47:30 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// save the failure condition so that we can get it again later
|
|
|
|
pipe->mCondition = rv;
|
|
|
|
goto done;
|
|
|
|
}
|
1999-09-10 02:05:05 +04:00
|
|
|
if (readCount == 0) { // EOF
|
|
|
|
pipe->mCondition = NS_BASE_STREAM_CLOSED;
|
|
|
|
goto done;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
NS_ASSERTION(readCount <= writeBufLen, "reader returned bad readCount");
|
|
|
|
writeBuf += readCount;
|
|
|
|
writeBufLen -= readCount;
|
|
|
|
*writeCount += readCount;
|
1999-11-02 08:38:22 +03:00
|
|
|
amt -= readCount;
|
1999-08-10 23:18:39 +04:00
|
|
|
pipe->mWriteCursor += readCount;
|
|
|
|
}
|
|
|
|
if (pipe->mWriteCursor == pipe->mWriteLimit) {
|
|
|
|
pipe->mWriteCursor = nsnull;
|
|
|
|
pipe->mWriteLimit = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
1999-10-08 06:03:34 +04:00
|
|
|
|
1999-08-10 23:18:39 +04:00
|
|
|
if (mBlocking && rv == NS_BASE_STREAM_WOULD_BLOCK && *writeCount > 0) {
|
|
|
|
mon.Notify(); // wake up reader
|
|
|
|
}
|
1999-10-08 06:03:34 +04:00
|
|
|
|
|
|
|
;
|
1999-08-10 23:18:39 +04:00
|
|
|
} // exit monitor
|
|
|
|
|
2000-08-22 11:03:33 +04:00
|
|
|
if (mObserver && *writeCount > 0) {
|
|
|
|
mObserver->OnWrite(this, *writeCount);
|
1999-08-10 23:18:39 +04:00
|
|
|
}
|
1999-11-02 08:38:22 +03:00
|
|
|
NS_ASSERTION(*writeCount <= count, "wrote more than expected");
|
1999-08-10 23:18:39 +04:00
|
|
|
return *writeCount == 0 ? rv : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NS_METHOD
|
2000-08-22 11:03:33 +04:00
|
|
|
nsReadFromRawBuffer(nsIOutputStream* outStr,
|
|
|
|
void* closure,
|
1999-08-10 23:18:39 +04:00
|
|
|
char* toRawSegment,
|
|
|
|
PRUint32 offset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *readCount)
|
|
|
|
{
|
|
|
|
const char* fromBuf = (const char*)closure;
|
2002-01-12 06:18:55 +03:00
|
|
|
memcpy(toRawSegment, &fromBuf[offset], count);
|
1999-08-10 23:18:39 +04:00
|
|
|
*readCount = count;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::Write(const char* fromBuf,
|
|
|
|
PRUint32 bufLen,
|
|
|
|
PRUint32 *writeCount)
|
|
|
|
{
|
|
|
|
return WriteSegments(nsReadFromRawBuffer, (void*)fromBuf, bufLen, writeCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::Flush(void)
|
|
|
|
{
|
|
|
|
nsPipe* pipe = GET_OUTPUTSTREAM_PIPE(this);
|
2000-08-22 11:03:33 +04:00
|
|
|
nsAutoMonitor mon(pipe->GetMonitor());
|
1999-08-10 23:18:39 +04:00
|
|
|
nsresult rv = NS_OK;
|
1999-08-13 04:46:16 +04:00
|
|
|
PRBool firstTime = PR_TRUE;
|
1999-08-10 23:18:39 +04:00
|
|
|
while (PR_TRUE) {
|
|
|
|
// check write buffer again while in the monitor
|
|
|
|
PRUint32 amt;
|
|
|
|
const char* buf;
|
|
|
|
rv = pipe->GetReadSegment(0, &buf, &amt);
|
1999-08-13 04:46:16 +04:00
|
|
|
if (firstTime && amt == 0) {
|
|
|
|
// If we think we needed to flush, yet there's nothing
|
|
|
|
// in the buffer to read, we must have not been able to
|
1999-08-24 12:45:17 +04:00
|
|
|
// allocate any segments.
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
1999-08-13 04:46:16 +04:00
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
if (NS_FAILED(rv) || amt == 0) return rv;
|
1999-08-13 04:46:16 +04:00
|
|
|
firstTime = PR_FALSE;
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
// else notify the reader and wait
|
|
|
|
rv = mon.Notify();
|
|
|
|
if (NS_FAILED(rv)) return rv; // interrupted
|
|
|
|
if (mBlocking) {
|
|
|
|
rv = mon.Wait();
|
|
|
|
if (NS_FAILED(rv)) return rv; // interrupted
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NS_BASE_STREAM_WOULD_BLOCK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NS_METHOD
|
2000-08-22 11:03:33 +04:00
|
|
|
nsReadFromInputStream(nsIOutputStream* outStr,
|
|
|
|
void* closure,
|
1999-08-10 23:18:39 +04:00
|
|
|
char* toRawSegment,
|
|
|
|
PRUint32 offset,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *readCount)
|
|
|
|
{
|
|
|
|
nsIInputStream* fromStream = (nsIInputStream*)closure;
|
|
|
|
return fromStream->Read(toRawSegment, count, readCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::WriteFrom(nsIInputStream* fromStream,
|
|
|
|
PRUint32 count,
|
|
|
|
PRUint32 *writeCount)
|
|
|
|
{
|
|
|
|
return WriteSegments(nsReadFromInputStream, fromStream, count, writeCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::GetNonBlocking(PRBool *aNonBlocking)
|
|
|
|
{
|
|
|
|
*aNonBlocking = !mBlocking;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPipe::nsPipeOutputStream::SetNonBlocking(PRBool aNonBlocking)
|
|
|
|
{
|
|
|
|
mBlocking = !aNonBlocking;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1999-09-26 11:26:21 +04:00
|
|
|
#ifdef PAGE_MANAGER
|
1999-08-10 23:18:39 +04:00
|
|
|
static NS_DEFINE_CID(kPageManagerCID, NS_PAGEMANAGER_CID);
|
1999-09-26 11:26:21 +04:00
|
|
|
#endif
|
2000-06-03 13:46:12 +04:00
|
|
|
static NS_DEFINE_CID(kMemoryCID, NS_MEMORY_CID);
|
1999-08-10 23:18:39 +04:00
|
|
|
|
|
|
|
NS_COM nsresult
|
2000-08-22 11:03:33 +04:00
|
|
|
NS_NewPipe(nsIInputStream* *inStrResult,
|
|
|
|
nsIOutputStream* *outStrResult,
|
1999-08-10 23:18:39 +04:00
|
|
|
PRUint32 segmentSize,
|
2000-08-22 11:03:33 +04:00
|
|
|
PRUint32 maxSize,
|
|
|
|
PRBool nonBlockingInput,
|
|
|
|
PRBool nonBlockingOutput,
|
|
|
|
nsIMemory* memory)
|
1999-08-10 23:18:39 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-12-15 01:08:40 +03:00
|
|
|
NS_ASSERTION(segmentSize > 0, "need to supply segmentSize for buffer");
|
|
|
|
NS_ASSERTION(maxSize > 0, "need to supply maxSize for buffer");
|
2000-06-03 13:46:12 +04:00
|
|
|
const nsCID* cid = &kMemoryCID;
|
1999-09-26 11:26:21 +04:00
|
|
|
#ifdef PAGE_MANAGER
|
1999-09-11 03:34:12 +04:00
|
|
|
// Take the page manager out altogether because some unices don't
|
|
|
|
// know how to reserve VM -- only preallocate it which takes up a lot
|
|
|
|
// of space.
|
|
|
|
#ifndef XP_MAC
|
1999-08-13 04:46:16 +04:00
|
|
|
// Don't use page buffers on the mac because we don't really have
|
|
|
|
// VM there, and they end up being more wasteful:
|
1999-09-11 03:34:12 +04:00
|
|
|
if (segmentSize >= NS_PAGEMGR_PAGE_SIZE) {
|
|
|
|
cid = &kPageManagerCID;
|
|
|
|
}
|
|
|
|
#endif
|
1999-08-13 04:46:16 +04:00
|
|
|
#endif
|
2000-08-22 11:03:33 +04:00
|
|
|
nsCOMPtr<nsIMemory> alloc = memory;
|
|
|
|
if (alloc == nsnull) {
|
|
|
|
alloc = do_GetService(*cid, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
|
1999-08-24 12:45:17 +04:00
|
|
|
nsPipe* pipe = new nsPipe();
|
1999-08-10 23:18:39 +04:00
|
|
|
if (pipe == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2000-08-22 11:03:33 +04:00
|
|
|
rv = pipe->Initialize(segmentSize, maxSize,
|
|
|
|
nonBlockingInput, nonBlockingOutput,
|
|
|
|
alloc);
|
1999-08-24 12:45:17 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
delete pipe;
|
|
|
|
return rv;
|
|
|
|
}
|
1999-08-10 23:18:39 +04:00
|
|
|
*inStrResult = pipe->GetInputStream();
|
|
|
|
*outStrResult = pipe->GetOutputStream();
|
|
|
|
NS_ADDREF(*inStrResult);
|
|
|
|
NS_ADDREF(*outStrResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|