Boris Zbarsky wrote:
> ksachdeva wrote:
> > * In the write method of my OutputStream class I invoke my method
> > passing it the required data. This method also returns some data which
> > I now want to pass back to YExt but do not know how to do it.
>
> Ah.  You need to communicate this data to the rest of your code somehow,
> basically.  How you do it is sort of up to you.
>
> >  Are you suggesting that I pass the listerner object
> > from AsynOpen method to nsMyOutstream and invoke OnDataAvailable from
> > nsMyOutStream::Write method ???..... Isn't Write method invoked on
> > separate thread ?
>
> Yes to both.  You'll need to proxy the call over to the right thread, of 
> course.
>
> For example, you could save the new data in some buffer and post an event to 
> the
> main thread.  When the event fires, take the data and call OnDataAvailable on
> the listener.
>

Thanks Boris, in order to avoid creating events and proxy the calls (of
which I do not have a good idea yet) I changed my design little bit.
Instead of using nsMyOutputStream I am using nsMyInputStream now. In
the AsyncOpen method of nsMyChannel this is what I do

<snippet>

        nsCOMPtr<nsIInputStream> In;
        rv = NS_NewMyInputStream(getter_AddRefs(In),param1,param2);

        if(NS_FAILED(rv)){
            return rv;
        }

        // create asynchronous input stream wrapping my input stream.
        nsCOMPtr<nsITransport> transport;
        rv = sts->CreateInputTransport(In, PRInt64(-1), PRInt64(-1),
PR_TRUE,

getter_AddRefs(transport));

        if(NS_FAILED(rv)){
            return rv;
        }

        // not fatal if this fails
        rv = transport->SetEventSink(this, eventQ);

        if(NS_FAILED(rv)){
            return rv;
        }

        // open input stream, and create input stream pump...
        nsCOMPtr<nsIInputStream> asyncIn;
        rv = transport->OpenInputStream(0, 0, 0,
getter_AddRefs(asyncIn));

        if(NS_FAILED(rv)){
            return rv;
        }

        rv = NS_NewInputStreamPump(getter_AddRefs(mPump),asyncIn);

        if(NS_FAILED(rv)){
            return rv;
        }

        rv = mPump->AsyncRead(this,nsnull);

</snippet>

Basically the data I want to pass to my program, I pass it first to
nsMyInputStream as param1 and param2.

Now I can see the Read method of nsMyInputStream invoked where my
implementation looks like this -

NS_METHOD
nsMyInputStream::Read(char* aBuf,PRUint32 aCount,PRUint32* _retval)
{
         // _param2 is a field of nsMyInputStream which was initialized
at the time of
         // stream creation in AsyncOpen of my nsMyChannel
         aBuf = ExchangeData(_param2);

         *_retval = 0; // indicating the close

     return NS_OK;
}

As expected this method is called on a separate thread. Once the method
is finished I can see that the OnStartRequest and OnStopRequest method
of my channel are invoked but not the OnDataAvaliable.

I was kind of having an understanding the the system would invoke
OnDataAvailable for me ?. I was debugging the FileChannel and there it
happens.

Please guide.

Regards & thanks
Kapil






> -Boris

_______________________________________________
dev-tech-network mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-network

Reply via email to