ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
UdpSocket.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
13 
14 class UdpSocket
15 {
16  const DATAGRAM_MAX_LENGTH = 65023;
17 
18  public function __construct($ip, $port = 514)
19  {
20  $this->ip = $ip;
21  $this->port = $port;
22  $this->socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
23  }
24 
25  public function write($line, $header = "")
26  {
27  $this->send($this->assembleMessage($line, $header));
28  }
29 
30  public function close()
31  {
32  socket_close($this->socket);
33  }
34 
35  protected function send($chunk)
36  {
37  socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
38  }
39 
40  protected function assembleMessage($line, $header)
41  {
42  $chunkSize = self::DATAGRAM_MAX_LENGTH - strlen($header);
43 
44  return $header . substr($line, 0, $chunkSize);
45  }
46 }
$header