<commit_before><?php

namespace Sockets;

use Evenement\EventEmitter;
use Socket\Raw\Socket as RawSocket;

class Datagram extends EventEmitter
{
    private $socket;
    private $poller;
    private $buffer;
    private $bufferSize = 65536;

    public function __construct(RawSocket $socket, SelectPoller $poller)
    {
        $this->socket = $socket;
        $this->poller = $poller;

        $this->buffer = new DatagramBuffer($socket, $poller);

        $this->resume();
    }

    public function resume()
    {
        $this->poller->addReadSocket($this->socket->getResource(), array($this, 'handleRead'));
    }

    public function pause()
    {
        $this->poller->removeReadSocket($this->socket->getResource());
    }

    public function send($data, $remote)
    {
        return $this->buffer->send($data, $remote);
    }

    public function handleRead()
    {
        $data = $this->socket->rcvFrom($this->bufferSize, 0, $remote = null);

        if ($data === '') {
            $this->close();
        } else {
            $this->emit('message', array($data, $remote));
        }
    }

    public function close()
    {
        $this->emit('close', array($this));

        $this->pause();
        $this->buffer->close();
        $this->socket->close();

        $this->removeAllListeners();
    }
}<commit_msg>Add support for broadcasts<commit_after><?php

namespace Sockets;

use Evenement\EventEmitter;
use Socket\Raw\Socket as RawSocket;

class Datagram extends EventEmitter
{
    private $socket;
    private $poller;
    private $buffer;
    private $bufferSize = 65536;

    public function __construct(RawSocket $socket, SelectPoller $poller)
    {
        $this->socket = $socket;
        $this->poller = $poller;

        $this->buffer = new DatagramBuffer($socket, $poller);

        $this->resume();
    }

    public function resume()
    {
        $this->poller->addReadSocket($this->socket->getResource(), array($this, 'handleRead'));
    }

    public function pause()
    {
        $this->poller->removeReadSocket($this->socket->getResource());
    }

    public function send($data, $remote)
    {
        return $this->buffer->send($data, $remote);
    }

    public function handleRead()
    {
        $data = $this->socket->rcvFrom($this->bufferSize, 0, $remote = null);

        if ($data === '') {
            $this->close();
        } else {
            $this->emit('message', array($data, $remote));
        }
    }

    public function close()
    {
        $this->emit('close', array($this));

        $this->pause();
        $this->buffer->close();
        $this->socket->close();

        $this->removeAllListeners();
    }

    /**
     * enable/disable sending and receiving broacasts (by setting/unsetting SO_BROADCAST option)
     *
     * @param boolean $toggle
     * @return self $this (chainable)
     * @throws Exception on error
     * @uses RawSocket::setOption()
     */
    public function setOptionBroadcast($toggle = true)
    {
        $this->socket->setOption(SOL_SOCKET, SO_BROADCAST, (int)(bool)$toggle);
        return $this;
    }
}
{'fraction_non_alphanumeric': 0.1560786842953886, 'fraction_numerical': 0.003869719445340213, 'mean_word_length': 2.477578475336323, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1315031', 'n_tokens_mistral': 945, 'n_tokens_neox': 916, 'n_words': 190}