BF2MC-Matchmaker
socket.h
1 #ifndef NET_SOCKET_H
2 #define NET_SOCKET_H
3 
4 #include <string>
5 #include <mutex>
6 #include <netinet/in.h>
7 #include <chrono>
8 
9 namespace Net
10 {
14  class Socket
15  {
16  protected:
17  int _socket;
18  struct sockaddr_in _address;
19  std::chrono::system_clock::time_point _recieved_time;
20  mutable std::mutex _mutex;
22  public:
23  Socket();
24 
28  void Close();
29 
34  std::string GetIP() const;
35 
40  void GetIpArray(uint8_t* ip) const;
41 
46  uint16_t GetPort() const;
47 
52  std::string GetAddress() const;
53 
58  std::string GetSocketType() const;
59 
64  std::chrono::system_clock::time_point GetLastRecievedTime() const;
65 
70  void Send(const std::string& msg) const;
71 
76  void Send(const std::vector<unsigned char>& msg) const;
77 
82  void UDPSend(const std::string& msg) const;
83 
88  void UDPSend(const std::vector<unsigned char>& msg) const;
89 
94 
102  virtual void WTF_WHY_AM_I_HERE_1337() { /* Empty virtual function */ }
103  };
104 }
105 
106 #endif // NET_SOCKET_H
A base class representing a network socket.
Definition: socket.h:15
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::mutex _mutex
Definition: socket.h:20
struct sockaddr_in _address
Definition: socket.h:18
virtual void WTF_WHY_AM_I_HERE_1337()
Empty virtual function required for static_cast in C++.
Definition: socket.h:102
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
std::chrono::system_clock::time_point _recieved_time
Definition: socket.h:19
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
int _socket
Definition: socket.h:17
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