1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| class Net_Socket
{
/** Etat connecté */
const CONNECTED = 1;
/** Etat non connecté */
const DISCONNECTED = 0;
/**
* Hote vers lequel se connecter. Ip ou domaine
* @var string
*/
protected $host = "";
/**
* Port de connexion
* @var int
*/
protected $port = 0;
/**
* Timeout de connexion en seconde. Egalement utilisé pour les timeout de lecture/écriture
* @var int
*/
protected $timeout = 5;
/**
* Socket
* @var resource
*/
protected $socket = null;
/**
* Etat de la socket
* @var int
*/
protected $state = 0;
/**
* Socket bloquante ou non
* @var boolean
*/
protected $isBlocking = false;
/**
* Création de la socket. Initialise également l'error handler pour transformer les
* erreur de socket en exception.
* @param type $blocking
*/
public function __construct($blocking = true)
{
$this->isBlocking = $blocking;
$this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
}
/**
* Connexion de la socket sur $host:$port avec un delay de $timeout
* @param type $host
* @param type $port
* @param type $timeout
* @return boolean
* @throws RuntimeException
*/
public function connect($host,$port,$timeout = 15)
{
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
if(!empty($this->socket) && is_resource($this->socket))
{
//Timeout de lecture
socket_set_option($this->socket,SOL_SOCKET,SO_RCVTIMEO,array(
'sec'=>$this->timeout,
'usec'=>0)
);
//Timeout d'écriture
socket_set_option($this->socket,SOL_SOCKET,SO_SNDTIMEO,array(
'sec'=>$this->timeout,
'usec'=>0)
);
$connect = socket_connect($this->socket, $this->host, $this->port);
if(!$connect)
{
socket_close($this->socket);
throw new RuntimeException('Can\'t connect to '.$this->host.':'.$this->port.'.
Reason : '.socket_strerror(socket_last_error()));
}
}
else
{
return false;
}
$this->state = self::CONNECTED;
return true;
}
/**
* Déconnecte la socket
* @return boolean
*/
public function disconnect()
{
if($this->isConnected())
{
socket_close($this->socket);
$this->state = self::DISCONNECTED;
return true;
}
return false;
}
/**
* Ecrit sur la socket le contenu de $buffer
* @param string $buffer
* @return mixed Nombre d'octet ou false en cas d'erreur
* @throws InvalidArgumentException
*/
public function write($buffer)
{
if($this->isConnected())
{
return socket_write($this->socket,$buffer, strlen($buffer));
}
throw new InvalidArgumentException('No active connection');
}
/**
* Lecture sur la socket
* @param int $size nombre d'octet à lire
* @return boolean
* @throws RuntimeException
*/
public function read($size = 2048)
{
if($this->isConnected())
{
$datas = socket_read($this->socket, $size, PHP_BINARY_READ);
if($datas !== false)
{
return $datas;
}
else
{
Throw new RuntimeException("Lecture impossible : ".socket_strerror(socket_last_error($this->socket)));
}
return false;
}
}
/**
* Vérifie si la socket est connecté
* @return boolean
*/
public function isConnected()
{
return (is_resource($this->socket) && $this->state == self::CONNECTED);
}
} |
Partager