5 #include <net/socket.h>
15 std::lock_guard<std::mutex> guard(this->_mutex);
17 if(this->_socket != -1)
19 shutdown(this->_socket, SHUT_WR);
27 char ip[INET_ADDRSTRLEN];
29 inet_ntop(AF_INET, &(this->_address.sin_addr), ip, INET_ADDRSTRLEN);
31 return std::string(ip);
36 *ip = this->_address.sin_addr.s_addr;
41 return ntohs(this->_address.sin_port);
46 return this->GetIP() +
":" + std::to_string(this->GetPort());
52 socklen_t optlen =
sizeof(socket_type);
55 getsockopt(this->_socket, SOL_SOCKET, SO_TYPE, &socket_type, &optlen);
75 std::lock_guard<std::mutex> guard(this->_mutex);
77 return this->_recieved_time;
82 std::lock_guard<std::mutex> guard(this->_mutex);
84 ssize_t size = send(this->_socket, msg.c_str(), msg.size(), 0);
89 std::lock_guard<std::mutex> guard(this->_mutex);
91 ssize_t size = send(this->_socket, &(msg[0]), msg.size(), 0);
96 std::lock_guard<std::mutex> guard(this->_mutex);
98 socklen_t address_len =
sizeof(this->_address);
100 sendto(this->_socket, msg.c_str(), msg.size(), 0, (
struct sockaddr*)&this->_address, address_len);
105 std::lock_guard<std::mutex> guard(this->_mutex);
107 socklen_t address_len =
sizeof(this->_address);
109 sendto(this->_socket, &(msg[0]), msg.size(), 0, (
struct sockaddr*)&this->_address, address_len);
114 std::lock_guard<std::mutex> guard(this->_mutex);
116 this->_recieved_time = std::chrono::system_clock::now();
void GetIpArray(uint8_t *ip) const
Gets the IP address associated with the socket as an array of bytes.
void Send(const std::string &msg) const
Sends a message over the socket.
void UpdateLastRecievedTime()
Updates the last received time to the current system time.
std::string GetIP() const
Gets the IP address associated with the socket.
std::string GetSocketType() const
Gets the socket type.
void Close()
Closes the socket.
uint16_t GetPort() const
Gets the port number associated with the socket.
std::string GetAddress() const
Gets the full address (IP:Port) associated with the socket.
void UDPSend(const std::string &msg) const
Sends a UDP message over the socket.
std::chrono::system_clock::time_point GetLastRecievedTime() const
Gets the time when the socket last received data.