Advertisement
miken32

Untitled

Aug 28th, 2013
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.93 KB | None | 0 0
  1. <?php
  2. namespace SockTest;
  3.  
  4. class Server
  5. {
  6.     public $socket;
  7.     public $error_no;
  8.     public $error_string;
  9.     public $event_base;
  10.     public $event_listener;
  11.     private $connections = array();
  12.  
  13.     public function __construct($address, $port = 0, $domain = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP)
  14.     {
  15.         if (($this->event_base = new \EventBase()) === false) {
  16.             return false;
  17.         }
  18.        
  19.         if (($this->socket = socket_create($domain, $type, $protocol)) === false) {
  20.             $this->error_num = socket_last_error();
  21.             $this->error_msg = socket_strerror($this->error_num);
  22.             return false;
  23.         }
  24.         if (socket_bind($this->socket, $address, $port) === false) {
  25.             $this->error_num = socket_last_error($this->socket);
  26.             $this->error_msg = socket_strerror($this->error_num);
  27.             return false;
  28.         }
  29.         if (socket_listen($this->socket, SOMAXCONN) === false) {
  30.             $this->error_num = socket_last_error($this->socket);
  31.             $this->error_msg = socket_strerror($this->error_num);
  32.             return false;
  33.         }
  34.  
  35.         $this->event_listener = new \EventListener(
  36.             $this->event_base,
  37.             array($this, "connectionCallback"),
  38.             $this->event_base,
  39.             \EventListener::OPT_CLOSE_ON_FREE | \EventListener::OPT_REUSEABLE,
  40.             0,
  41.             $this->socket
  42.         );
  43.         $this->event_listener->setErrorCallback(array($this, "errorCallback"));
  44.         return $this;
  45.     }
  46.  
  47.     public function start()
  48.     {
  49.         $this->event_base->dispatch();
  50.     }
  51.  
  52.     public function connectionCallback($event_listener, $sock, $address, $context)
  53.     {
  54.         $this->connections[] = new ServerConnection($this->event_base, $sock);
  55.     }
  56.    
  57.     public function errorCallback($event_listener, $context)
  58.     {
  59.         $this->error_num = \EventUtil::getLastSocketErrno();
  60.         $this->error_msg = \EventUtil::getLastSocketError();
  61.         $this->base->exit(NULL);
  62.     }
  63. }
  64.  
  65. class ServerConnection
  66. {
  67.     private $event_buffer;
  68.     private $event_base;
  69.    
  70.     public function __construct($event_base, $sock)
  71.     {
  72.         $this->event_base = $event_base;
  73.         $this->event_buffer = new \EventBufferEvent($event_base, $sock, \EventBufferEvent::OPT_CLOSE_ON_FREE);
  74.         $this->event_buffer->setCallbacks(array($this, "readCallback"), null, array($this, "eventCallback"), null);
  75.         if (!$this->event_buffer->enable(\Event::READ)) {
  76.             //failed to read
  77.             return false;
  78.         }
  79.     }
  80.  
  81.     public function __destruct() {
  82.         $this->event_buffer->free();
  83.     }
  84.  
  85.     public function readCallback(\EventBufferEvent $event_buffer = null, $args = null)
  86.     {
  87.         $input = $event_buffer->getInput();
  88.         $input_text = $input->read(1024);
  89.         $output = $event_buffer->getOutput();
  90.         //send replies via $output->add()
  91.         $output->add(strtolower($input_text));
  92.     }
  93.  
  94.     public function eventCallback(\EventBufferEvent $event_buffer = null, $events = 0, $args = null)
  95.     {
  96.         if ($events & \EventBufferEvent::ERROR) {
  97.             //we got an error
  98.         }
  99.         if ($events & (\EventBufferEvent::EOF | \EventBufferEvent::ERROR)) {
  100.             //connection close
  101.             $this->__destruct();
  102.         }
  103.     }
  104. }
  105.  
  106. $srv = new Server("0.0.0.0", 4200);
  107. $srv->start();
  108. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement