Cogs.Core
Classes | Public Member Functions | Protected Member Functions | Private Attributes | List of all members
Cogs::Network::ConnectionHub Class Reference

A ConnectionHub combines a TCP connection with a MessageHub to enable the forwarding and receiving of messages over a network connection. More...

#include <ConnectionHub.h>

Inheritance diagram for Cogs::Network::ConnectionHub:
Cogs::Network::ConnectionTCP Cogs::Network::MessageHub Cogs::Network::ConnectionBase

Classes

struct  Header
 

Public Member Functions

 ConnectionHub (uint32_t id)
 
 ConnectionHub (Socket socket, const SockaddrIn &addr)
 
virtual bool processOutgoing () override
 
virtual bool processIncoming () override
 Manages accepting incoming connections when this connection is a listener.
 
- Public Member Functions inherited from Cogs::Network::ConnectionTCP
 ConnectionTCP (Socket socket, const SockaddrIn &addr)
 Constructs a connection that is attached to the specified socket.
 
virtual ~ConnectionTCP ()
 Destroys this connection.
 
SocketType type () const override
 
IPProto proto () const override
 
void enableAutoReconnect ()
 
void disableAutoReconnect ()
 
virtual bool isAutoConnectEnabled () const override
 
virtual bool isConnected () const override
 
void setConnectTimeout (int64_t milliseconds)
 
void setNoDelay (bool val=true)
 Set the TCP_NODELAY flag (Enable / Disables Nagles algorithm).
 
void shutdown ()
 Shutdown this connection.
 
virtual bool connect (const SockaddrIn &addr)
 Sets up this connection ready to connect to the specified remote address and port.
 
virtual bool reconnect () override
 Reconnects this connection to the remote host previously specified through a call to connect.
 
virtual bool disconnect () override
 Disconnects this connection.
 
bool listen (uint16_t port)
 
bool listen (AddrIn ip, uint16_t port)
 Sets this connection up as a listener that will listen for and respond to incoming connection requests on the specified port.
 
virtual bool processIncoming () override
 Manages accepting incoming connections when this connection is a listener.
 
virtual ConnectionTCPaccept (Socket, const SockaddrIn &)
 
- Public Member Functions inherited from Cogs::Network::ConnectionBase
Family family () const
 
virtual SocketType type () const =0
 
virtual IPProto proto () const =0
 
Socket getSocket () const
 
void remove ()
 
void removeAndDelete ()
 
const SockaddrIngetAddress () const
 
int64_t getReconnectTime () const
 
virtual bool isAutoConnectEnabled () const
 
virtual bool isConnected () const
 
bool shouldRemove () const
 
bool shouldDelete () const
 
void setNonblocking (bool val=true)
 Sets socket non-blocking mode to val.
 
void setDoNotProcess ()
 
virtual bool reconnect ()
 
virtual bool disconnect ()=0
 
bool bind (uint16_t port)
 
virtual bool bind (AddrIn ip, uint16_t port)
 Sets this connection up as a listener that will listen for and respond to incoming connection requests on the specified port.
 
bool send (const void *data, uint64_t byteCount, uint64_t &bytesSent)
 Attempts to send as much of the specified data as possible.
 
bool receive (void *buffer, uint64_t byteCount, uint64_t &bytesReceived)
 Attempts to read the specified number of bytes from the network socket this connection represents.
 
bool sendTo (const void *data, uint64_t byteCount, uint64_t &bytesSent, const SockaddrIn &addr)
 Attempts to send as much of the specified data as possible.
 
bool recvFrom (void *buffer, uint64_t byteCount, uint64_t &bytesReceived, SockaddrIn &addr)
 Attempts to read the specified number of bytes from the network socket this connection represents.
 
virtual bool processOutgoing ()
 
virtual bool processIncoming ()
 
- Public Member Functions inherited from Cogs::Network::MessageHub
 MessageHub (uint32_t ident=0)
 Constructs a new MessageHub instance and adds it to the global hub list.
 
virtual ~MessageHub ()
 Removes this MessageHub from the global list and cleans up all connections to and from this hub.
 
bool addListener (MessageHub *hub, bool bidirectional=false)
 Adds the specified hub as a listener to this hub.
 
bool addListener (uint32_t hubid, bool bidirectional=false)
 Attempts to add the hub with the given ID as a listener to this hub.
 
bool listenTo (MessageHub *hub, bool bidirectional=false)
 Sets this MessageHub up to listen for messages from the specified hub.
 
bool listenTo (uint32_t hubid, bool bidirectional=false)
 Sets up this MessageHub to listen for messages from the hub with the specified ID (if a matching hub can be found).
 
bool removeListener (MessageHub *hub)
 Removes the specified hub as a listener to this hub.
 
bool removeListener (uint32_t hubid)
 Removes the hub with the given ID as a listener to this hub.
 
void removeAllListeners ()
 Removes all listeners from this hub.
 
void disconnectFromSender (uint32_t hubid)
 Disconnects this hub from the specified hub to which it is listening.
 
void disconnectFromAllSenders ()
 Disconnects this hub from all hubs to which it is listening.
 
virtual void broadcastMessage (const Message::Ptr &message)
 Broadcast message to all hubs.
 
virtual void sendMessage (const Message::Ptr &message)
 Send message to all hubs listening to this one.
 
virtual void queueMessage (const Message::Ptr &message)
 Queue a message for this hub to process.
 
virtual bool processMessages (size_t limit=noLimit)
 Process all queued messages for this hub.
 
virtual bool isSendOnly () const
 
uint32_t getID () const
 
size_t getNoOfQueuedMessages ()
 Retrieves the number of messages currently awaiting processing by this hub.
 
void flushQueuedMessages ()
 Flushes all pending messages from this hub without processing them.
 

Protected Member Functions

virtual void handleReceivedMessage (const Message::Ptr &message)
 
virtual bool disconnect () override
 Called in response to a problem with the network, or a request by the application to disconnect.
 
- Protected Member Functions inherited from Cogs::Network::ConnectionBase
void create ()
 Internal wrapper for the socket() call.
 
void close ()
 Internal wrapper for closing the socket.
 
- Protected Member Functions inherited from Cogs::Network::MessageHub
Message::Ptr getNextMessage ()
 Retrieves the next pending message.
 

Private Attributes

Message::Ptr outgoingMessage
 
uint64_t bytesSent = 0
 
Message::Ptr incomingMessage
 
Header incomingHeader
 

Additional Inherited Members

- Public Types inherited from Cogs::Network::MessageHub
using MessageQueue = std::deque< Message::Ptr >
 
using HubList = std::vector< MessageHub * >
 
- Static Public Attributes inherited from Cogs::Network::MessageHub
static constexpr size_t noLimit = 10000000
 
- Protected Attributes inherited from Cogs::Network::ConnectionBase
Cogs::Atomic< uint32_t > flags = 0
 
int64_t reconnectTime = 0
 
Socket socket = InvalidSocket
 
SockaddrIn sockAddr = {}
 
- Static Protected Attributes inherited from Cogs::Network::ConnectionBase
static constexpr uint32_t cRemove = 0x00000001
 
static constexpr uint32_t cDelete = 0x00000002
 
static constexpr uint32_t cDoNotProcess = 0x00000004
 

Detailed Description

A ConnectionHub combines a TCP connection with a MessageHub to enable the forwarding and receiving of messages over a network connection.

Queued messages in the hub will be transmitted over the network. Received messages will be forwarded to local listeners for processing.

Definition at line 17 of file ConnectionHub.h.

Constructor & Destructor Documentation

◆ ConnectionHub() [1/2]

Cogs::Network::ConnectionHub::ConnectionHub ( uint32_t  id)
inline

Definition at line 19 of file ConnectionHub.h.

◆ ConnectionHub() [2/2]

Cogs::Network::ConnectionHub::ConnectionHub ( Socket  socket,
const SockaddrIn addr 
)
inline

Definition at line 20 of file ConnectionHub.h.

Member Function Documentation

◆ disconnect()

bool Cogs::Network::ConnectionHub::disconnect ( )
overrideprotectedvirtual

Called in response to a problem with the network, or a request by the application to disconnect.

Resets the state of this hub, before calling ConnectionTCP::disconnect. *============================================================================================

Reimplemented from Cogs::Network::ConnectionTCP.

Definition at line 143 of file ConnectionHub.cpp.

References Cogs::Network::ConnectionTCP::disconnect().

◆ handleReceivedMessage()

void Cogs::Network::ConnectionHub::handleReceivedMessage ( const Message::Ptr &  message)
protectedvirtual

Definition at line 135 of file ConnectionHub.cpp.

◆ processIncoming()

bool Cogs::Network::ConnectionHub::processIncoming ( )
overridevirtual

Manages accepting incoming connections when this connection is a listener.

Reimplemented from Cogs::Network::ConnectionTCP.

Definition at line 87 of file ConnectionHub.cpp.

References Cogs::Network::Message::allocate(), and Cogs::Network::ConnectionBase::receive().

◆ processOutgoing()

bool Cogs::Network::ConnectionHub::processOutgoing ( )
overridevirtual

Reimplemented from Cogs::Network::ConnectionBase.

Definition at line 16 of file ConnectionHub.cpp.

Member Data Documentation

◆ bytesSent

uint64_t Cogs::Network::ConnectionHub::bytesSent = 0
private

Definition at line 36 of file ConnectionHub.h.

◆ incomingHeader

Header Cogs::Network::ConnectionHub::incomingHeader
private

Definition at line 39 of file ConnectionHub.h.

◆ incomingMessage

Message::Ptr Cogs::Network::ConnectionHub::incomingMessage
private

Definition at line 38 of file ConnectionHub.h.

◆ outgoingMessage

Message::Ptr Cogs::Network::ConnectionHub::outgoingMessage
private

Definition at line 35 of file ConnectionHub.h.


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