changeset 2:7e342a0a3b74 draft default tip

Fixed UDP mode in hub
author Ivo Smits <Ivo@UCIS.nl>
date Sat, 04 May 2013 00:36:14 +0200
parents 7f01316130e8
children
files client/debugclient.php hub/hub.php
diffstat 2 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/client/debugclient.php	Fri Apr 01 17:02:41 2011 +0200
+++ b/client/debugclient.php	Sat May 04 00:36:14 2013 +0200
@@ -24,7 +24,8 @@
    authors and should not be interpreted as representing official policies, either expressed
    or implied, of Ivo Smits.*/
 
-$server = '127.0.0.1:15387';
+if ($_SERVER['argc'] >= 2) $server = $_SERVER['argv'][1];
+else $server = '127.0.0.1:15387';
 
 $socket = stream_socket_client('tcp://'.$server) or die("Could not create socket\n");
 stream_set_timeout($socket, -1);
--- a/hub/hub.php	Fri Apr 01 17:02:41 2011 +0200
+++ b/hub/hub.php	Sat May 04 00:36:14 2013 +0200
@@ -92,11 +92,11 @@
 		socket_delete($this);
 		$this->socket = socket_create($this->ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, SOL_TCP);
 		socket_set_nonblock($this->socket);
+		socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1);
 		if (!socket_bind($this->socket, $this->host, $this->port)) return FALSE;
 		if (!socket_listen($this->socket)) return FALSE;
 		socket_delete($this);
 		socket_add_connected($this);
-		fprintf(STDERR, "UDPMSG: Listening\n");
 		return TRUE;
 	}
 	public function select_read() {
@@ -150,7 +150,7 @@
 class UDPPeer {
 	public $socket = NULL;
 	function __construct($host = NULL, $port = NULL, $ipv6 = FALSE, $bindhost = NULL, $bindport = NULL) {
-		$this->socket = socket_create($this->ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, SOL_TCP);
+		$this->socket = socket_create($ipv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, SOL_UDP);
 		socket_set_nonblock($this->socket);
 		if ($bindhost || $bindport) socket_bind($this->socket, $bindhost, $bindport);
 		socket_add_connected($this);
@@ -168,15 +168,17 @@
 	}
 	private function udpmsg_read() {
 		$msg = socket_read($this->socket, 1024 + 20);
+		//$msg = '';
+		//if (socket_recvfrom($this->socket, $msg, 1024 + 20, 0, $name, $port) <= 0) return FALSE;
 		if (!strlen($msg)) {
 			fprintf(STDERR, "UDPMSG: End of file\n");
 			return FALSE;
 		}
-		if (strlen($msg) < 20 || substr($msg, 0, 20) != sha(substr($msg, 20), TRUE)) {
+		if (strlen($msg) < 20 || substr($msg, 0, 20) != sha1(substr($msg, 20), TRUE)) {
 			fprintf(STDERR, "UDPMSG: Short message or checksum mismatch\n");
 			return FALSE;
 		}
-		udpmsg_receive($this, $msg);
+		udpmsg_receive($this, substr($msg, 20));
 		return TRUE;
 	}
 	public function send($msg) {
@@ -246,7 +248,7 @@
 			isset($e['bindport']) ? $e['bindport'] : NULL);
 if (isset($config['udppeer']))
 	foreach ($config['udppeer'] as $e)
-		new StreamClient(
+		new UDPPeer(
 			isset($e['host']) ? $e['host'] : NULL,
 			isset($e['port']) ? $e['port'] : 15387,
 			isset($e['ipv6']) && $e['ipv6'],