<?php

namespace App\Classes\Sock;

final class SocketException extends \Exception {
	public const CANT_CREATE_SOCKET = 1;
	public const CANT_BIND_SOCKET	= 2;
	public const CANT_LISTEN		= 3;
	public const CANT_ACCEPT		= 4;
	public const CANT_CONNECT		= 5;
	public const SOCKET_ERROR		= 6;
	public const SOCKET_EAGAIN		= 11;
	public const SOCKET_READ		= 22;

	private array $messages = [
		self::CANT_CREATE_SOCKET	=> 'Can\'t create socket: "%s"',
		self::CANT_BIND_SOCKET		=> 'Can\'t bind socket: "%s"',
		self::CANT_LISTEN			=> 'Can\'t listen: "%s"',
		self::CANT_ACCEPT			=> 'Can\'t accept connections: "%s"',
		self::CANT_CONNECT			=> 'Can\'t connect: "%s"',
		self::SOCKET_ERROR			=> 'Socket Error: "%s"',
		self::SOCKET_EAGAIN			=> 'Socket Resource Temporarily Unavailable - Try again',
		self::SOCKET_READ			=> 'Unable to read from socket',
	];

	public function __construct(int $code,string $params=NULL) {
		$message = $params
			? call_user_func_array('sprintf',[$this->messages[$code],$params])
			: $this->messages[$code];

		parent::__construct($message,$code);
	}
}