From 441cf81acf9dff087addfa8d01a61e4513b1dd6e Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 31 May 2018 01:13:15 +0200 Subject: Starting... --- src/DirectConnection.cpp | 112 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 6 --- 2 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 src/DirectConnection.cpp delete mode 100644 src/main.cpp (limited to 'src') diff --git a/src/DirectConnection.cpp b/src/DirectConnection.cpp new file mode 100644 index 0000000..12ae761 --- /dev/null +++ b/src/DirectConnection.cpp @@ -0,0 +1,112 @@ +#include "../include/DirectConnection.hpp" +#include +#include + +#ifndef WIN32 + #include + #include +#else + #include + #include +#endif +#include + +namespace sibs +{ + Ipv4::Ipv4(const char *ip, u16 port) + { + struct addrinfo hints = {}; + hints.ai_flags = AI_PASSIVE; + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + + char portStr[6]; + sprintf(portStr, "%d", port); + int addrInfoResult = getaddrinfo(ip, portStr, &hints, &address); + if(addrInfoResult != 0) + { + std::string errMsg = "Ip "; + errMsg += ip; + errMsg += " is not a valid ip"; + throw InvalidAddressException(errMsg); + } + } + + Ipv4::~Ipv4() + { + freeaddrinfo(address); + } + + DirectConnections::DirectConnections() : + mySocket(0) + { + try + { + init(); + } + catch(...) + { + cleanup(); + } + } + + DirectConnections::~DirectConnections() + { + cleanup(); + } + + void DirectConnections::init() + { + UDT::startup(); + eid = UDT::epoll_create(); + mySocket = UDT::socket(AI_PASSIVE, AF_INET, SOCK_STREAM); + bool rendezvous = true; + UDT::setsockopt(mySocket, 0, UDT_RENDEZVOUS, &rendezvous, sizeof(bool)); + bool reuseAddr = true; + UDT::setsockopt(mySocket, 0, UDT_REUSEADDR, &reuseAddr, sizeof(bool)); + + // Windows UDP issue + // For better performance, modify HKLM\System\CurrentControlSet\Services\Afd\Parameters\FastSendDatagramThreshold + #ifdef WIN32 + int mss = 1052; + UDT::setsockopt(socket, 0, UDT_MSS, &mss, sizeof(mss)); + #endif + + sockaddr_in myAddr = {}; + myAddr.sin_family = AF_INET; + myAddr.sin_port = htons(9000); + myAddr.sin_addr.s_addr = INADDR_ANY; + memset(&myAddr.sin_zero, '\0', 8); + + if(UDT::bind(mySocket, (sockaddr*)&myAddr, sizeof(myAddr)) == UDT::ERROR) + { + // TODO: Add ip and port to error + std::string errMsg = "UDT: Failed to bind, error: "; + errMsg += UDT::getlasterror().getErrorMessage(); + throw ConnectionException(errMsg); + } + } + + void DirectConnections::cleanup() + { + UDT::epoll_release(eid); + + if(mySocket != 0) + UDT::close(mySocket); + + UDT::cleanup(); + } + + void DirectConnections::connect(const Ipv4 &address) + { + if(UDT::connect(mySocket, address.address->ai_addr, address.address->ai_addrlen) == UDT::ERROR) + { + // TODO: Add ip and port to error + std::string errMsg = "UDT: Failed to connect, error: "; + errMsg += UDT::getlasterror().getErrorMessage(); + throw ConnectionException(errMsg); + } + + //UDT::epoll_add_usock(eid, 2); + } +} diff --git a/src/main.cpp b/src/main.cpp deleted file mode 100644 index f3f6f6c..0000000 --- a/src/main.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main() -{ - return 0; -} -- cgit v1.2.3