. */ namespace SP\Util; use SP\Core\Exceptions\SPException; /** * Class Connection para crear conexiones TCP o UDP * * @package SP\Util */ final class Connection implements ConnectionInterface { /** * @var resource */ protected $socket; /** * @var string */ protected $host = ''; /** * @var int */ protected $port = 0; /** * Código de error del socket * * @var int */ protected $errorno = 0; /** * Mensaje de error del socket * * @var string */ protected $errorstr = ''; /** * @param $host string El host a conectar * @param $port int El puerto a conectar */ public function __construct(string $host, int $port) { $this->host = gethostbyname($host); $this->port = $port; } /** * Obtener un socket * * @param $type int EL tipo de socket TCP/UDP * * @return resource * @throws SPException */ public function getSocket(int $type) { switch ($type) { case self::TYPE_UDP: $this->socket = $this->getUDPSocket(); break; default: $this->socket = $this->getTCPSocket(); break; } if ($this->socket === false) { throw new SPException($this->getSocketError(), SPException::WARNING); } stream_set_timeout($this->socket, self::SOCKET_TIMEOUT); return $this->socket; } /** * Obtener un socket del tipo UDP * * @return resource */ private function getUDPSocket() { return stream_socket_client('udp://' . $this->host . ':' . $this->port, $this->errorno, $this->errorstr, self::SOCKET_TIMEOUT); // return @socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); } /** * Obtener un socket del tipo TCP * * @return resource */ private function getTCPSocket() { return stream_socket_client('tcp://' . $this->host . ':' . $this->port, $this->errorno, $this->errorstr, self::SOCKET_TIMEOUT); // return @socket_create(AF_INET, SOCK_STREAM, SOL_TCP); } /** * Obtener el último error del socket * * @return string */ public function getSocketError(): string { return sprintf('%s (%d)', $this->errorstr, $this->errorno); // return socket_strerror(socket_last_error($this->_socket)); } /** * Cerrar el socket */ public function closeSocket() { fclose($this->socket); // @socket_close($this->_socket); } /** * Enviar un mensaje al socket * * @param $message string El mensaje a enviar * * @return int|bool * @throws SPException */ public function send(string $message) { if (!is_resource($this->socket)) { throw new SPException(__u('Socket not initialized'), SPException::WARNING); } $nBytes = @fwrite($this->socket, $message); // $nBytes = @socket_sendto($this->_socket, $message, strlen($message), 0, $this->_host, $this->port); if ($nBytes === false) { throw new SPException(__u('Error while sending the data'), SPException::WARNING, $this->getSocketError()); } return $nBytes; } }