Template Class URStream

Inheritance Relationships

Base Type

Class Documentation

template<typename T>
class URStream : public urcl::comm::TCPSocket

The stream is an abstraction of the TCPSocket that offers reading a full UR data package out of the socket. This means, it has to have some knowledge about the package structure to peek at the field defining the package length. This is why it is templated with the package header type.

Public Functions

inline URStream(const std::string &host, int port)

Creates a new URStream object. Note, that this does not immediately open the socket, that has to be done separately by calling the connect() function.

Parameters:
  • host – IP address of the remote host

  • port – Port on which the socket shall be connected

inline bool connect(const size_t max_num_tries = 0, const std::chrono::milliseconds reconnection_time = std::chrono::seconds(10))

Connects to the configured socket.

Parameters:
  • max_num_tries – Maximum number of connection attempts before counting the connection as failed. Unlimited number of attempts when set to 0.

  • reconnection_time – time in between connection attempts to the server

Returns:

True on success, false if connection could not be established

inline void disconnect()

Disconnects from the configured socket.

inline bool closed()

Returns whether the underlying socket is currently closed.

bool read(uint8_t *buf, const size_t buf_len, size_t &read)

Reads a full UR package out of a socket. For this, it looks into the package and reads the byte length from the socket directly. It returns as soon as all bytes for the package are read from the socket.

Parameters:
  • buf[out] The byte buffer where the content shall be stored

  • buf_len[in] Number of bytes allocated for the buffer

  • read[out] Number of bytes actually read from the socket

Returns:

True on success, false on error, e.g. the buffer is smaller than the package size

bool write(const uint8_t *buf, const size_t buf_len, size_t &written)

Writes directly to the underlying socket (with a mutex guard)

Parameters:
  • buf[in] Byte stream that should be sent

  • buf_len[in] Number of bytes in buffer

  • written[out] Number of bytes actually written to the socket

Returns:

False if sending went wrong

inline std::string getHost()

Get the host IP.

Returns:

The host IP