AERA
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Types | Protected Member Functions | Protected Attributes | List of all members
tcp_io_device::TCPConnection Class Reference

#include <tcp_connection.h>

Public Member Functions

 TCPConnection (std::shared_ptr< SafeQueue > receive_queue, std::shared_ptr< SafeQueue > send_queue, uint64_t msg_length_buf_size)
 
int listenAndAwaitConnection (std::string port)
 
int establishConnection (std::string host, std::string port)
 
void start ()
 
void stop ()
 
bool isRunning ()
 

Static Public Member Functions

static int receiveIsReady (int fd)
 

Static Public Attributes

static std::map< int, std::string > type_to_name_map_
 

Protected Types

enum  State { NOT_STARTED = 0, RUNNING = 1, STOPPED = 2 }
 
enum  SocketType { CLIENT = 0, SERVER = 1 }
 

Protected Member Functions

void tcpBackgroundHandler ()
 
std::unique_ptr< TCPMessagereceiveMessage ()
 
int sendMessage (std::unique_ptr< TCPMessage > msg)
 

Protected Attributes

State state_
 
SocketType socket_type_
 
std::shared_ptr< std::thread > tcp_background_thread_
 
int tcp_socket_
 
int server_listen_socket_
 
uint64_t msg_buf_size_
 
uint64_t msg_length_buf_size_
 
std::shared_ptr< SafeQueueincoming_queue_
 
std::shared_ptr< SafeQueueoutgoing_queue_
 
std::string host_
 
std::string port_
 

Detailed Description

TCPConnection is used to pass data using Win-Sockets between the environment simulation and the TcpIoDevice object. It runs in a different thread and uses SafeQueues to pass data between the IODevice and the socket connection.

Definition at line 167 of file tcp_connection.h.

Constructor & Destructor Documentation

◆ TCPConnection()

tcp_io_device::TCPConnection::TCPConnection ( std::shared_ptr< SafeQueue receive_queue,
std::shared_ptr< SafeQueue send_queue,
uint64_t  msg_length_buf_size 
)

Constructor for the TCPConnection used in a seperate thread to communicate with the environment simulation

Parameters
receive_queueThe queue used to pass incoming messages to the TcpIoDevice.
send_queueThe queue used to pass outgoing messages from the TcpIoDevice.
msg_length_buf_sizeThe number of bytes used to store the message length of the serialized protobuf message (should be 8)

Member Function Documentation

◆ establishConnection()

int tcp_io_device::TCPConnection::establishConnection ( std::string  host,
std::string  port 
)

Opens a socket to connect to a client on the passed port.

Parameters
hostThe host address used to communicate with the server.
portThe port used to communicate with the server.
Returns
0 for success, nonzero for error.

◆ isRunning()

bool tcp_io_device::TCPConnection::isRunning ( )
inline

Returns true if the TCPConnection is running.

Returns
true if running, false otherwise.

Definition at line 211 of file tcp_connection.h.

◆ listenAndAwaitConnection()

int tcp_io_device::TCPConnection::listenAndAwaitConnection ( std::string  port)

Listen on the passed port and wait for a client to connect.

Parameters
portThe port used to communicate with the client.
Returns
0 for success, nonzero for error.

◆ receiveIsReady()

static int tcp_io_device::TCPConnection::receiveIsReady ( int  fd)
static

Check the socket if there is incoming data ready. This does not block.

Parameters
fdThe socket file descriptor.
Returns
1 if data is ready, 0 if no data is ready, -1 for error.

◆ receiveMessage()

std::unique_ptr<TCPMessage> tcp_io_device::TCPConnection::receiveMessage ( )
protected

Method to receive data from the socket and parse them into a TCPMessage

Returns
A unique_ptr to a TCPMessage for enqueuing it into the receive_queue_.

◆ sendMessage()

int tcp_io_device::TCPConnection::sendMessage ( std::unique_ptr< TCPMessage msg)
protected

Converts a message to a byte-stream and sends it to the client.

Parameters
msgThe TCPMessage to send.
Returns
The number of bytes sent. If <= 0 an error occured while sending the message.

◆ start()

void tcp_io_device::TCPConnection::start ( )

Starts the communication between environment simulation and the AERA TCPConnection.

◆ stop()

void tcp_io_device::TCPConnection::stop ( )

Stops the communication

◆ tcpBackgroundHandler()

void tcp_io_device::TCPConnection::tcpBackgroundHandler ( )
protected

Handles the TCP connection in the background by checking for new outgoing and incoming messages, dequeueing and enqueueing the SafeQueues, respectively. Repeatedly checks for new messages on the socket and parses them to TCPMessage objects. Takes TCPMessage objects from the outgoing_queue_ and parses them into a byte-stream to send to the client.


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