The realization of streaming in PHP

Source: Internet
Author: User

Implementation stream

One of the most powerful features of PHP's flow is its access to a wide range of data sources: Ordinary files, compressed files, network transparent channels, encrypted networks, named pipes, and domain sockets, which are unified APIs for both user space and the interior.

Under the surface of the PHP stream

For a given stream instance, such as file flow and network flow, the difference lies in the OPS member in the PHP_STREAM structure returned by the flow creation function you used in the previous chapter.

typedef struct _PHP_STREAM {  
    ...  
    Php_stream_ops *ops;  
    ...  
} Php_stream;

The PHP_STREAM_OPS structure defines a set of function pointers and a description tag.

 typedef struct _PHP_STREAM_OPS {size_t (*write) (Php_stream *stream, const char *BUF,  
    size_t Count TSRMLS_DC);  
    size_t (*read) (Php_stream *stream, Char *buf, size_t count tsrmls_dc);  
    Int (*close) (Php_stream *stream, int close_handle tsrmls_dc);  
      
    Int (*flush) (Php_stream *stream tsrmls_dc);  
      
    const char *label;  
    Int (*seek) (Php_stream *stream, off_t offset, int whence, off_t *newoffset);  
    Int (*cast) (Php_stream *stream, int castas, void **ret tsrmls_dc);  
    Int (*stat) (Php_stream *stream, php_stream_statbuf *ssb tsrmls_dc);  
Int (*set_option) (Php_stream *stream, int option,int value, void *ptrparam tsrmls_dc); } php_stream_ops 

When a stream access function such as Php_stream_read () is called, the Flow wrapper layer actually resolves the corresponding function in the stream->ops, so the actual invocation is the read implementation peculiar to the current stream type. For example, the read function in the stream OPS structure of a normal file is implemented as follows (the actual implementation is a bit more complex than the following example):

size_t Php_stdio_read (Php_stream *stream, Char *buf,  
                                size_t count tsrmls_dc)  
{  
    Php_stdio_stream_data * data =  
                (php_stdio_stream_data*) stream->abstract;  
    Return read (DATA->FD, buf, count);  
}

In the OPS structure used by the Compress.zlib stream, read then points to the following function:

size_t Php_zlib_read (Php_stream *stream, Char *buf,  
                                size_t count tsrmls_dc)  
{  
    struct php_gz_stream_data_ T *data =  
            (struct php_gz_stream_data_t *) stream->abstract;  
      
    Return Gzread (Data->gz_file, buf, count);  
}

The 1th point to note here is that the function pointers pointed to by the OPS structure are often a thin proxy for the true reading function of the data source. In the above two examples, the standard I/O stream uses the POSIX read () function, while the zlib stream uses the Libz gzread () function.

You may also notice that the stream->abstract element is used here. This is a handy pointer to a stream implementation that can be used to get a variety of related bundle information. In the example above, a pointer to a custom structure body is used to store the file descriptor to be used by the underlying read function.

Another thing you might notice is that every function in the PHP_STREAM_OPS structure expects an existing stream instance, but how do you get the instance? How are the abstract members set and when does the flow indicate which OPS structure to use? The answer is in the first function (Php_stream_open_wrapper ()) that you used in the previous chapter to open the stream.

When this function is invoked, the flow wrapper layer of PHP attempts to determine what protocol is requested based on the scheme://part of the passed URL. This allows it to find the corresponding Php_stream_wrapper entry in the registered PHP wrapper. Each php_stream_wrapper structure can take its own OPS element, which points to a php_stream_wrapper_ops structure:

typedef struct _PHP_STREAM_WRAPPER_OPS {Php_stream * (*stream_opener) (Php_stream_wrapper *wrapper,  
                        Char *filename, char *mode, int options, Char **opened_path,  
    Php_stream_context *context streams_dc tsrmls_dc);  
    Int (*stream_closer) (Php_stream_wrapper *wrapper, Php_stream *stream tsrmls_dc); Int (*stream_stat) (Php_stream_wrapper *wrapper, Php_stream *stream, ph  
    P_stream_statbuf *SSB tsrmls_dc); Int (*url_stat) (Php_stream_wrapper *wrapper, char *url, int flags, PHP  
    _stream_statbuf *SSB, Php_stream_context *context tsrmls_dc);  
                  Php_stream * (*dir_opener) (Php_stream_wrapper *wrapper, Char *filename, Char *mode,      int options, char **opened_path, Php_stream_context *context STR  
      
    EAMS_DC tsrmls_dc);  
      
    const char *label; Int (*unlink) (Php_stream_wrapper *wrapper, char *url, int options, PHP  
      
    _stream_context *context tsrmls_dc);  
                        Int (*rename) (Php_stream_wrapper *wrapper, Char *url_from, Char *url_to,  
      
      
    int options, Php_stream_context *context tsrmls_dc);  
                        Int (*stream_mkdir) (Php_stream_wrapper *wrapper, char *url, int mode, int options,  
    Php_stream_context *context tsrmls_dc);  
                        Int (*stream_rmdir) (Php_stream_wrapper *wrapper, char *url, int options,  
   Php_stream_context *context                     TSRMLS_DC); } php_stream_wrapper_ops;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.