Re: streams, stderr/out and zend_fprintf ?
| From: | Wez Furlong | Date: | Tue, 06 May 2003 19:59:35 +0000 |
| Subject: | Re: streams, stderr/out and zend_fprintf ? | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-1322@lists.php.net to get a copy of this message | ||
On Tue, 6 May 2003, Hans Zaunere wrote:
> I don't have to deal with Solaris, fortunately. However, I'm unclear as to
> what I've described that makes it sound like I don't need to use the streams
> API. Because it's primarily a CLI extension? Or because I'm talking to a
> serial port?
Both ;)
Usually, you will want to perform low level operations on the serial
port using ioctl() or other library functions that expect to work with
the fd. Also, you might not want to have any buffering at all; so it
sounds like you don't need a stream. (but read on...)
> My initial take on getting the serial device online was to use a resource,
> defined as (where msr is the name of the device):
>
> typedef struct {
> int filedes;
> struct termios sio;
> } msr_resource;
>
> Thus, also having init and destructor functions to open(), cfmakeraw(),
> cfsetspeed(), tcsetattr() and close() the returned filedes.
>
> Then it hit me that php.net/streams says a stream is already a resource with
> many of the resource housekeeping tasks taken care of. Great, but then how
> can I set my serial options on a php stream (like cfmakeraw, for example)?
>
> I see the ability to cast a php_stream to a filedes, but vice-versa, or as
> you said "you can open a php stream around an existing file descriptor," I'm
> having no luck?
>
php_stream_fopen_from_fd() is present in 4.3.2 and later (try a snapshot
from snaps.php.net), however, it sounds to me like you *might* benefit
from implementing your own stream. (depends on how complex your API is).
> Perhaps I'm making it overly complex, and simply using a resource->filedes is
> the way to go, but I'd like to get familiarized with php streams. Any
> additional tips would be helpful.
If you are tempted to implement your own streams, I recommend reading my
articles in PHP Magazine </plug> and/or the content from my Streams talk
at the International PHP Conference in Amsterdam this week </plug> ;)
(I'm more than half serious here)
For starters, check out the API docs for streams at php.net/streams;
although by no means complete, its better than nothing; they focus more
on using the streams API than implementing streams.
Its hard to advise you on which is the "best" approach for your
extension without really knowing what it does, or what functions it
exposes to PHP user-land. Generally, it is best to keep it simple
(KISS), so it might make you life easier to go with the structure you
described and make it into a resource, but replace the "int fildes" with
"php_stream *stream".
Hope that helps :)
--Wez.