Skip to content

src/socket.hpp

Functions

Name
void socket_setup()
initialize platform specific network module
void socket_teardown()
release platform specific network module
uint32_t socket_recent()
network related error
bool socket_is_valid(int64_t sd)
check if given socket descriptor is valid
uint32_t socket_create(const addrinfo & hint, int64_t & sd)
create 1 socket
uint32_t socket_close(int64_t sd)
dispose the given socket
uint32_t socket_bind(int64_t sd, const sockaddr_in & local)
bind the socket to given address
uint32_t socket_bind(int64_t sd, const sockaddr_in6 & local)
bind the socket to given address
uint32_t socket_listen(int64_t sd)
start listening with the socket
uint32_t socket_connect(int64_t sd, const sockaddr_in & remote)
try connect to given endpoint(IPv4)
uint32_t socket_connect(int64_t sd, const sockaddr_in6 & remote)
try connect to given endpoint(IPv6)
uint32_t socket_accept(int64_t ln, int64_t & sd)
accept a connection request and return client socket
uint32_t socket_get_name(int64_t sd, sockaddr_in & local)
get the socket's address
uint32_t socket_get_name(int64_t sd, sockaddr_in6 & local)
get the socket's address
uint32_t socket_get_peer(int64_t sd, sockaddr_in & remote)
get connected peer's address
uint32_t socket_get_peer(int64_t sd, sockaddr_in6 & remote)
get connected peer's address
uint32_t socket_set_option(int64_t sd, int64_t level, int64_t option, int64_t value)
change the socket's option
uint32_t socket_set_option_nonblock(int64_t sd)
make socket to operate in non-blocking mode
uint32_t socket_set_option_reuse_address(int64_t sd)
make socket to reuse address
uint32_t socket_set_option_nodelay(int64_t sd)
make tcp send without delay
uint32_t socket_set_option_send_timout(int64_t sd, uint32_t us)
set the socket's send timeout
uint32_t socket_set_option_recv_timout(int64_t sd, uint32_t us)
set the socket's recv timeout
bool socket_would_block(uint32_t ec)
test if the error code is for non-blocking

Functions Documentation

function socket_setup

void socket_setup()

initialize platform specific network module

Exceptions:

  • std::system_error

function socket_teardown

void socket_teardown()

release platform specific network module

function socket_recent

uint32_t socket_recent()

network related error

Return: error code from errno or WSAGetLastError

function socket_is_valid

bool socket_is_valid(
    int64_t sd
)

check if given socket descriptor is valid

Parameters:

  • sd

Return:

  • true the sd is considered valid
  • false negative or INVALID_SOCKET

function socket_create

uint32_t socket_create(
    const addrinfo & hint,
    int64_t & sd
)

create 1 socket

Parameters:

  • hint family, type, protocol
  • sd reference to save the new socket

Return: error code from errno or WSAGetLastError

function socket_close

uint32_t socket_close(
    int64_t sd
)

dispose the given socket

Parameters:

  • sd

Return: error code from errno or WSAGetLastError

function socket_bind

uint32_t socket_bind(
    int64_t sd,
    const sockaddr_in & local
)

bind the socket to given address

Parameters:

  • sd socket to bind
  • local IPv4 address

Return: error code from errno or WSAGetLastError

function socket_bind

uint32_t socket_bind(
    int64_t sd,
    const sockaddr_in6 & local
)

bind the socket to given address

Parameters:

  • sd socket to bind
  • local IPv6 address

Return: error code from errno or WSAGetLastError

function socket_listen

uint32_t socket_listen(
    int64_t sd
)

start listening with the socket

Parameters:

  • sd socket to start listen

Return: error code from errno or WSAGetLastError

The backlog value is fixed to 7.

function socket_connect

uint32_t socket_connect(
    int64_t sd,
    const sockaddr_in & remote
)

try connect to given endpoint(IPv4)

Parameters:

  • sd socket to start connect
  • remote IPv4 address

Return: error code from errno or WSAGetLastError

function socket_connect

uint32_t socket_connect(
    int64_t sd,
    const sockaddr_in6 & remote
)

try connect to given endpoint(IPv6)

Parameters:

  • sd socket to start connect
  • remote IPv6 address

Return: error code from errno or WSAGetLastError

function socket_accept

uint32_t socket_accept(
    int64_t ln,
    int64_t & sd
)

accept a connection request and return client socket

Parameters:

  • ln listener socket
  • sd reference to descriptor to save the new connected socket

Return: error code from errno or WSAGetLastError

function socket_get_name

uint32_t socket_get_name(
    int64_t sd,
    sockaddr_in & local
)

get the socket's address

Parameters:

  • sd socket to query the bound address
  • local object to receive IPv4 address

Return: error code from errno or WSAGetLastError

function socket_get_name

uint32_t socket_get_name(
    int64_t sd,
    sockaddr_in6 & local
)

get the socket's address

Parameters:

  • sd socket to query the bound address
  • local object to receive IPv6 address

Return: error code from errno or WSAGetLastError

function socket_get_peer

uint32_t socket_get_peer(
    int64_t sd,
    sockaddr_in & remote
)

get connected peer's address

Parameters:

  • sd socket to query the peer address
  • remote object to receive IPv4 address

Return: error code from errno or WSAGetLastError

function socket_get_peer

uint32_t socket_get_peer(
    int64_t sd,
    sockaddr_in6 & remote
)

get connected peer's address

Parameters:

  • sd socket to query the peer address
  • remote object to receive IPv6 address

Return: error code from errno or WSAGetLastError

function socket_set_option

uint32_t socket_set_option(
    int64_t sd,
    int64_t level,
    int64_t option,
    int64_t value
)

change the socket's option

Parameters:

  • sd
  • level
  • option
  • value

Return: error code from errno or WSAGetLastError

function socket_set_option_nonblock

uint32_t socket_set_option_nonblock(
    int64_t sd
)

make socket to operate in non-blocking mode

Return: error code from errno or WSAGetLastError

function socket_set_option_reuse_address

uint32_t socket_set_option_reuse_address(
    int64_t sd
)

make socket to reuse address

Return: error code from errno or WSAGetLastError

function socket_set_option_nodelay

uint32_t socket_set_option_nodelay(
    int64_t sd
)

make tcp send without delay

Return: error code from errno or WSAGetLastError

function socket_set_option_send_timout

uint32_t socket_set_option_send_timout(
    int64_t sd,
    uint32_t us
)

set the socket's send timeout

Parameters:

  • sd socket to change timeout
  • us microsecond fot the duration

Return: error code from errno or WSAGetLastError

function socket_set_option_recv_timout

uint32_t socket_set_option_recv_timout(
    int64_t sd,
    uint32_t us
)

set the socket's recv timeout

Parameters:

  • sd socket to change timeout
  • us microsecond fot the duration

Return: error code from errno or WSAGetLastError

function socket_would_block

bool socket_would_block(
    uint32_t ec
)

test if the error code is for non-blocking

Source code

#pragma once

#include <cstdint>

struct addrinfo;

struct sockaddr_in;
struct sockaddr_in6;

void socket_setup() noexcept(false);

void socket_teardown() noexcept;

uint32_t socket_recent() noexcept;

bool socket_is_valid(int64_t sd) noexcept;

uint32_t socket_create(const addrinfo& hint, int64_t& sd) noexcept;

uint32_t socket_close(int64_t sd) noexcept;

uint32_t socket_bind(int64_t sd, const sockaddr_in& local) noexcept;

uint32_t socket_bind(int64_t sd, const sockaddr_in6& local) noexcept;

uint32_t socket_listen(int64_t sd) noexcept;

uint32_t socket_connect(int64_t sd, const sockaddr_in& remote) noexcept;

uint32_t socket_connect(int64_t sd, const sockaddr_in6& remote) noexcept;

uint32_t socket_accept(int64_t ln, int64_t& sd) noexcept;

uint32_t socket_get_name(int64_t sd, sockaddr_in& local) noexcept;

uint32_t socket_get_name(int64_t sd, sockaddr_in6& local) noexcept;

uint32_t socket_get_peer(int64_t sd, sockaddr_in& remote) noexcept;

uint32_t socket_get_peer(int64_t sd, sockaddr_in6& remote) noexcept;

uint32_t socket_set_option(int64_t sd, int64_t level, //
                           int64_t option, int64_t value) noexcept;

uint32_t socket_set_option_nonblock(int64_t sd) noexcept;

uint32_t socket_set_option_reuse_address(int64_t sd) noexcept;

uint32_t socket_set_option_nodelay(int64_t sd) noexcept;

uint32_t socket_set_option_send_timout(int64_t sd, //
                                       uint32_t us) noexcept;

uint32_t socket_set_option_recv_timout(int64_t sd, //
                                       uint32_t us) noexcept;

bool socket_would_block(uint32_t ec) noexcept;

Updated on 2023-06-05 at 18:28:33 +0900