<commit_before>import net/[Socket, Address, DNS, Exceptions]
import io/[Reader, Writer]
import berkeley into socket

/**
    A stream based socket interface.
 */
TCPSocket: class extends Socket {
    remote: SocketAddress

    /**
       Create a new socket to a given remote address

       :param remote: The address of the host to eventually connect to.
     */
    init: func ~addr(=remote) {
        super(remote family(), SocketType STREAM, 0)
    }

    /**
       Create a new socket to a given remote address with a specific
       file descriptor.

       :param remote: The address of the host to eventually connect to.
     */
    init: func ~addrDescriptor(=remote, .descriptor) {
        super(remote family(), SocketType STREAM, 0, descriptor)
    }

    /**
       Create a new socket to a given hostname and port number

       :param host: The hostname, for example 'localhost', or 'www.example.org'
       :param port: The port, for example 8080, or 80.
     */
    init: func ~hostAndPort(host: String, port: Int) {
        init(host, port, AddressFamily UNSPEC)
    }


    /**
       Create a new socket to a given hostname, port number and specific family

       :param host: The hostname, for example 'localhost', or 'www.example.org'
       :param port: The port, for example 8080, or 80.
       :param family: The port, for example AddressFamily IP4.
     */
    init: func ~family(host: String, port: Int, family: Int) {
        ip := DNS resolveOne(host, SocketType STREAM, family)
        init(SocketAddress new(ip, port))
    }

    /**
       Attempt to connect this socket to the remote host.

       :throws: A SocketError if something went wrong
     */
    connect: func {
        if(socket connect(descriptor, remote addr(), remote length()) == -1) {
            SocketError new() throw()
        }
        connected? = true
    }

    /**
       :return: A reader that reads data from this socket
     */
    reader: func -> TCPSocketReader {
        return TCPSocketReader new(this)
    }

    /**
       :return: A writer that writes data to this socket
     */
    writer: func -> TCPSocketWriter {
        return TCPSocketWriter new(this)
    }

    /**
       Send data through this socket
       :param data: The data to be sent
       :param length: The length of the data to be sent
       :param flags: Send flags
       :param resend: Attempt to resend any data left unsent

       :return: The number of bytes sent
     */
    send: func ~withLength(data: Char*, length: SizeT, flags: Int, resend: Bool) -> Int {
        bytesSent := socket send(descriptor, data, length, flags)

        if (resend)
            while(bytesSent < length && bytesSent != -1) {<commit_msg>Added TCPSocket connect(fn), for more awesomeness.<commit_after>import net/[Socket, Address, DNS, Exceptions]
import io/[Reader, Writer]
import berkeley into socket

/**
    A stream based socket interface.
 */
TCPSocket: class extends Socket {
    remote: SocketAddress

    /**
       Create a new socket to a given remote address

       :param remote: The address of the host to eventually connect to.
     */
    init: func ~addr(=remote) {
        super(remote family(), SocketType STREAM, 0)
    }

    /**
       Create a new socket to a given remote address with a specific
       file descriptor.

       :param remote: The address of the host to eventually connect to.
     */
    init: func ~addrDescriptor(=remote, .descriptor) {
        super(remote family(), SocketType STREAM, 0, descriptor)
    }

    /**
       Create a new socket to a given hostname and port number

       :param host: The hostname, for example 'localhost', or 'www.example.org'
       :param port: The port, for example 8080, or 80.
     */
    init: func ~hostAndPort(host: String, port: Int) {
        init(host, port, AddressFamily UNSPEC)
    }


    /**
       Create a new socket to a given hostname, port number and specific family

       :param host: The hostname, for example 'localhost', or 'www.example.org'
       :param port: The port, for example 8080, or 80.
       :param family: The port, for example AddressFamily IP4.
     */
    init: func ~family(host: String, port: Int, family: Int) {
        ip := DNS resolveOne(host, SocketType STREAM, family)
        init(SocketAddress new(ip, port))
    }

    /**
       Attempt to connect this socket to the remote host.

       :throws: A SocketError if something went wrong
     */
    connect: func {
        if(socket connect(descriptor, remote addr(), remote length()) == -1) {
            SocketError new() throw()
        }
        connected? = true
    }

    /**
       Attempt to connect this socket to the remote host, then runs f(),
       passing a TCPReaderWriterPair.

       :throws: A SocketError if something went wrong
     */
    connect: func ~withClosure (f: Func(TCPReaderWriterPair) -> Bool) {
        if(!connected?)
            connect()

        conn := TCPReaderWriterPair new(this)

        f(conn)
    }

    /**
       :return: A reader that reads data from this socket
     */
    reader: func -> TCPSocketReader {
        return TCPSocketReader new(this)
    }

    /**
       :return: A writer that writes data to this socket
     */
    writer: func -> TCPSocketWriter {
        return TCPSocketWriter new(this)
    }

    /**
       Send data through this socket
       :param data: The data to be sent
       :param length: The length of the data to be sent
       :param flags: Send flags
       :param resend: Attempt to resend any data left unsent

       :return: The number of bytes sent
     */
    send: func ~withLength(data: Char*, length: SizeT, flags: Int, resend: Bool) -> Int {
        bytesSent := socket send(descriptor, data, length, flags)

        if (resend)
            while(bytesSent < length && bytesSent != -1) {
{'fraction_non_alphanumeric': 0.0942524803284297, 'fraction_numerical': 0.005815942524803284, 'mean_word_length': 2.3015245623941274, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, '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': '5485620', 'n_tokens_mistral': 1661, 'n_tokens_neox': 1597, 'n_words': 649}