Exemple #1
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;
			}