2021-12-07 22:47:57 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <hex.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
#include <winsock2.h>
|
2021-12-07 23:09:30 +01:00
|
|
|
#include <ws2tcpip.h>
|
2021-12-07 22:47:57 +01:00
|
|
|
#include <windows.h>
|
2021-12-07 23:09:30 +01:00
|
|
|
|
|
|
|
#define SOCKET_NONE INVALID_SOCKET
|
2021-12-07 22:47:57 +01:00
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
2021-12-07 23:09:30 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#define SOCKET_NONE -1
|
2021-12-07 22:47:57 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace hex {
|
|
|
|
|
|
|
|
class Socket {
|
|
|
|
public:
|
2022-02-01 22:09:44 +01:00
|
|
|
Socket() = default;
|
2022-01-24 20:53:17 +01:00
|
|
|
Socket(const Socket &) = delete;
|
2021-12-07 22:47:57 +01:00
|
|
|
Socket(Socket &&other);
|
|
|
|
|
|
|
|
Socket(const std::string &address, u16 port);
|
|
|
|
~Socket();
|
|
|
|
|
|
|
|
void connect(const std::string &address, u16 port);
|
|
|
|
void disconnect();
|
|
|
|
|
2022-01-24 20:53:17 +01:00
|
|
|
[[nodiscard]] bool isConnected() const;
|
2021-12-07 22:47:57 +01:00
|
|
|
|
|
|
|
std::string readString(size_t size = 0x1000) const;
|
|
|
|
std::vector<u8> readBytes(size_t size = 0x1000) const;
|
|
|
|
|
|
|
|
void writeString(const std::string &string) const;
|
|
|
|
void writeBytes(const std::vector<u8> &bytes) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool m_connected = false;
|
2022-01-24 20:53:17 +01:00
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
SOCKET m_socket = SOCKET_NONE;
|
|
|
|
#else
|
|
|
|
int m_socket = SOCKET_NONE;
|
|
|
|
#endif
|
2021-12-07 22:47:57 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|