<commit_before><?php
namespace Phastlight\Module\NET;

class Socket extends \Phastlight\EventEmitter
{
    private $socket; //the underlying uv socket
    private $type; //the socket type
    private $shouldClose; //whether this socket should close or not

    public function setSocket($socket)
    {
        $this->socket = $socket; 
    }

    public function getSocket()
    {
        return $this->socket; 
    }

    public function setType($type)
    {
        $this->type = $type; 
    }

    public function getType()
    {
        return $this->type; 
    }

    public function setShouldClose($shouldClose)
    {
        $this->shouldClose = $shouldClose; 
    }

    public function getShouldClose()
    {
        return $this->shouldClose;
    }

    public function connect($port, $host, $connectionListener) 
    {
        $self = $this;
        $client = uv_tcp_init();
        uv_tcp_connect($client, uv_ip4_addr($host,$port), function($uvSocket, $stat) use ($self, $connectionListener, &$client) {
            $self->setType("tcp4");
            if ($stat == 0) {
                $self->setSocket($uvSocket);
                $self->emit("connect"); 
                if (is_callable($connectionListener)) {
                    $connectionListener(); 
                }

                //start reading data from server...
                uv_read_start($uvSocket, function($uvSocket, $nread, $buffer) use ($socket) {
                    $shouldClose = $socket->getShouldClose();
                    if ($shouldClose) {
                        uv_close($uvSocket); 
                        $self->emit('close');
                    }
                });

            }
        });

        return $this;
    }

    public function write($data, $callback = '')
    {
        if ($this->socket) {
            $self = $this;
            $socket = $this->socket;
            uv_write($socket, $data, function($uvSocket, $stat) use ($self,$callback) {
                if ($stat == 0) {
                    uv_read_start($uvSocket, function($uvSocket, $nread, $buffer) use ($self) {
                        if ($nread > 0) {
                            $self->emit('data', $buffer);
                        }
                        $self->emit('end'); //send the "end" event
                        if (is_callable($callback)) {
                            $callback(); 
                        }

                        //finally, see if we should close the socket or not
                        $shouldClose = $self->getShouldClose();
                        if ($shouldClose) {
                            uv_close($uvSocket); 
                            $self->emit('close');
                        }

                    });
                }

                //finally, see if we should close the socket or not, we need to do it again in case the socket is not closed in read callback
                $shouldClose = $self->getShouldClose();
                if ($shouldClose) {
                    uv_close($uvSocket); 
                    $self->emit('close');
                }

            });
        }  
    }

    public function end($data = null)
    {
        if ($data) {
            $this->write($data);
            $this->end();
        }
        else{
            $this->shouldClose = true; 
        }
    }
}<commit_msg>remove redundant socket apis<commit_after><?php
namespace Phastlight\Module\NET;

class Socket extends \Phastlight\EventEmitter
{
    private $connection;
    private $type; //the socket type
    private $shouldClose; //whether this socket should close or not

    public function setType($type)
    {
        $this->type = $type; 
    }

    public function getType()
    {
        return $this->type; 
    }

    public function connect($port, $host, $connectionListener) 
    {
        $self = $this;
        $this->connection = uv_tcp_init();
        $client = $this->connection;
        uv_tcp_connect($this->connection, uv_ip4_addr($host,$port), function($stream, $stat) use ($self, $connectionListener, &$client) {
            $self->setType("tcp4");
            if ($stat == 0) {
                $self->emit("connect"); 
                if (is_callable($connectionListener)) {
                    $connectionListener(); 
                }

                //start reading data from server...
                uv_read_start($client, function($stream, $nread, $buffer) use ($self) {
                    if ($nread > 0) { //we got some data from server
                        $self->emit('data', $buffer);
                    }
                    
                    $shouldClose = $self->getShouldClose();
                    if ($shouldClose) {
                        uv_close($stream); 
                        $self->emit('close');
                    }
                });

            }
        });

        return $this;
    }

    public function write($data, $callback = '')
    {
        if ($this->connection) {
            $self = $this;
            uv_write($this->connection, $data, function($stream, $stat) use ($callback) {
                if ($stat == 0) {
                    //data is written out, execute the callback
                    if (is_callable($callback)) {
                        $callback(); 
                    }
                }
            });
        }
    }

    public function end($data = null)
    {
        if ($data) {
            $this->write($data);
            $this->end();
        }
        else{
            $this->shouldClose = true; 
        }
    }

    private function getShouldClose()
    {
        return $this->shouldClose;
    }

}
{'fraction_non_alphanumeric': 0.12530973451327435, 'fraction_numerical': 0.0017699115044247787, 'mean_word_length': 1.3216926869350862, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, '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': '7642710', 'n_tokens_mistral': 1537, 'n_tokens_neox': 1411, 'n_words': 335}