Mercurial > hg > udpmsg3
comparison hub/hub.php @ 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 | dd81c38b513a |
children |
comparison
equal
deleted
inserted
replaced
1:7f01316130e8 | 2:7e342a0a3b74 |
---|---|
90 } | 90 } |
91 public function select_timeout() { | 91 public function select_timeout() { |
92 socket_delete($this); | 92 socket_delete($this); |
93 $this->socket = socket_create($this->ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, SOL_TCP); | 93 $this->socket = socket_create($this->ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, SOL_TCP); |
94 socket_set_nonblock($this->socket); | 94 socket_set_nonblock($this->socket); |
95 socket_set_option($this->socket, SOL_SOCKET, SO_REUSEADDR, 1); | |
95 if (!socket_bind($this->socket, $this->host, $this->port)) return FALSE; | 96 if (!socket_bind($this->socket, $this->host, $this->port)) return FALSE; |
96 if (!socket_listen($this->socket)) return FALSE; | 97 if (!socket_listen($this->socket)) return FALSE; |
97 socket_delete($this); | 98 socket_delete($this); |
98 socket_add_connected($this); | 99 socket_add_connected($this); |
99 fprintf(STDERR, "UDPMSG: Listening\n"); | |
100 return TRUE; | 100 return TRUE; |
101 } | 101 } |
102 public function select_read() { | 102 public function select_read() { |
103 $client = socket_accept($this->socket); | 103 $client = socket_accept($this->socket); |
104 if (!$client) return; | 104 if (!$client) return; |
148 } | 148 } |
149 } | 149 } |
150 class UDPPeer { | 150 class UDPPeer { |
151 public $socket = NULL; | 151 public $socket = NULL; |
152 function __construct($host = NULL, $port = NULL, $ipv6 = FALSE, $bindhost = NULL, $bindport = NULL) { | 152 function __construct($host = NULL, $port = NULL, $ipv6 = FALSE, $bindhost = NULL, $bindport = NULL) { |
153 $this->socket = socket_create($this->ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, SOL_TCP); | 153 $this->socket = socket_create($ipv6 ? AF_INET6 : AF_INET, SOCK_DGRAM, SOL_UDP); |
154 socket_set_nonblock($this->socket); | 154 socket_set_nonblock($this->socket); |
155 if ($bindhost || $bindport) socket_bind($this->socket, $bindhost, $bindport); | 155 if ($bindhost || $bindport) socket_bind($this->socket, $bindhost, $bindport); |
156 socket_add_connected($this); | 156 socket_add_connected($this); |
157 if ($host) { | 157 if ($host) { |
158 socket_connect($this->socket, $host, $port); | 158 socket_connect($this->socket, $host, $port); |
166 public function select_error() { | 166 public function select_error() { |
167 fprintf(STDERR, "UDPMSG: Error: select error.\n"); | 167 fprintf(STDERR, "UDPMSG: Error: select error.\n"); |
168 } | 168 } |
169 private function udpmsg_read() { | 169 private function udpmsg_read() { |
170 $msg = socket_read($this->socket, 1024 + 20); | 170 $msg = socket_read($this->socket, 1024 + 20); |
171 //$msg = ''; | |
172 //if (socket_recvfrom($this->socket, $msg, 1024 + 20, 0, $name, $port) <= 0) return FALSE; | |
171 if (!strlen($msg)) { | 173 if (!strlen($msg)) { |
172 fprintf(STDERR, "UDPMSG: End of file\n"); | 174 fprintf(STDERR, "UDPMSG: End of file\n"); |
173 return FALSE; | 175 return FALSE; |
174 } | 176 } |
175 if (strlen($msg) < 20 || substr($msg, 0, 20) != sha(substr($msg, 20), TRUE)) { | 177 if (strlen($msg) < 20 || substr($msg, 0, 20) != sha1(substr($msg, 20), TRUE)) { |
176 fprintf(STDERR, "UDPMSG: Short message or checksum mismatch\n"); | 178 fprintf(STDERR, "UDPMSG: Short message or checksum mismatch\n"); |
177 return FALSE; | 179 return FALSE; |
178 } | 180 } |
179 udpmsg_receive($this, $msg); | 181 udpmsg_receive($this, substr($msg, 20)); |
180 return TRUE; | 182 return TRUE; |
181 } | 183 } |
182 public function send($msg) { | 184 public function send($msg) { |
183 socket_write($this->socket, sha1($msg, TRUE).$msg); | 185 socket_write($this->socket, sha1($msg, TRUE).$msg); |
184 return TRUE; | 186 return TRUE; |
244 isset($e['ipv6']) && $e['ipv6'], | 246 isset($e['ipv6']) && $e['ipv6'], |
245 isset($e['bindhost']) ? $e['bindhost'] : NULL, | 247 isset($e['bindhost']) ? $e['bindhost'] : NULL, |
246 isset($e['bindport']) ? $e['bindport'] : NULL); | 248 isset($e['bindport']) ? $e['bindport'] : NULL); |
247 if (isset($config['udppeer'])) | 249 if (isset($config['udppeer'])) |
248 foreach ($config['udppeer'] as $e) | 250 foreach ($config['udppeer'] as $e) |
249 new StreamClient( | 251 new UDPPeer( |
250 isset($e['host']) ? $e['host'] : NULL, | 252 isset($e['host']) ? $e['host'] : NULL, |
251 isset($e['port']) ? $e['port'] : 15387, | 253 isset($e['port']) ? $e['port'] : 15387, |
252 isset($e['ipv6']) && $e['ipv6'], | 254 isset($e['ipv6']) && $e['ipv6'], |
253 isset($e['bindhost']) ? $e['bindhost'] : NULL, | 255 isset($e['bindhost']) ? $e['bindhost'] : NULL, |
254 isset($e['bindport']) ? $e['bindport'] : NULL); | 256 isset($e['bindport']) ? $e['bindport'] : NULL); |