public static function send($fifo, $message) { $message = serialize($message); $messageLen = hex2bin(str_pad(dechex(strlen($message)), self::MESSAGE_LEN_FIELD_LENGTH_BYTES * 2, "0", STR_PAD_LEFT)); $writeStream = fopen($fifo, "c+"); if (!$writeStream) { throw new \Exception("Could not open fifo {$fifo} for writing: " . var_export(error_get_last(), 1)); } if (!fwrite($writeStream, $messageLen . $message)) { log::warning("Could not write to fifo {$fifo}"); throw new \Exception("Could not write to fifo {$fifo}"); } else { log::debug("Written message to {$fifo}"); } fclose($writeStream); }
/** * Send message to all pipes in directory, optionally using StreamSet * * @param string $fifoPath Path to directory with pipes * @param mixed $message Message should be castable to string * @param mixed $useStreamSet If \Phasty\Stream\StreamSet use it for streams \ * If false no need using streams \ * If null use default StreamSet instance */ public static function send($fifoPath, $message, $useStreamSet = false) { $fifos = glob("{$fifoPath}/*"); $message = (string) $message; log::debug("Mx1 send to " . count($fifos) . " fifos"); foreach ($fifos as $fifo) { if (!isset(self::$streams[$fifo])) { self::$streams[$fifo] = new \Phasty\Stream\Reader\NamedPipe($fifo); if ($useStreamSet !== false) { if (is_null($useStreamSet)) { self::$streams[$fifo]->setStreamSet(\Phasty\Stream\StreamSet::instance()); } else { self::$streams[$fifo]->setStreamSet($useStreamSet); } } self::$streams[$fifo]->on("close", function () use($fifo) { unset(self::$streams[$fifo]); }); } self::$streams[$fifo]->write($message); } }
public function write($data) { log::debug("Writing data to {$this->fifoFile}"); parent::write($data); }
protected function logByType($msg, $type) { if (!$this->canLog) { echo "{$msg}\n"; return; } if ($this->verbose) { echo "{$msg}\n"; } switch ($type) { case 'error': log::error($msg); break; case 'debug': log::debug($msg); break; default: log::info($msg); } }