Class Pipe { Public $fifoPath; Private $w _pipe; Private $r _pipe; /** * Automatically create a pipeline * * @param string $name pipe name * @param int $mode The permission of the pipe, any user group can read and write by default */ function __construct ($name = ' pipe ', $mode = 0666) { $fifoPath = "/tmp/$name." Posix_getpid (); if (!file_exists ($fifoPath)) { if (!posix_mkfifo ($fifoPath, $mode)) { Error ("Create new pipe ($name) error."); return false; } } else { Error ("Pipe ($name) has exit."); return false; } $this->fifopath = $fifoPath; } /////////////////////////////////////////////////// Write Pipeline function start /////////////////////////////////////////////////// function Open_write () { $this->w_pipe = fopen ($this->fifopath, ' W '); if ($this->w_pipe = = NULL) { Error ("Open pipe {$this->fifopath} for write error."); return false; } return true; } function Write ($data) { Return fwrite ($this->w_pipe, $data); } function Write_all ($data) { $w _pipe = fopen ($this->fifopath, ' W '); Fwrite ($w _pipe, $data); Fclose ($w _pipe); } function Close_write () { Return fclose ($this->w_pipe); } ///////////////////////////////////////////////////////// Read Pipeline correlation function start //////////////////////////////////////////////////////// function Open_read () { $this->r_pipe = fopen ($this->fifopath, ' R '); if ($this->r_pipe = = NULL) { Error ("Open pipe {$this->fifopath} for read error."); return false; } return true; } function Read ($byte = 1024) { Return Fread ($this->r_pipe, $byte); } function Read_all () { $r _pipe = fopen ($this->fifopath, ' R '); $data = "; while (!feof ($r _pipe)) { echo "read one k\n"; $data. = Fread ($r _pipe, 1024); } Fclose ($r _pipe); return $data; } function Close_read () { Return fclose ($this->r_pipe); } /** * Delete Pipeline * * @return Boolean is success */ function Rm_pipe () { Return unlink ($this->fifopath); } } ?> /* With this class, you can implement a simple pipeline communication. */ |