libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
TcpServer Class Reference

TCP server that listens for incoming connections. More...

#include <tcpserver.h>

Inheritance diagram for TcpServer:
Collaboration diagram for TcpServer:

Public Member Functions

 TcpServer (ObjectBase *parent=nullptr)
 Constructs a TcpServer.
 
 ~TcpServer () override
 Destructor. Closes the server if listening.
 
Error listen (const SocketAddress &address, int backlog=50)
 Starts listening for connections.
 
void close ()
 Stops listening and closes the server socket.
 
bool isListening () const
 Returns true if the server is listening.
 
SocketAddress serverAddress () const
 Returns the address the server is listening on.
 
TcpSocketnextPendingConnection ()
 Returns the next accepted connection.
 
bool hasPendingConnections () const
 Returns true if there are pending connections.
 
Error waitForNewConnection (unsigned int timeoutMs=0)
 Blocks until a new connection arrives or timeout.
 
void setMaxPendingConnections (int count)
 Sets the maximum number of pending connections.
 
 PROMEKI_SIGNAL (newConnection)
 Emitted when a new connection is available.
 
- Public Member Functions inherited from ObjectBase
 ObjectBase (ObjectBase *p=nullptr)
 Default ObjectBase constructor.
 
virtual ~ObjectBase ()
 Destructor. Emits aboutToDestroy, detaches from parent, and destroys children.
 
ObjectBaseparent () const
 Returns the parent object, if one. nullptr if none.
 
void setParent (ObjectBase *p)
 Sets the parent of this object. If the object already has a parent, it will be removed as a child from the old parent and added as a child to the new one.
 
const ObjectBaseListchildList () const
 Returns a list of children of this object.
 
template<typename... Args>
int registerSlot (Slot< Args... > *slot)
 Registers a slot with this object and assigns it an ID.
 
EventLoopeventLoop () const
 Returns the EventLoop this object is affiliated with.
 
void moveToThread (EventLoop *loop)
 Changes the EventLoop affinity of this object.
 
int startTimer (unsigned int intervalMs, bool singleShot=false)
 Starts a timer on this object's EventLoop.
 
void stopTimer (int timerId)
 Stops a timer previously started with startTimer().
 
template<typename... Args>
PROMEKI_NAMESPACE_BEGIN void connect (Signal< Args... > *signal, Slot< Args... > *slot)
 

Additional Inherited Members

- Public Types inherited from ObjectBase
using SlotVariantFunc = std::function< void(const VariantList &)>
 Function type for invoking a slot with a list of Variants.
 
- Static Public Member Functions inherited from ObjectBase
static const MetaInfometaInfo ()
 Returns the MetaInfo for the ObjectBase class.
 
template<typename... Args>
static void connect (Signal< Args... > *signal, Slot< Args... > *slot)
 connects a signal and slot together. This function assumes both the signal and slot exist in a ObjectBase or derived object
 
- Public Attributes inherited from ObjectBase
Signal< ObjectBase * > aboutToDestroySignal = Signal< ObjectBase * >(this, aboutToDestroySignalName)
 
- Static Public Attributes inherited from ObjectBase
static constexpr const charaboutToDestroySignalName = PROMEKI_STRINGIFY( aboutToDestroy ) "(" PROMEKI_STRINGIFY_ARGS( ObjectBase * ) ")"
 
static SignalMeta aboutToDestroySignalMeta = SignalMeta(metaInfo(), aboutToDestroySignalName)
 
- Protected Member Functions inherited from ObjectBase
ObjectBasesignalSender ()
 Returns the ObjectBase that emitted the signal currently being handled.
 
virtual void event (Event *e)
 Called by EventLoop to deliver events to this object.
 
virtual void timerEvent (TimerEvent *e)
 Called when a timer fires for this object.
 

Detailed Description

TCP server that listens for incoming connections.

TcpServer binds to a local address and listens for incoming TCP connections. Accepted connections are returned as TcpSocket objects.

This class must only be used from the thread that created it (or moved to via moveToThread()).

Example
server.listen(SocketAddress::any(8080));
server.waitForNewConnection(5000);
TcpSocket *client = server.nextPendingConnection();
Dynamic array container wrapping std::vector.
Definition list.h:40
static SocketAddress any(uint16_t port)
Returns INADDR_ANY with the given port.
Definition socketaddress.h:71
TCP server that listens for incoming connections.
Definition tcpserver.h:36
Stream-oriented TCP socket.
Definition tcpsocket.h:35

Constructor & Destructor Documentation

◆ TcpServer()

TcpServer::TcpServer ( ObjectBase parent = nullptr)

Constructs a TcpServer.

Parameters
parentThe parent object, or nullptr.

Member Function Documentation

◆ hasPendingConnections()

bool TcpServer::hasPendingConnections ( ) const

Returns true if there are pending connections.

Returns
True if at least one connection is waiting.

◆ listen()

Error TcpServer::listen ( const SocketAddress address,
int  backlog = 50 
)

Starts listening for connections.

Parameters
addressThe local address and port to listen on.
backlogThe maximum pending connection queue length.
Returns
Error::Ok on success, or an error on failure.

◆ nextPendingConnection()

TcpSocket * TcpServer::nextPendingConnection ( )

Returns the next accepted connection.

The caller takes ownership of the returned TcpSocket. Returns nullptr if no pending connections.

Returns
A new TcpSocket for the accepted connection, or nullptr.

◆ PROMEKI_SIGNAL()

TcpServer::PROMEKI_SIGNAL ( newConnection  )

Emitted when a new connection is available.

Signal:

◆ setMaxPendingConnections()

void TcpServer::setMaxPendingConnections ( int  count)
inline

Sets the maximum number of pending connections.

Parameters
countThe maximum count.

◆ waitForNewConnection()

Error TcpServer::waitForNewConnection ( unsigned int  timeoutMs = 0)

Blocks until a new connection arrives or timeout.

Parameters
timeoutMsTimeout in milliseconds (0 = wait forever).
Returns
Error::Ok if a connection arrived, Error::Timeout on timeout.

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