玩轉 PHP 網路程式設計全套之 libevent 框架多人聊天應用

勺顛顛發表於2020-04-25
class MyListenerConnection {     private $bev, $base;     private $fd;     private $listener;     private $message;     public function __destruct() {          $this->bev->free();     }     public function __construct($base, $fd,$listener) {         $this->base = $base;         $this->listener = $listener;         $this->bev = new EventBufferEvent($base, $fd, EventBufferEvent::OPT_CLOSE_ON_FREE);         $this->bev->setCallbacks([$this, "echoReadCallback"], [$this, "echoWriteCallback"], [$this, "echoEventCallback"], NULL);         $this->fd = $fd;         if (!$this->bev->enable(Event::READ)) {             echo "Failed to enable READ\n";             return;         }     }     public function echoReadCallback($bev, $ctx) {          $input    = $bev->getInput();         $data = $input->read(8192);         $this->message = $data;          foreach ($this->listener->getConnLists() as $connection){             if ($this->fd!=$connection->fd)$connection->bev->output->add($this->message);          }      }      public function echoWriteCallback($bev, $ctx) {          //echo "finish write\n";     }      public function echoEventCallback($bev, $events, $ctx) {          if ($events & EventBufferEvent::ERROR) {             echo "Error from bufferevent\n";         }          if ($events & (EventBufferEvent::EOF | EventBufferEvent::ERROR)) {             //$bev->free();             $this->__destruct();         }     } }  class MyListener {     public $base, $listener, $socket;     private $conn = array();      public function __destruct() {         foreach ($this->conn as &$c) $c = NULL;     }      public function getConnLists()     {         return $this->conn;     }      public function __construct($port) {          $this->base = new EventBase();         if (!$this->base) {             echo "Couldn't open event base";             exit(1);         }         $this->listener = new EventListener($this->base, [$this, "acceptConnCallback"], $this->base, EventListener::OPT_CLOSE_ON_FREE | EventListener::OPT_REUSEABLE, -1, "0.0.0.0:$port");         if (!$this->listener) {             echo "Couldn't create listener";             exit(1);         }          $this->listener->setErrorCallback(array($this, "accept_error_cb"));     }      public function dispatch() {         $this->base->dispatch();     }      public function acceptConnCallback($listener, $fd, $address, $ctx) {          $base = $this->base;         $this->conn[] = new MyListenerConnection($base, $fd,$this);     }      public function accept_error_cb($listener, $ctx) {         $base = $this->base;          fprintf(STDERR, "Got an error %d (%s) on the listener. "             ."Shutting down.\n",             EventUtil::getLastSocketErrno(),             EventUtil::getLastSocketError());          $base->exit(NULL);     } }  $port = 12345;  if ($argc > 1) {     $port = (int) $argv[1]; } if ($port <= 0 || $port > 65535) {     exit("Invalid port"); }  $l = new MyListener($port);  $l->dispatch(); 

玩轉 PHP 網路程式設計全套之 libevent 框架多人聊天應用

PS

相關使用方法可查閱php手冊和官方資料

本作品採用《CC 協議》,轉載必須註明作者和本文連結

只會php crud的渣渣

相關文章