Sese Framework  2.3.0
A cross-platform framework
Loading...
Searching...
No Matches
sese::net::Socket Class Reference

Native Socket class. More...

#include <Socket.h>

Inheritance diagram for sese::net::Socket:
sese::io::Stream sese::io::PeekableStream sese::io::Closeable sese::io::InputStream sese::io::OutputStream sese::security::SecuritySocket

Public Types

enum class  Type { TCP = SOCK_STREAM , UDP = SOCK_DGRAM }
 Socket type. More...
 
enum class  Family { IPv4 = AF_INET , IPv6 = AF_INET6 }
 Address family. More...
 
enum class  ShutdownMode { READ = SHUT_RD , WRITE = SHUT_WR , BOTH = SHUT_RDWR }
 Shutdown mode. More...
 
using Ptr = std::shared_ptr<Socket>
 
- Public Types inherited from sese::io::Stream
typedef std::shared_ptr< StreamPtr
 
- Public Types inherited from sese::io::InputStream
typedef std::shared_ptr< InputStreamPtr
 
- Public Types inherited from sese::io::OutputStream
typedef std::shared_ptr< OutputStreamPtr
 
- Public Types inherited from sese::io::PeekableStream
typedef std::shared_ptr< PeekableStreamPtr
 

Public Member Functions

 Socket (Family family, Type type, int32_t protocol=IPPROTO_IP) noexcept
 
 Socket (socket_t handle, Address::Ptr address) noexcept
 
 ~Socket () noexcept override
 
virtual int32_t bind (Address::Ptr address) noexcept
 
virtual int32_t connect (Address::Ptr address) noexcept
 
virtual int32_t listen (int32_t backlog) const noexcept
 
virtual Socket::Ptr accept () const
 
virtual int32_t shutdown (ShutdownMode mode) const
 
bool setNonblocking () const noexcept
 
int64_t read (void *buffer, size_t length) override
 
int64_t write (const void *buffer, size_t length) override
 
int64_t send (void *buffer, size_t length, const IPAddress::Ptr &to, int32_t flags) const
 
int64_t recv (void *buffer, size_t length, const IPAddress::Ptr &from, int32_t flags) const
 
int64_t peek (void *buffer, size_t length) override
 
int64_t trunc (size_t length) override
 
int64_t writeInt16 (int16_t value)
 
int64_t writeInt32 (int32_t value)
 
int64_t writeInt64 (int64_t value)
 
int64_t writeUint16 (uint16_t value)
 
int64_t writeUint32 (uint32_t value)
 
int64_t writeUint64 (uint64_t value)
 
int64_t readInt16 (int16_t &value)
 
int64_t readInt32 (int32_t &value)
 
int64_t readInt64 (int64_t &value)
 
int64_t readUint16 (uint16_t &value)
 
int64_t readUint32 (uint32_t &value)
 
int64_t readUint64 (uint64_t &value)
 
void close () override
 
const socket_tgetRawSocket () const
 
const Address::PtrgetAddress () const
 
- Public Member Functions inherited from sese::io::InputStream
virtual ~InputStream () noexcept=default
 
- Public Member Functions inherited from sese::io::OutputStream
virtual ~OutputStream () noexcept=default
 
int64_t write (const std::string_view &buffer)
 
int64_t write (const text::StringView &buffer)
 
template<typename T >
int64_t write (std::vector< T > &buffer)
 
template<typename T , size_t N>
int64_t write (std::array< T, N > &buffer)
 
- Public Member Functions inherited from sese::io::PeekableStream
virtual ~PeekableStream ()=default
 
- Public Member Functions inherited from sese::io::Closeable
virtual ~Closeable ()=default
 

Static Public Member Functions

static socket_t socket (int family, int type, int protocol) noexcept
 
static int bind (socket_t socket, const sockaddr *addr, socklen_t addr_len) noexcept
 
static socket_t accept (socket_t socket, sockaddr *addr=nullptr, socklen_t *len=nullptr) noexcept
 
static int connect (socket_t socket, const sockaddr *addr, socklen_t addr_len) noexcept
 
static int64_t write (socket_t socket, const void *buffer, size_t len, int flags) noexcept
 
static int64_t read (socket_t socket, void *buffer, size_t len, int flags) noexcept
 
static int listen (socket_t socket, int backlog) noexcept
 
static int setNonblocking (socket_t socket) noexcept
 
static void close (socket_t socket) noexcept
 
static int shutdown (socket_t socket, ShutdownMode mode) noexcept
 

Private Attributes

socket_t handle {}
 
Address::Ptr address
 

Detailed Description

Native Socket class.

Member Typedef Documentation

◆ Ptr

using sese::net::Socket::Ptr = std::shared_ptr<Socket>

Member Enumeration Documentation

◆ Family

enum class sese::net::Socket::Family
strong

Address family.

Enumerator
IPv4 

IPv4.

IPv6 

IPv6.

◆ ShutdownMode

Shutdown mode.

Enumerator
READ 
WRITE 
BOTH 

◆ Type

enum class sese::net::Socket::Type
strong

Socket type.

Enumerator
TCP 

TCP socket.

UDP 

UDP socket.

Constructor & Destructor Documentation

◆ Socket() [1/2]

Socket::Socket ( Family family,
Type type,
int32_t protocol = IPPROTO_IP )
noexcept

◆ Socket() [2/2]

Socket::Socket ( socket_t handle,
Address::Ptr address )
noexcept

◆ ~Socket()

Socket::~Socket ( )
overridenoexcept

Member Function Documentation

◆ accept() [1/2]

Socket::Ptr Socket::accept ( ) const
nodiscardvirtual

◆ accept() [2/2]

sese::socket_t sese::net::Socket::accept ( socket_t socket,
sockaddr * addr = nullptr,
socklen_t * len = nullptr )
staticnoexcept

◆ bind() [1/2]

◆ bind() [2/2]

int sese::net::Socket::bind ( socket_t socket,
const sockaddr * addr,
socklen_t addr_len )
staticnoexcept

◆ close() [1/2]

◆ close() [2/2]

void sese::net::Socket::close ( socket_t socket)
staticnoexcept

References close().

◆ connect() [1/2]

int32_t Socket::connect ( Address::Ptr address)
virtualnoexcept

◆ connect() [2/2]

int sese::net::Socket::connect ( socket_t socket,
const sockaddr * addr,
socklen_t addr_len )
staticnoexcept

◆ getAddress()

const Address::Ptr & sese::net::Socket::getAddress ( ) const
inlinenodiscard

◆ getRawSocket()

const socket_t & sese::net::Socket::getRawSocket ( ) const
inlinenodiscard

◆ listen() [1/2]

int32_t Socket::listen ( int32_t backlog) const
virtualnoexcept

◆ listen() [2/2]

int sese::net::Socket::listen ( socket_t socket,
int backlog )
staticnoexcept

◆ peek()

int64_t Socket::peek ( void * buffer,
size_t length )
overridevirtual

Read content without stepping

Parameters
bufferBuffer
lengthBuffer size
Returns
Actual size read

Implements sese::io::PeekableStream.

References handle.

◆ read() [1/2]

int64_t sese::net::Socket::read ( socket_t socket,
void * buffer,
size_t len,
int flags )
staticnoexcept

◆ read() [2/2]

int64_t Socket::read ( void * buffer,
size_t length )
overridevirtual

TCP receive bytes

Parameters
bufferBuffer
lengthBuffer size
Returns
Actual number of bytes received

Implements sese::io::InputStream.

References handle.

Referenced by sese::iocp::v1::IOCPService::read(), and sese::service::TcpTransporter::read().

◆ readInt16()

int64_t sese::net::Socket::readInt16 ( int16_t & value)
inline

References FromBigEndian16(), and R.

◆ readInt32()

int64_t sese::net::Socket::readInt32 ( int32_t & value)
inline

◆ readInt64()

int64_t sese::net::Socket::readInt64 ( int64_t & value)
inline

◆ readUint16()

int64_t sese::net::Socket::readUint16 ( uint16_t & value)
inline

◆ readUint32()

int64_t sese::net::Socket::readUint32 ( uint32_t & value)
inline

◆ readUint64()

int64_t sese::net::Socket::readUint64 ( uint64_t & value)
inline

◆ recv()

int64_t Socket::recv ( void * buffer,
size_t length,
const IPAddress::Ptr & from,
int32_t flags ) const

UDP receive bytes

Parameters
bufferBuffer
lengthBuffer size
fromSource address
flagsFlags
Returns
Actual number of bytes received

References sese::net::Address::getRawAddress(), sese::net::Address::getRawAddressLength(), and handle.

◆ send()

int64_t Socket::send ( void * buffer,
size_t length,
const IPAddress::Ptr & to,
int32_t flags ) const

UDP send bytes

Parameters
bufferBuffer
lengthBuffer size
toDestination address
flagsFlags
Returns
Actual number of bytes sent

References sese::net::Address::getRawAddress(), sese::net::Address::getRawAddressLength(), and handle.

◆ setNonblocking() [1/2]

◆ setNonblocking() [2/2]

int sese::net::Socket::setNonblocking ( socket_t socket)
staticnoexcept

◆ shutdown() [1/2]

int32_t Socket::shutdown ( ShutdownMode mode) const
virtual

Reimplemented in sese::security::SecuritySocket.

References handle.

◆ shutdown() [2/2]

int sese::net::Socket::shutdown ( sese::socket_t socket,
ShutdownMode mode )
staticnoexcept

◆ socket()

sese::socket_t sese::net::Socket::socket ( int family,
int type,
int protocol )
staticnoexcept

◆ trunc()

int64_t Socket::trunc ( size_t length)
overridevirtual

Step only, without reading content

Parameters
lengthStep size
Returns
Actual step size

Implements sese::io::PeekableStream.

References handle.

◆ write() [1/2]

int64_t Socket::write ( const void * buffer,
size_t length )
overridevirtual

TCP send bytes

Parameters
bufferBuffer
lengthBuffer size
Returns
Actual number of bytes sent

Implements sese::io::OutputStream.

References handle.

Referenced by sese::iocp::v1::IOCPService::write(), and sese::service::TcpTransporter::write().

◆ write() [2/2]

int64_t sese::net::Socket::write ( socket_t socket,
const void * buffer,
size_t len,
int flags )
staticnoexcept

◆ writeInt16()

int64_t sese::net::Socket::writeInt16 ( int16_t value)
inline

References ToBigEndian16(), and W.

◆ writeInt32()

int64_t sese::net::Socket::writeInt32 ( int32_t value)
inline

◆ writeInt64()

int64_t sese::net::Socket::writeInt64 ( int64_t value)
inline

◆ writeUint16()

int64_t sese::net::Socket::writeUint16 ( uint16_t value)
inline

◆ writeUint32()

int64_t sese::net::Socket::writeUint32 ( uint32_t value)
inline

◆ writeUint64()

int64_t sese::net::Socket::writeUint64 ( uint64_t value)
inline

Member Data Documentation

◆ address

Address::Ptr sese::net::Socket::address
private

Referenced by accept().

◆ handle

socket_t sese::net::Socket::handle {}
private

The documentation for this class was generated from the following files: