Pipe.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * @author Jenner <hypxm@qq.com>
  4. * @blog http://www.huyanping.cn
  5. * @license https://opensource.org/licenses/MIT MIT
  6. * @datetime: 2015/11/24 16:29
  7. */
  8. namespace Jenner\SimpleFork\Queue;
  9. class Pipe
  10. {
  11. /**
  12. * @var resource
  13. */
  14. protected $read;
  15. /**
  16. * @var resource
  17. */
  18. protected $write;
  19. /**
  20. * @var string
  21. */
  22. protected $filename;
  23. /**
  24. * @var bool
  25. */
  26. protected $block;
  27. /**
  28. * @param string $filename fifo filename
  29. * @param int $mode
  30. * @param bool $block if blocking
  31. */
  32. public function __construct($filename = '/tmp/simple-fork.pipe', $mode = 0666, $block = false)
  33. {
  34. if (!file_exists($filename) && !posix_mkfifo($filename, $mode)) {
  35. throw new \RuntimeException('create pipe failed');
  36. }
  37. if (filetype($filename) != 'fifo') {
  38. throw new \RuntimeException('file exists and it is not a fifo file');
  39. }
  40. $this->filename = $filename;
  41. $this->block = $block;
  42. }
  43. public function setBlock($block = true)
  44. {
  45. if (is_resource($this->read)) {
  46. $set = stream_set_blocking($this->read, $block);
  47. if (!$set) {
  48. throw new \RuntimeException('stream_set_blocking failed');
  49. }
  50. }
  51. if (is_resource($this->write)) {
  52. $set = stream_set_blocking($this->write, $block);
  53. if (!$set) {
  54. throw new \RuntimeException('stream_set_blocking failed');
  55. }
  56. }
  57. $this->block = $block;
  58. }
  59. /**
  60. * if the stream is blocking, you would better set the value of size,
  61. * it will not return until the data size is equal to the value of param size
  62. *
  63. * @param int $size
  64. * @return string
  65. */
  66. public function read($size = 1024)
  67. {
  68. if (!is_resource($this->read)) {
  69. $this->read = fopen($this->filename, 'r+');
  70. if (!is_resource($this->read)) {
  71. throw new \RuntimeException('open file failed');
  72. }
  73. if (!$this->block) {
  74. $set = stream_set_blocking($this->read, false);
  75. if (!$set) {
  76. throw new \RuntimeException('stream_set_blocking failed');
  77. }
  78. }
  79. }
  80. return fread($this->read, $size);
  81. }
  82. /**
  83. * @param $message
  84. * @return int
  85. */
  86. public function write($message)
  87. {
  88. if (!is_resource($this->write)) {
  89. $this->write = fopen($this->filename, 'w+');
  90. if (!is_resource($this->write)) {
  91. throw new \RuntimeException('open file failed');
  92. }
  93. if (!$this->block) {
  94. $set = stream_set_blocking($this->write, false);
  95. if (!$set) {
  96. throw new \RuntimeException('stream_set_blocking failed');
  97. }
  98. }
  99. }
  100. return fwrite($this->write, $message);
  101. }
  102. /**
  103. *
  104. */
  105. public function __destruct()
  106. {
  107. $this->close();
  108. }
  109. /**
  110. *
  111. */
  112. public function close()
  113. {
  114. if (is_resource($this->read)) {
  115. fclose($this->read);
  116. }
  117. if (is_resource($this->write)) {
  118. fclose($this->write);
  119. }
  120. }
  121. public function remove()
  122. {
  123. return unlink($this->filename);
  124. }
  125. }