All pastes #297850 Raw Edit

Stuff

public php v1 · immutable
#297850 ·published 2006-12-31 10:24 UTC
rendered paste body
<?phpset_time_limit (0);/* EgoChat Remote Server * 28/Dec/06 */class EgoChat_Server{	// ** Public Variables		public $errors; // Stores errors		// ** Private Variables		private $debug = 1; // Debug Mode		private $socket; // Main Socket	private $server_enabled;	private $clients = array (); // Clients		// ** Private Functions		private function accept () // Accepts connection if avilable	{		$this-> RefrashClients();				$tmpSocket = socket_accept ($this-> socket); // Accepts new temporary socket		return $tmpSocket;	}		private function listen ($max_connections = 0) // Start listeing to new connections	{		socket_listen ($this-> socket, $max_connections);	}		private function init ($port, $max_connections = 0) // Initilaize for first run	{		$this-> socket = socket_create (AF_INET, SOCK_STREAM, SOL_TCP); // Creating new stream full-duplex TCP socket. IPv4		socket_set_option ($this-> socket, SOL_SOCKET, SO_REUSEADDR, 1);		socket_bind ($this-> socket, '0.0.0.0', $port);		$this-> listen (); // Starts the server	}		private function handle_new_socket ($socket) // Handles new socket	{			// Checks for ban and send error		// Checks for errors on socket.. version or anything and send error socket_close ($socket);				socket_getpeername ($socket, $client_ip); // Gets client ip		print "I have ". count ($this-> clients). " clients..\n\r";		$this-> RefrashClients();		require ('handler.php'); // Socket Handler		$temp = new EgoChat_Handler ($this, $socket, $pid, $client_ip) or die ("Could not create socket handler"); // New handler		$this-> AddClient ($temp);		$pid = pcntl_fork();		if ($pid != 0) 			$temp-> handle (); // Handle socket	}		private function wait () // Waits for new connection	{		$this-> server_enabled = true;		while ($this-> server_enabled)		{			$this-> RefrashClients();				$new_socket = $this-> accept (); // Accepts the socket			$this-> handle_new_socket ($new_socket);		}	}		// ** Public Functions		public function __construct ($port, $max_connections = 0)	{		$this-> init ($port, $max_connections); // Starts the server				print 'Server Started';		if ($this-> debug)			print " (Debug)";					print "\n\r";				$this-> wait (); // Waits for new connections	}		public function close () // Close server	{		$this-> server_enabled = false;		socket_shutdown ($this-> socket, 2);		socket_close ($this-> socket);	}		public function RefrashClients () // Remove old clients and refrash list	{		print "I have ". count ($this-> clients). " clients..\n\r";		foreach ($this-> clients as $index => $client) // Kills old clients		{			if ((!$client-> socket) || ($client-> socket == NULL))			{				print "Now i kill $index \n\r";				unset ($this-> clients [$index]);			}		}	}		public function SendToAll ($message) // Sends message to all clients	{		print "Now sending event to ". count ($this-> clients). " clients..\n\r";		foreach ($this-> clients as $index => $client)		{			print "sending to $index\n\r";			$client-> send ($message); // Sends message to client		}	}		public function AddError ($error) // Adds error	{		$this-> errors [date ("FjYgia")] = $error;				if ($this-> debug)			print $error;					print $error;	}		public function AddClient ($handle_obj) // Adds client (automatic by handler)	{		print "I have ". count ($this-> clients). " clients..\n\r";		$this-> clients [] = $handle_obj; // Adds client		print "Now i have ". count ($this-> clients). " clients..\n\r";	}}$temp = new EgoChat_Server (5123, 3); // Listen to port 5123?>