Location: PHPKode > projects > Kbot > kbot_1.3/rconBots/includes/logComms.class.php
<?PHP 
/**
 *
 * $Id: logComms.class.php 117 2006-09-30 07:37:15Z khaless $
 * $Revision: 117 $
 * $Author: khaless $
 * $Date: 2006-09-30 17:37:15 +1000 (Sat, 30 Sep 2006) $
 * 
 * Copyright (c) 2005 Mathew Rodley <hide@address.com>
 * 
 * Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
// urgh messy and quick
class logComms
{
	private $srcAddr;
	private $srcPort;
	private $specificPort;
	private $ignoreIncomingPort;

	function setSpecificPort($port) {
		$this->specificPort = $port;
	}
	
	// sometimes games (or nc) will not send logs from the game port.
	// so only check ip.
	function incomingPortComparison($bool) {
		$this->ignoreIncomingPort = $bool;
	}

	function bind($address, $port)
	{
		$this->srcAddr = $address;
		$this->srcPort = $port;

		// rewriote this portion to use sockets to fix the firewall issue
		if(!$this->socket = socket_create (AF_INET, SOCK_DGRAM, getprotobyname("UDP"))) {
			return FALSE;
		} else {
			
			if(isset($this->specificPort)) {
				// attempt to bind to a specific port!
				$arrayPorts = array ($this->specificPort);
			}
			else {
				// this will bind to 1 of 10 ports
				$arrayPorts = array ('40000','40001','40002','40003','40004','40005','40006','40007','40008','40009','40010');
			}

			$i=0;
			while (!@socket_bind($this->socket,0,$arrayPorts[$i]))
			{
				$i++;
				if(is_null($arrayPorts[$i]))
				{
					socket_close($this->socket);
					return FALSE;
				}
			}
			socket_set_option($this->socket,SOL_SOCKET,SO_REUSEADDR,1);

			socket_set_nonblock ($this->socket);

			$this->connected = TRUE;
			$this->boundPort = $arrayPorts[$i];
			$this->boundAddress = $address;
			return true;
		}
		return false;
	}

	function send($command)
	{
		if(!socket_write($this->socket,$command,strlen($command))) {
			return FALSE;
		} else return TRUE;
	}

	// will read correctly and format teh shite of the log entry...
	function read()
	{
		@socket_recvfrom($this->socket, $buf, 65535, 0, $addr, $port);
		
		if($addr !=  $this->srcAddr || ($port != $this->srcPort && $this->ignoreIncomingPort !== true))
		return false;
		
		if($buf !== FALSE) {
			return $buf;
		}
		//return substr(@socket_read($this->socket,1024),4);
	}
	function close()
	{
		socket_close($this->socket);
	}
}
?>
Return current item: Kbot