Esempio n. 1
0
        public FileHandleDataSource(PhpStream handle, int flength)
        {
            this.handle  = handle;
            this.flength = flength;

            //TODO : Replace memorystream with a better reading/seeking class
            PhpBytes data;

            if (flength > 0)
            {
                data = handle.ReadBytes(flength);
            }
            else
            {
                data = handle.ReadBinaryContents(-1);
            }
            this.m_ms = new MemoryStream(data.ReadonlyData);
        }
Esempio n. 2
0
            private void Callback(IAsyncResult ar)
            {
                if (access == StreamAccessOptions.Read)
                {
                    int count = stream.EndRead(ar);
                    if (count > 0)
                    {
                        if (count != buffer.Length)
                        {
                            // TODO: improve streams
                            var buf = new byte[count];
                            Buffer.BlockCopy(buffer, 0, buf, 0, count);
                            phpStream.WriteBytes(buf);
                        }
                        else
                        {
                            phpStream.WriteBytes(buffer);
                        }

                        stream.BeginRead(buffer, 0, buffer.Length, callback, ar.AsyncState);
                    }
                    else
                    {
                        phpStream.Flush();
                        stream.Dispose();
                    }
                }
                else
                {
                    buffer = phpStream.ReadBytes(BufferSize);
                    if (buffer != null)
                    {
                        stream.BeginWrite(buffer, 0, buffer.Length, callback, ar.AsyncState);
                    }
                    else
                    {
                        stream.EndWrite(ar);
                        stream.Dispose();
                    }
                }
            }
Esempio n. 3
0
            public static bool BeginIO(Stream stream, PhpStream phpStream, StreamAccessOptions access, int desc_no)
            {
                if (access == StreamAccessOptions.Read && !phpStream.CanWrite ||
                    access == StreamAccessOptions.Write && !phpStream.CanRead)
                {
                    PhpException.Throw(PhpError.Warning, Resources.LibResources.descriptor_item_invalid_mode, desc_no.ToString());
                    return(false);
                }

                var pipe = new ActivePipe
                {
                    stream    = stream,
                    phpStream = phpStream,
                    access    = access
                };

                pipe.callback = new AsyncCallback(pipe.Callback);

                if (access == StreamAccessOptions.Read)
                {
                    var buffer = new byte[BufferSize];
                    stream.BeginRead(buffer, 0, buffer.Length, pipe.callback, null);
                    pipe.buffer = buffer;
                }
                else
                {
                    pipe.buffer = phpStream.ReadBytes(BufferSize);
                    if (pipe.buffer != null)
                    {
                        stream.BeginWrite(pipe.buffer, 0, pipe.buffer.Length, pipe.callback, null);
                    }
                    else
                    {
                        stream.Dispose();
                    }
                }

                return(true);
            }
Esempio n. 4
0
            public static bool BeginIO(Stream stream, PhpStream phpStream, StreamAccessOptions access, int desc_no)
            {
                if (access == StreamAccessOptions.Read && !phpStream.CanWrite ||
                    access == StreamAccessOptions.Write && !phpStream.CanRead)
                {
                    PhpException.Throw(PhpError.Warning, LibResources.GetString("descriptor_item_invalid_mode", desc_no));
                    return(false);
                }

                ActivePipe pipe = new ActivePipe();

                pipe.stream    = stream;
                pipe.phpStream = phpStream;
                pipe.access    = access;
                pipe.callback  = new AsyncCallback(pipe.Callback);

                if (access == StreamAccessOptions.Read)
                {
                    var buffer = new byte[BufferSize];
                    stream.BeginRead(buffer, 0, buffer.Length, pipe.callback, null);
                    pipe.buffer = new PhpBytes(buffer);
                }
                else
                {
                    pipe.buffer = phpStream.ReadBytes(BufferSize);
                    if (pipe.buffer != null)
                    {
                        stream.BeginWrite(pipe.buffer.ReadonlyData, 0, pipe.buffer.Length, pipe.callback, null);
                    }
                    else
                    {
                        stream.Close();
                    }
                }

                return(true);
            }