src.dualinventive.com/dinet/sec-multi-proxy/include/server/UDPConnection.h

94 lines
2.5 KiB
C++

/**
* @file include/server/UDPConnection.h
* @brief The UDP connection class
* @copyright 2015 Dual Inventive Technology Centre B.V.
*
* UDP Connection contains information about the UDP connection
*/
#ifndef INCLUDE_SERVER_UDPCONNECTION_H_
#define INCLUDE_SERVER_UDPCONNECTION_H_
#include <set>
#include <shared_mutex>
#include <atomic>
#include <memory>
#include <string>
#include <di/Buffer.h>
class UDPConnection {
public:
/**
* constructor for UDP connection
* @param address The socket address
* @param ip The UDP IP address
* @param port The UDP port
*/
UDPConnection(const std::shared_ptr<struct sockaddr_in> &address, const std::string &ip, uint16_t port);
/**
* returns an unique key for this connection that can be used in a std::map
* @param ip The IP of the connection
* @param port The port of the connection
* @return the unique key as format <ip>:<port>
*/
static std::string getUniqueKey(const std::string &ip, uint16_t port);
/**
* Returns the socket address
* @return The sockaddr_in UDP address
*/
std::shared_ptr<struct sockaddr_in> getAddress();
/**
* Returns the connection information in the format:
* `"<remote addr>:<remote port>"`
* @return the connection information in format `"<remote addr>:<remote port>"`
*/
std::string getPeer();
/**
* Returns the IP of this connection
* @return the IP of this connection
*/
std::string getIP();
/**
* Returns the port of this connection
* @return the port of this connection
*/
uint16_t getPort();
/**
* DI-Net timestamp when the last incoming data was seen.
*/
uint64_t lastReadTime(void);
/**
* Update the internal readTime. Call this every time a new message is received.
*/
void updateReadTime(void);
/**
* Adds a new Device UID to this connection
* @param deviceUid The device UID to add
*/
void addDeviceUid(const std::string &deviceUid);
/**
* Returns all the known device UIDs on this connection
* @return A set with all the known device UIDs on this connection
*/
std::set<std::string> getDeviceUids(void);
private:
std::shared_ptr<struct sockaddr_in> __address; /**< peer address*/
std::string __peerIp; /**< peer IP address*/
uint16_t __peerPort; /**< peer port number*/
std::atomic<uint64_t> __lastReadTime; /**< time of the last read*/
std::set<std::string> __deviceUids; /**< device unique Identifiers */
mutable std::shared_timed_mutex __deviceUidsMu; /**< device unique Identifiers */
};
#endif // INCLUDE_SERVER_UDPCONNECTION_H_