Skip to content
Snippets Groups Projects
Commit c881ccdb authored by Carsten Patzke's avatar Carsten Patzke
Browse files

SystemIO: Merged windows and linux code

parent ceb920d2
No related branches found
No related tags found
No related merge requests found
......@@ -393,6 +393,64 @@ std::unique_ptr<std::tuple<std::string, uint16_t>> SystemIO::SplitAddressToHostA
}
}
void hidra2::SystemIO::InetConnect(SocketDescriptor socket_fd, const std::string& address, IOErrors* err) const {
*err = IOErrors::kNoError;
auto host_port_tuple = SplitAddressToHostAndPort(address);
if (!host_port_tuple) {
*err = IOErrors::kInvalidAddressFormat;
return;
}
std::string host;
uint16_t port = 0;
std::tie(host, port) = *host_port_tuple;
short family = AddressFamilyToPosixFamily(AddressFamilies::INET);
if (family == -1) {
*err = IOErrors::kUnsupportedAddressFamily;
return;
}
sockaddr_in socket_address{};
socket_address.sin_addr.s_addr = inet_addr(host.c_str());
socket_address.sin_port = htons(port);
socket_address.sin_family = family;
if (::connect(socket_fd, (struct sockaddr*) &socket_address, sizeof(socket_address)) == -1) {
*err = GetLastError();
return;
}
}
std::unique_ptr<std::tuple<std::string, SocketDescriptor>> SystemIO::InetAccept(SocketDescriptor socket_fd,
IOErrors* err) const {
*err = IOErrors::kNoError;
static short family = AddressFamilyToPosixFamily(AddressFamilies::INET);
if (family == -1) {
*err = IOErrors::kUnsupportedAddressFamily;
return nullptr;
}
sockaddr_in client_address{};
static int client_address_size = sizeof(sockaddr_in);
int peer_fd = ::accept(socket_fd, reinterpret_cast<sockaddr*>(&client_address), &client_address_size);
if (peer_fd == -1) {
*err = GetLastError();
return nullptr;
}
std::string
address = std::string(inet_ntoa(client_address.sin_addr)) + ':' + std::to_string(client_address.sin_port);
return std::unique_ptr<std::tuple<std::string, SocketDescriptor>>(new
std::tuple<std::string,
SocketDescriptor>(
address,
peer_fd));
}
}
......@@ -8,8 +8,6 @@
#include <direct.h>
#include <iostream>
#pragma comment(lib, "Ws2_32.lib")
using std::string;
using std::vector;
using std::chrono::system_clock;
......@@ -144,63 +142,6 @@ void SystemIO::CollectFileInformationRecursivly(const std::string& path,
}
}
std::unique_ptr<std::tuple<std::string, SocketDescriptor>> SystemIO::InetAccept(SocketDescriptor socket_fd,
IOErrors* err) const {
*err = IOErrors::kNoError;
static short family = AddressFamilyToPosixFamily(AddressFamilies::INET);
if (family == -1) {
*err = IOErrors::kUnsupportedAddressFamily;
return nullptr;
}
sockaddr_in client_address{};
static int client_address_size = sizeof(sockaddr_in);
int peer_fd = ::accept(socket_fd, reinterpret_cast<sockaddr*>(&client_address), &client_address_size);
if (peer_fd == -1) {
*err = GetLastError();
return nullptr;
}
std::string
address = std::string(inet_ntoa(client_address.sin_addr)) + ':' + std::to_string(client_address.sin_port);
return std::unique_ptr<std::tuple<std::string, SocketDescriptor>>(new
std::tuple<std::string,
SocketDescriptor>(
address,
peer_fd));
}
void hidra2::SystemIO::InetConnect(SocketDescriptor socket_fd, const std::string& address, IOErrors* err) const {
*err = IOErrors::kNoError;
auto host_port_tuple = SplitAddressToHostAndPort(address);
if (!host_port_tuple) {
*err = IOErrors::kInvalidAddressFormat;
return;
}
std::string host;
uint16_t port = 0;
std::tie(host, port) = *host_port_tuple;
short family = AddressFamilyToPosixFamily(AddressFamilies::INET);
if (family == -1) {
*err = IOErrors::kUnsupportedAddressFamily;
return;
}
sockaddr_in socket_address{};
socket_address.sin_addr.s_addr = inet_addr(host.c_str());
socket_address.sin_port = htons(port);
socket_address.sin_family = family;
if (::connect(socket_fd, (struct sockaddr*) &socket_address, sizeof(socket_address)) == -1) {
*err = GetLastError();
return;
}
}
FileDescriptor SystemIO::_open(const char* filename, int posix_open_flags) const {
int fd;
errno = _sopen_s(&fd, filename, posix_open_flags, _SH_DENYNO, _S_IREAD | _S_IWRITE);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment