sys: Fix sockets compiling on Unix
This commit is contained in:
parent
cc5a437573
commit
c55146a78c
@ -7,10 +7,17 @@
|
|||||||
|
|
||||||
#if defined(OS_WINDOWS)
|
#if defined(OS_WINDOWS)
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#include <ws2tcpip.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
|
#define SOCKET_NONE INVALID_SOCKET
|
||||||
#else
|
#else
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/sock.h>
|
#include <sys/socket.h>
|
||||||
|
#include <netinet/ip.h>
|
||||||
|
#include <arpa/inet.h>
|
||||||
|
|
||||||
|
#define SOCKET_NONE -1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace hex {
|
namespace hex {
|
||||||
@ -39,9 +46,9 @@ namespace hex {
|
|||||||
private:
|
private:
|
||||||
bool m_connected = false;
|
bool m_connected = false;
|
||||||
#if defined(OS_WINDOWS)
|
#if defined(OS_WINDOWS)
|
||||||
SOCKET m_socket = INVALID_SOCKET;
|
SOCKET m_socket = SOCKET_NONE;
|
||||||
#else
|
#else
|
||||||
int m_socket = -1;
|
int m_socket = SOCKET_NONE;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -3,8 +3,6 @@
|
|||||||
#include <hex/helpers/utils.hpp>
|
#include <hex/helpers/utils.hpp>
|
||||||
#include <hex/helpers/logger.hpp>
|
#include <hex/helpers/logger.hpp>
|
||||||
|
|
||||||
#include <ws2tcpip.h>
|
|
||||||
|
|
||||||
namespace hex {
|
namespace hex {
|
||||||
|
|
||||||
Socket::Socket(const std::string &address, u16 port) {
|
Socket::Socket(const std::string &address, u16 port) {
|
||||||
@ -26,7 +24,7 @@ namespace hex {
|
|||||||
this->m_socket = other.m_socket;
|
this->m_socket = other.m_socket;
|
||||||
this->m_connected = other.m_connected;
|
this->m_connected = other.m_connected;
|
||||||
|
|
||||||
other.m_socket = INVALID_SOCKET;
|
other.m_socket = SOCKET_NONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::~Socket() {
|
Socket::~Socket() {
|
||||||
@ -74,21 +72,31 @@ namespace hex {
|
|||||||
|
|
||||||
void Socket::connect(const std::string &address, u16 port) {
|
void Socket::connect(const std::string &address, u16 port) {
|
||||||
this->m_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
this->m_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||||
if (this->m_socket == INVALID_SOCKET)
|
if (this->m_socket == SOCKET_NONE)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
sockaddr_in client = { 0 };
|
sockaddr_in client = { 0 };
|
||||||
|
|
||||||
client.sin_family = AF_INET;
|
client.sin_family = AF_INET;
|
||||||
client.sin_addr.S_un.S_addr = ::inet_addr(address.c_str());
|
|
||||||
client.sin_port = ::htons(port);
|
client.sin_port = ::htons(port);
|
||||||
|
|
||||||
this->m_connected = ::connect(this->m_socket, reinterpret_cast<SOCKADDR*>(&client), sizeof(client)) != SOCKET_ERROR;
|
#if defined(OS_WINDOWS)
|
||||||
|
client.sin_addr.S_un.S_addr = ::inet_addr(address.c_str());
|
||||||
|
#else
|
||||||
|
client.sin_addr.s_addr = ::inet_addr(address.c_str());
|
||||||
|
#endif
|
||||||
|
|
||||||
|
this->m_connected = ::connect(this->m_socket, reinterpret_cast<sockaddr*>(&client), sizeof(client)) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Socket::disconnect() {
|
void Socket::disconnect() {
|
||||||
if (this->m_socket != INVALID_SOCKET)
|
if (this->m_socket != SOCKET_NONE) {
|
||||||
closesocket(this->m_socket);
|
#if defined(OS_WINDOWS)
|
||||||
|
closesocket(this->m_socket);
|
||||||
|
#else
|
||||||
|
close(this->m_socket);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
this->m_connected = false;
|
this->m_connected = false;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user