public function __construct($sServer, $iPort) { AirD::Log(AirD::LOGTYPE_INTERNAL, "socketClient's constructor", true); parent::__construct(Config::BIND_ADDRESS); $this->connect($sServer, $iPort); $this->set_non_block(true); SocketEngine::AddFd($this); }
public function __construct($socket) { AirD::Log(AirD::LOGTYPE_INTERNAL, "socketServerClient's constructor", true); $this->socket = $socket; if (!is_resource($this->socket)) { throw new socketException("Invalid socket or resource"); } elseif (!socket_getsockname($this->socket, $this->local_addr, $this->local_port)) { throw new socketException("Could not retrieve local address & port: " . socket_strerror(socket_last_error($this->socket))); } elseif (!socket_getpeername($this->socket, $this->remote_address, $this->remote_port)) { throw new socketException("Could not retrieve remote address & port: " . socket_strerror(socket_last_error($this->socket))); } $this->on_connect(); $this->set_non_block(true); SocketEngine::AddFd($this); }
public function __construct($bind_address = 0, $bind_port = 0, $domain = AF_INET, $type = SOCK_STREAM, $protocol = SOL_TCP) { $this->bind_address = $bind_address; $this->bind_port = $bind_port; $this->domain = $domain; $this->type = $type; $this->protocol = $protocol; if (($this->socket = @socket_create($domain, $type, $protocol)) === false) { throw new socketException("Could not create socket: " . socket_strerror(socket_last_error($this->socket))); } AirD::Log(AirD::LOGTYPE_INTERNAL, "SocketBase's constructor (FD " . (int) $this->socket . ")", true); if (!@socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1)) { throw new socketException("Could not set SO_REUSEADDR: " . $this->get_error()); } if (!@socket_bind($this->socket, $bind_address, $bind_port)) { throw new socketException("Could not bind socket to [{$bind_address} - {$bind_port}]: " . socket_strerror(socket_last_error($this->socket))); } if (!@socket_getsockname($this->socket, $this->local_addr, $this->local_port)) { throw new socketException("Could not retrieve local address & port: " . socket_strerror(socket_last_error($this->socket))); } $this->set_non_block(true); SocketEngine::AddFd($this); }
include "./src/" . $sClass . ".php"; if (!class_exists($sClass)) { AirD::Log(AirD::LOGTYPE_INTERNAL, "Loaded " . $sClass . " as a file, but class still doesn't exist. Aiee."); } } abstract class AirD { public static $aIRCClients = array(); const VERSION_STRING = "1.0"; const LOGTYPE_INTERNAL = "INTERNAL"; const LOGTYPE_HTTP = "HTTP"; const LOGTYPE_IRC = "IRC"; const LOGTYPE_JAVASCRIPT = "JAVASCRIPT"; public static function Log($sType, $sMessage, $bDebug = false) { $sMsg = strftime('%T') . " " . $sType . ": " . $sMessage . "\n"; echo $sMsg; $rLog = fopen("aird.log", "a"); fwrite($rLog, $sMsg); fclose($rLog); } } unlink("aird.log"); if (!class_exists(Config)) { die("Please make a config."); } error_reporting(E_ALL | E_NOTICE | E_STRICT); AirD::Log(AirD::LOGTYPE_INTERNAL, "AirD " . AirD::VERSION_STRING . " starting up..."); new HTTPServer(2001); SocketEngine::process();
public static function process() { // if socketClient is in write set, and $socket->connecting === true, set connecting to false and call on_connect $read_set = SocketEngine::create_read_set(); $write_set = SocketEngine::create_write_set(); $exception_set = SocketEngine::create_exception_set(); $event_time = time(); while (($events = socket_select($read_set, $write_set, $exception_set, null)) !== false) { AirD::Log(AirD::LOGTYPE_INTERNAL, "Top of main loop.", true); if ($events > 0) { AirD::Log(AirD::LOGTYPE_INTERNAL, "Processing " . $events . " socket events", true); foreach ($read_set as $socket) { AirD::Log(AirD::LOGTYPE_INTERNAL, "Processing a read event for " . (int) $socket, true); SocketEngine::$clients[(int) $socket]->read(); } foreach ($write_set as $socket) { $socket = SocketEngine::get_class($socket); if (is_subclass_of($socket, 'socketClient')) { if ($socket->connecting === true) { $socket->on_connect(); $socket->connecting = false; } $socket->do_write(true); } } foreach ($exception_set as $socket) { $socket = SocketEngine::get_class($socket); if (is_subclass_of($socket, 'socketClient')) { $socket->on_disconnect(); if (isset(SocketEngine::$clients[(int) $socket->socket])) { unset(SocketEngine::$clients[(int) $socket->socket]); } } } } if (time() - $event_time > 1) { // only do this if more then a second passed, else we'd keep looping this for every bit recieved foreach (SocketEngine::$clients as $socket) { $socket->on_timer(); } $event_time = time(); } SocketEngine::clean_sockets(); $read_set = SocketEngine::create_read_set(); $write_set = SocketEngine::create_write_set(); $exception_set = SocketEngine::create_exception_set(); } }