diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-05-31 01:13:15 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-05-31 01:13:17 +0200 |
commit | 595b1b7d2fc7aa084296978b8a38a26ed960a0f5 (patch) | |
tree | 966a0bd21e1a824cf0151a8091ad9fdc71998b8f /src | |
parent | ce3a9cdb6929b78f6c163f1c8f4aaf91f384f2ee (diff) |
Starting...
Diffstat (limited to 'src')
-rw-r--r-- | src/DirectConnection.cpp | 112 | ||||
-rw-r--r-- | src/main.cpp | 6 |
2 files changed, 112 insertions, 6 deletions
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 <cstdio> +#include <cstring> + +#ifndef WIN32 + #include <arpa/inet.h> + #include <netdb.h> +#else + #include <winsock2.h> + #include <ws2tcpip.h> +#endif +#include <udt/udt.h> + +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 <udt/udt.h> - -int main() -{ - return 0; -} |