<commit_before>/**
 * Created by <NAME>
 * @since  2016.09.08
 * @author <NAME> <<EMAIL>>
 * @link   http://chensoft.com
 */
#pragma once

#include <socket/bsd/bsd_address.hpp>
#include <socket/bsd/bsd_option.hpp>
#include <socket/bsd/bsd_setup.hpp>
#include <vector>

namespace chen
{
    namespace bsd
    {
        class socket
        {
        public:
            socket(socket_t fd);
            socket(int domain, int type, int protocol);

            socket(socket &&o);
            socket& operator=(socket &&o);

            ~socket();

        public:
            /**
             * Connect to remote address
             */
            chen::status connect(const bsd::address &addr);

            /**
             * Bind on specific address
             */
            chen::status bind(const bsd::address &addr);

            /**
             * Listen for request
             * @param backlog maximum queue length for pending connections
             */
            chen::status listen();
            chen::status listen(int backlog);

            /**
             * Accept new request and create a new socket
             */
            socket_t accept();

        public:
            /**
             * Send data to connected host, usually used in tcp
             */
            ssize_t send(const void *data, std::size_t size, int flags = 0);

            /**
             * Send data to specific host, usually used in udp
             */
            ssize_t send(const void *data, std::size_t size, const bsd::address &addr, int flags = 0);

            /**
             * Receive data from connected host, usually used in tcp
             */
            ssize_t recv(std::vector<std::uint8_t> &out, std::size_t size, int flags = 0);

            /**
             * Receive data from specific host, usually used in udp
             */
            ssize_t recv(std::vector<std::uint8_t> &out, std::size_t size, bsd::address &addr, int flags = 0);

        public:
            /**
             * Stop send or receive, but socket is still connected
             */
            void shutdownBoth();
            void shutdownRead();
            void shutdownWrite();

            /**
             * Close the socket, the socket will disconnect immediately
             */
            void close();

        public:
            /**
             * Local and remote address
             */
            bsd::address local() const;
            bsd::address remote() const;

            /**
             * Non-blocking status
             */
            bool nonblocking() const;
            bool nonblocking(bool enable);

            /**
             * Get/Set socket option
             * Usage:
             * >> auto opt = socket.option();
             * >> opt.reuseaddr(true);           // set option
             * >> bool reuse = opt.reuseaddr();  // get option
             */
            bsd::option option();

            /**
             * Check socket is valid
             */
            bool valid() const;
            operator bool() const;

            /**
             * Native socket handle
             */
            socket_t native() const;

        private:
            socket(const socket&) = delete;
            socket& operator=(const socket&) = delete;

        private:
            socket_t _fd = socket_t();  // socket descriptor
        };
    }
}<commit_msg>socket: treat -1 as invalid and init socket file descriptor<commit_after>/**
 * Created by <NAME>
 * @since  2016.09.08
 * @author <NAME> <<EMAIL>>
 * @link   http://chensoft.com
 */
#pragma once

#include <socket/bsd/bsd_address.hpp>
#include <socket/bsd/bsd_option.hpp>
#include <socket/bsd/bsd_setup.hpp>
#include <vector>

namespace chen
{
    namespace bsd
    {
        class socket
        {
        public:
            socket(socket_t fd);
            socket(int domain, int type, int protocol);

            socket(socket &&o);
            socket& operator=(socket &&o);

            ~socket();

        public:
            /**
             * Connect to remote address
             */
            chen::status connect(const bsd::address &addr);

            /**
             * Bind on specific address
             */
            chen::status bind(const bsd::address &addr);

            /**
             * Listen for request
             * @param backlog maximum queue length for pending connections
             */
            chen::status listen();
            chen::status listen(int backlog);

            /**
             * Accept new request and create a new socket
             */
            socket_t accept();

        public:
            /**
             * Send data to connected host, usually used in tcp
             */
            ssize_t send(const void *data, std::size_t size, int flags = 0);

            /**
             * Send data to specific host, usually used in udp
             */
            ssize_t send(const void *data, std::size_t size, const bsd::address &addr, int flags = 0);

            /**
             * Receive data from connected host, usually used in tcp
             */
            ssize_t recv(std::vector<std::uint8_t> &out, std::size_t size, int flags = 0);

            /**
             * Receive data from specific host, usually used in udp
             */
            ssize_t recv(std::vector<std::uint8_t> &out, std::size_t size, bsd::address &addr, int flags = 0);

        public:
            /**
             * Stop send or receive, but socket is still connected
             */
            void shutdownBoth();
            void shutdownRead();
            void shutdownWrite();

            /**
             * Close the socket, the socket will disconnect immediately
             */
            void close();

        public:
            /**
             * Local and remote address
             */
            bsd::address local() const;
            bsd::address remote() const;

            /**
             * Non-blocking status
             */
            bool nonblocking() const;
            bool nonblocking(bool enable);

            /**
             * Get/Set socket option
             * Usage:
             * >> auto opt = socket.option();
             * >> opt.reuseaddr(true);           // set option
             * >> bool reuse = opt.reuseaddr();  // get option
             */
            bsd::option option();

            /**
             * Check socket is valid
             */
            bool valid() const;
            operator bool() const;

            /**
             * Native socket handle
             */
            socket_t native() const;

        private:
            socket(const socket&) = delete;
            socket& operator=(const socket&) = delete;

        private:
            socket_t _fd = socket_t(-1);  // socket descriptor
        };
    }
}
{'fraction_non_alphanumeric': 0.1065166569257744, 'fraction_numerical': 0.004383401519579193, 'mean_word_length': 1.2710683477106834, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 35, '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': '4871356', 'n_tokens_mistral': 1764, 'n_tokens_neox': 1664, 'n_words': 515}