2022-06-11 11:32:27 -04:00
|
|
|
#include "XSocketClient.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2022-06-22 23:37:46 -04:00
|
|
|
#include "common/cross_sockets/XSocket.h"
|
2023-08-08 20:53:16 -04:00
|
|
|
#include "common/log/log.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
|
|
|
|
// clang-format off
|
2022-06-11 11:32:27 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define NOMINMAX
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <WinSock2.h>
|
|
|
|
#include <WS2tcpip.h>
|
|
|
|
#endif
|
2023-01-07 11:24:02 -05:00
|
|
|
#include "common/repl/nrepl/ReplServer.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
|
2022-06-11 11:32:27 -04:00
|
|
|
#include "third-party/fmt/core.h"
|
2022-06-22 23:37:46 -04:00
|
|
|
// clang-format on
|
2022-06-11 11:32:27 -04:00
|
|
|
|
|
|
|
XSocketClient::XSocketClient(int _tcp_port) {
|
|
|
|
tcp_port = _tcp_port;
|
|
|
|
}
|
|
|
|
|
|
|
|
XSocketClient::~XSocketClient() {
|
|
|
|
disconnect();
|
|
|
|
client_socket = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void XSocketClient::disconnect() {
|
|
|
|
close_socket(client_socket);
|
|
|
|
client_socket = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool XSocketClient::connect() {
|
|
|
|
// Open Socket
|
|
|
|
client_socket = open_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
|
|
|
if (client_socket < 0) {
|
|
|
|
// TODO - log
|
|
|
|
disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.sin_family = AF_INET;
|
2023-08-08 20:53:16 -04:00
|
|
|
// Convert IP address from text to binary form using inet_pton()
|
|
|
|
if (inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr) <= 0) {
|
|
|
|
lg::error("inet_pton() failed");
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-11 11:32:27 -04:00
|
|
|
addr.sin_port = htons(tcp_port);
|
|
|
|
|
|
|
|
// Connect to server
|
|
|
|
int result = connect_socket(client_socket, (sockaddr*)&addr, sizeof(addr));
|
|
|
|
if (result == -1) {
|
|
|
|
// TODO - log and close
|
|
|
|
disconnect();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|