10Duke Scale C++ Client
Loading...
Searching...
No Matches
tenduke::osa::Socket Class Referenceabstract

#include <Socket.h>

Detailed Description

OS-independent abstraction of a socket.

NOTE: Construction of Socket might throw tenduke::net::NetworkingException

Inheritance diagram for tenduke::osa::Socket:
tenduke::osa::AbstractBaseSocket tenduke::test::fakes::FakeSocket tenduke::test::mocks::SocketMock

Public Member Functions

virtual std::unique_ptr< Socketaccept (uint32_t timeoutMs)=0
 Accepts next connection from listening socket, with a timeout.
 
virtual std::unique_ptr< Socketaccept ()=0
 Accepts next connection from listening socket.
 
virtual void listen ()=0
 Configures the socket to listen for incoming connections.
 
virtual int read (void *buffer, std::size_t bufferSize)=0
 Reads bytes from the socket.
 
virtual int write (const void *buffer, std::size_t len)=0
 Write bytes to the socket.
 
virtual std::uint16_t getPort () const =0
 Returns port of the socket.
 
virtual void setNonBlocking ()=0
 Sets the socket to non-blocking mode.
 

Member Function Documentation

◆ accept() [1/2]

virtual std::unique_ptr< Socket > tenduke::osa::Socket::accept ( )
pure virtual

Accepts next connection from listening socket.

No timeout.

Returns
the child socket

Implemented in tenduke::test::fakes::FakeSocket.

◆ accept() [2/2]

virtual std::unique_ptr< Socket > tenduke::osa::Socket::accept ( uint32_t  timeoutMs)
pure virtual

Accepts next connection from listening socket, with a timeout.

The socket must be set to listening mode using listen() before calling this method. New connection is returned as new tenduke::osa::Socket. This method times out after timeoutMs milliseconds and then returns nullptr.

IMPORTANT NOTE: When using timeout, you should call setNonBlocking() on the socket to avoid race conditions.

Parameters
timeoutMstimeout in milliseconds. Zero timeout is not yet supported.
Returns
the child socket or nullptr if accept timed out
Exceptions
tenduke::net::NetworkingExceptionin any case of error

Implemented in tenduke::osa::AbstractBaseSocket, and tenduke::test::fakes::FakeSocket.

◆ getPort()

virtual std::uint16_t tenduke::osa::Socket::getPort ( ) const
pure virtual

Returns port of the socket.

Returns
the port number

Implemented in tenduke::test::fakes::FakeSocket.

◆ listen()

virtual void tenduke::osa::Socket::listen ( )
pure virtual

Configures the socket to listen for incoming connections.

Exceptions
tenduke::net::NetworkingExceptionin any case of error

Implemented in tenduke::osa::AbstractBaseSocket, and tenduke::test::fakes::FakeSocket.

◆ read()

virtual int tenduke::osa::Socket::read ( void *  buffer,
std::size_t  bufferSize 
)
pure virtual

Reads bytes from the socket.

Parameters
bufferbuffer where the bytes are placed to
bufferSizesize of the buffer in bytes
Returns
number of bytes read. Returns -1, if socket is non-blocking and nothing to read yet.
Exceptions
tenduke::net::NetworkingExceptionin any case of error

Implemented in tenduke::osa::AbstractBaseSocket, and tenduke::test::fakes::FakeSocket.

◆ setNonBlocking()

virtual void tenduke::osa::Socket::setNonBlocking ( )
pure virtual

Sets the socket to non-blocking mode.

Exceptions
tenduke::net::NetworkingExceptionin any case of error

Implemented in tenduke::test::fakes::FakeSocket.

◆ write()

virtual int tenduke::osa::Socket::write ( const void *  buffer,
std::size_t  len 
)
pure virtual

Write bytes to the socket.

Parameters
bufferthese bytes are written
lennumber of bytes to write
Returns
number of bytes written
Exceptions
tenduke::net::NetworkingExceptionin any case of error

Implemented in tenduke::osa::AbstractBaseSocket, and tenduke::test::fakes::FakeSocket.


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