All pastes #353617 Raw Edit

Mine

public text v1 · immutable
·published 2007-02-13 13:52 UTC
rendered paste body
<?php

/**
 * Enter description here...
 *
 */
class Tcp {

	/**
	 * Enter description here...
	 *
	 */
	protected function __construct(){
		
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $ip
	 * @param unknown_type $port
	 * @return unknown
	 */
	static public function server($ip, $port){
		$sock = stream_socket_server("tcp://$ip:$port");
		if ($sock) {
			return new TcpServer($sock);
		}else{
			return null;
		}
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $ip
	 * @param unknown_type $port
	 * @return unknown
	 */
	static public function client($ip, $port){
		$sock = stream_socket_client("tcp://$ip:$port");
		if ($sock) {
			return new TcpConnection($sock);
		} else {
			return null;
		}
	}

}

/**
 * Enter description here...
 *
 */
class TcpServer {

	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	protected $hook = null;
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	protected $connected = false;
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	protected $sock = null;

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $sock
	 */
	public function __construct($sock){
		$this->connected = true;
		Gtk::io_add_watch($sock,Gtk::IO_IN,array($this,"socket_accept"));
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $sock
	 * @param unknown_type $conditions
	 * @return unknown
	 */
	public function socket_accept(&$sock, $conditions){
		$connection = new TcpConnection(stream_socket_accept($sock));
		$this->trigger_hook($connection);
		return $this->connected;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $hook
	 * @return unknown
	 */
	public function set_accept_hook($hook){
		$oldhook = $this->hook;
		$this->hook = $hook;
		return $oldhook;
	}
	
	/**
	 * Enter description here...
	 *
	 * @return unknown
	 */
	public function get_accept_hook(){
		return $this->hook;
	}

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $connection
	 */
	protected function trigger_hook(&$connection){
		if ($this->hook) {
			call_user_func_array($this->hook,array($connection));
		}
	}
	
	/**
	 * Enter description here...
	 *
	 */
	protected function __clone(){

	}

}

/**
 * Enter description here...
 *
 */
class TcpConnection {
	
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	protected $hook = null;
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	protected $sock = null;
	/**
	 * Enter description here...
	 *
	 * @var unknown_type
	 */
	protected $connected = false;

	/**
	 * Enter description here...
	 *
	 * @param unknown_type $sock
	 */
	public function __construct(&$sock){
		$this->connected = true;
		$this->sock = $sock;
		Gtk::io_add_watch($sock,Gtk::IO_IN,array($this,"data_in"));
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $sock
	 * @param unknown_type $conditions
	 * @return unknown
	 */
	public function data_in(&$sock, $conditions){
		if (!$this->connected) return false;
		if ($this->hook) {
			call_user_func_array($this->hook, array($this->sock,$this));
		}
		return $this->connected;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $data
	 * @return unknown
	 */
	public function write($data){
		echo "writing : $data\n";
		return fwrite($this->sock, $data, strlen($data));
	}
	
	/**
	 * Enter description here...
	 *
	 */
	public function close(){
		$this->connected = false;
		fclose($this->sock);
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $hook
	 * @return unknown
	 */
	public function set_receive_hook($hook){
		$oldhook = $this->hook;
		$this->hook = $hook;
		return $oldhook;
	}
	
}

?>