Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace SockTest;
- class Server
- {
- public $socket;
- public $error_no;
- public $error_string;
- public $event_base;
- public $event_listener;
- private $connections = array();
- public function __construct($address, $port = 0, $domain = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP)
- {
- if (($this->event_base = new \EventBase()) === false) {
- return false;
- }
- if (($this->socket = socket_create($domain, $type, $protocol)) === false) {
- $this->error_num = socket_last_error();
- $this->error_msg = socket_strerror($this->error_num);
- return false;
- }
- if (socket_bind($this->socket, $address, $port) === false) {
- $this->error_num = socket_last_error($this->socket);
- $this->error_msg = socket_strerror($this->error_num);
- return false;
- }
- if (socket_listen($this->socket, SOMAXCONN) === false) {
- $this->error_num = socket_last_error($this->socket);
- $this->error_msg = socket_strerror($this->error_num);
- return false;
- }
- $this->event_listener = new \EventListener(
- $this->event_base,
- array($this, "connectionCallback"),
- $this->event_base,
- \EventListener::OPT_CLOSE_ON_FREE | \EventListener::OPT_REUSEABLE,
- 0,
- $this->socket
- );
- $this->event_listener->setErrorCallback(array($this, "errorCallback"));
- return $this;
- }
- public function start()
- {
- $this->event_base->dispatch();
- }
- public function connectionCallback($event_listener, $sock, $address, $context)
- {
- $this->connections[] = new ServerConnection($this->event_base, $sock);
- }
- public function errorCallback($event_listener, $context)
- {
- $this->error_num = \EventUtil::getLastSocketErrno();
- $this->error_msg = \EventUtil::getLastSocketError();
- $this->base->exit(NULL);
- }
- }
- class ServerConnection
- {
- private $event_buffer;
- private $event_base;
- public function __construct($event_base, $sock)
- {
- $this->event_base = $event_base;
- $this->event_buffer = new \EventBufferEvent($event_base, $sock, \EventBufferEvent::OPT_CLOSE_ON_FREE);
- $this->event_buffer->setCallbacks(array($this, "readCallback"), null, array($this, "eventCallback"), null);
- if (!$this->event_buffer->enable(\Event::READ)) {
- //failed to read
- return false;
- }
- }
- public function __destruct() {
- $this->event_buffer->free();
- }
- public function readCallback(\EventBufferEvent $event_buffer = null, $args = null)
- {
- $input = $event_buffer->getInput();
- $input_text = $input->read(1024);
- $output = $event_buffer->getOutput();
- //send replies via $output->add()
- $output->add(strtolower($input_text));
- }
- public function eventCallback(\EventBufferEvent $event_buffer = null, $events = 0, $args = null)
- {
- if ($events & \EventBufferEvent::ERROR) {
- //we got an error
- }
- if ($events & (\EventBufferEvent::EOF | \EventBufferEvent::ERROR)) {
- //connection close
- $this->__destruct();
- }
- }
- }
- $srv = new Server("0.0.0.0", 4200);
- $srv->start();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement