<commit_before><?php

namespace Datagram;

/** @event message */
// interface similar to Stream
// TODO: implment EventEmitter only here?
class SocketReadable extends Socket
{
    public $bufferSize = 1500;

    public function pause()
    {
        $this->loop->removeReadStream($this->socket);
    }

    public function resume()
    {
        $this->loop->addReadStream($this->socket, array($this, 'onReceive'));
    }

    public function onReceive($message)
    {
        $data = stream_socket_recvfrom($this->socket, $this->bufferSize, 0, $peer);

        // create remote socket that does NOT have a dedicated readable method (see thie main DatagramSocket instead)
        $remote = new DatagramSocket($this->socket, $peer);

        $this->emit('message', array($data, $socket));
    }
}<commit_msg>Emit 'error' event when receiving fails<commit_after><?php

namespace Datagram;

/** @event message */
// interface similar to Stream
// TODO: implment EventEmitter only here?
class SocketReadable extends Socket
{
    public $bufferSize = 1500;

    public function pause()
    {
        $this->loop->removeReadStream($this->socket);
    }

    public function resume()
    {
        $this->loop->addReadStream($this->socket, array($this, 'onReceive'));
    }

    public function onReceive($message)
    {
        $data = stream_socket_recvfrom($this->socket, $this->bufferSize, 0, $peer);

        if ($data === false) {
            // receiving data failed => remote side rejected one of our packets
            // due to the nature of UDP, there's no way to tell which one exactly
            // $peer is not filled either

            // emit error message and local socket
            $this->emit('error', array(new \Exception('Invalid message'), $this));
            return;
        }

        // create remote socket that does NOT have a dedicated readable method (see thie main DatagramSocket instead)
        $remote = new Socket($this->loop, $this->socket, $peer);

        $this->emit('message', array($data, $remote));
    }
}
{'fraction_non_alphanumeric': 0.12805474095796676, 'fraction_numerical': 0.004887585532746823, 'mean_word_length': 2.7907407407407407, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 26, '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': '4709223', 'n_tokens_mistral': 574, 'n_tokens_neox': 547, 'n_words': 174}