BF2MC-Matchmaker
socket.cpp
1 #include <vector>
2 #include <arpa/inet.h>
3 #include <unistd.h>
4 
5 #include <net/socket.h>
6 #include <logger.h>
7 
8 Net::Socket::Socket()
9 {
10 
11 }
12 
14 {
15  std::lock_guard<std::mutex> guard(this->_mutex); // socket lock
16 
17  if(this->_socket != -1)
18  {
19  shutdown(this->_socket, SHUT_WR);
20  close(this->_socket);
21  this->_socket = -1; // Removes reference
22  }
23 }
24 
25 std::string Net::Socket::GetIP() const
26 {
27  char ip[INET_ADDRSTRLEN];
28 
29  inet_ntop(AF_INET, &(this->_address.sin_addr), ip, INET_ADDRSTRLEN);
30 
31  return std::string(ip);
32 }
33 
34 void Net::Socket::GetIpArray(uint8_t* ip) const
35 {
36  *ip = this->_address.sin_addr.s_addr;
37 }
38 
39 uint16_t Net::Socket::GetPort() const
40 {
41  return ntohs(this->_address.sin_port);
42 }
43 
44 std::string Net::Socket::GetAddress() const
45 {
46  return this->GetIP() + ":" + std::to_string(this->GetPort());
47 }
48 
49 std::string Net::Socket::GetSocketType() const
50 {
51  int socket_type;
52  socklen_t optlen = sizeof(socket_type);
53 
54  // Get socket type
55  getsockopt(this->_socket, SOL_SOCKET, SO_TYPE, &socket_type, &optlen);
56 
57  switch(socket_type)
58  {
59  case SOCK_STREAM:
60  return "tcp";
61  break;
62 
63  case SOCK_DGRAM:
64  return "udp";
65  break;
66 
67  default:
68  return "unknown";
69  break;
70  }
71 }
72 
73 std::chrono::system_clock::time_point Net::Socket::GetLastRecievedTime() const
74 {
75  std::lock_guard<std::mutex> guard(this->_mutex); // socket lock
76 
77  return this->_recieved_time;
78 }
79 
80 void Net::Socket::Send(const std::string& msg) const
81 {
82  std::lock_guard<std::mutex> guard(this->_mutex); // socket lock
83 
84  ssize_t size = send(this->_socket, msg.c_str(), msg.size(), 0);
85 }
86 
87 void Net::Socket::Send(const std::vector<unsigned char>& msg) const
88 {
89  std::lock_guard<std::mutex> guard(this->_mutex); // socket lock
90 
91  ssize_t size = send(this->_socket, &(msg[0]), msg.size(), 0);
92 }
93 
94 void Net::Socket::UDPSend(const std::string& msg) const
95 {
96  std::lock_guard<std::mutex> guard(this->_mutex); // socket lock
97 
98  socklen_t address_len = sizeof(this->_address);
99 
100  sendto(this->_socket, msg.c_str(), msg.size(), 0, (struct sockaddr*)&this->_address, address_len);
101 }
102 
103 void Net::Socket::UDPSend(const std::vector<unsigned char>& msg) const
104 {
105  std::lock_guard<std::mutex> guard(this->_mutex); // socket lock
106 
107  socklen_t address_len = sizeof(this->_address);
108 
109  sendto(this->_socket, &(msg[0]), msg.size(), 0, (struct sockaddr*)&this->_address, address_len);
110 }
111 
113 {
114  std::lock_guard<std::mutex> guard(this->_mutex); // socket lock
115 
116  this->_recieved_time = std::chrono::system_clock::now();
117 }
118 
void GetIpArray(uint8_t *ip) const
Gets the IP address associated with the socket as an array of bytes.
Definition: socket.cpp:34
void Send(const std::string &msg) const
Sends a message over the socket.
Definition: socket.cpp:80
void UpdateLastRecievedTime()
Updates the last received time to the current system time.
Definition: socket.cpp:112
std::string GetIP() const
Gets the IP address associated with the socket.
Definition: socket.cpp:25
std::string GetSocketType() const
Gets the socket type.
Definition: socket.cpp:49
void Close()
Closes the socket.
Definition: socket.cpp:13
uint16_t GetPort() const
Gets the port number associated with the socket.
Definition: socket.cpp:39
std::string GetAddress() const
Gets the full address (IP:Port) associated with the socket.
Definition: socket.cpp:44
void UDPSend(const std::string &msg) const
Sends a UDP message over the socket.
Definition: socket.cpp:94
std::chrono::system_clock::time_point GetLastRecievedTime() const
Gets the time when the socket last received data.
Definition: socket.cpp:73