phpircbot2/modules/fsock.class.php

102 lines
2.3 KiB
PHP

<?php
/*****************************
* Simple Fsock Class by JPT *
* http://jpt.de.tf *
*****************************/
class fsock{
public $host;
public $port;
public $buffer; //is passed to fgets()
public $timeout; //Timout to connect
public $recvtimeout; //Timeout for recieving...
public $endl; //Linebreak to use...
public $debug = false; //Debug switch;
private $connection;
private $errno;
private $errstr;
function __construct($host, $port, $buffer, $endl, $timeout, $recvtimeout){
$this -> host = $host;
$this -> port = $port;
$this -> buffer = $buffer;
$this -> endl = $endl;
$this -> timeout = $timeout;
$this -> recvtimeout = $recvtimeout;
$this -> connection = fsockopen($this -> host, $this -> port, $this -> errno, $this -> errstr, $this -> timeout);
if(!$this -> connection){
$this -> error("Could not connect to ".$this -> host.":".$this -> port."!");
}
if($this -> recvtimeout != false) stream_set_timeout($this -> connection, $this -> recvtimeout);
}
public function checkConnection(){
if(!is_resource($this -> connection)){
$this -> close();
return false;
}
else{
return true;
}
}
public function isConnected(){
return ($this -> connection && is_resource($this -> connection)) ? true : false;
}
public function gotData(){
return ($this -> isConnected()) ? feof($this -> connection) : false;
}
public function send($msg){
$this -> checkConnection();
if(trim($msg) != ""){
if(!fwrite($this -> connection, $msg.$this -> endl)){
if($this -> debug) echo "[Fsock] Could not send:'".$msg."'";
return false;
}
else{
if($this -> debug) echo "[Fsock] Sent: ".$msg."\n";
return true;
}
}
}
public function recv(){
$this -> checkConnection();
if(!feof($this -> connection)){
if(($recv = fread($this -> connection, $this -> buffer)) != ''){
if($this -> debug) echo "[Fsock] Recv: ".$recv."\n";
return $recv;
}
else{
return false;
}
}
else{
return false;
}
}
public function close(){
if($this -> connection){
fclose($this -> connection);
unset($this -> connection);
}
}
function __destruct(){
$this -> close();
}
public function error($msg){
echo "[ClientFSockClass] ".$msg."\n";
//die();
}
}
?>