From a76b00c511db1d3529bba51475a24ca93d12913e Mon Sep 17 00:00:00 2001 From: Joursoir Date: Tue, 5 Jan 2021 18:07:25 +0300 Subject: client gui: add support windows --- src/client/ClientBase.cpp | 103 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 82 insertions(+), 21 deletions(-) (limited to 'src/client/ClientBase.cpp') diff --git a/src/client/ClientBase.cpp b/src/client/ClientBase.cpp index 8988622..2256532 100644 --- a/src/client/ClientBase.cpp +++ b/src/client/ClientBase.cpp @@ -1,51 +1,112 @@ #include #include -#include // for sockaddr_in -#include // for iten_aton -#include // for bind, connect -#include // for bind, connect +#ifndef _WIN32 + #include // for sockaddr_in + #include // for iten_aton + #include // for bind, connect + #include // for bind, connect +#endif #include #include +#include #include "ClientBase.hpp" ClientBase::ClientBase(const char* ip, int port) : out_buf_used(0), exit_flag(false), connection(true) { - fd = CreateSocket(ip, port); + if(InitSocket(ip, port) == -1) { + exit(5); +#ifdef _WIN32 + fd = INVALID_SOCKET; +#else + fd = -1; +#endif + } } ClientBase::~ClientBase() { +#ifdef _WIN32 + if(fd != INVALID_SOCKET) { + closesocket(fd); + } + WSACleanup(); +#else if(fd != -1) close(fd); +#endif } -int ClientBase::CreateSocket(const char* ip, int port) +int ClientBase::ConstuctorError() const { - int client = socket(AF_INET, SOCK_STREAM, 0); - if(client == -1) return -1; +#ifdef _WIN32 + return fd == INVALID_SOCKET ? 1 : 0; +#else + return fd == -1 ? 1 : 0; +#endif +} - // remove "port sticking" aka socket in TIME_WAIT - int opt = 1; - setsockopt(client, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); +/* + * call bind optional, the system chooses + * the address automatically + */ +int ClientBase::InitSocket(const char* ip, int port) +{ + int result; + int opt = 1; // for remove "port sticking" + int ifamily = AF_INET; + int itype = SOCK_STREAM; + int iprotocol = 0; + +#ifdef _WIN32 // windows socket + WSADATA wsaData; + SOCKADDR_IN server_address; + + // initialize winsock: + result = WSAStartup(MAKEWORD(2, 2), &wsaData); + if(result != 0) return -1; + + fd = socket(ifamily, itype, iprotocol); + if(fd == INVALID_SOCKET) return -1; + + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)); + + server_address.sin_family = ifamily; + server_address.sin_addr.s_addr = inet_addr(ip); + server_address.sin_port = htons(port); + result = connect(fd, (SOCKADDR *) &server_address, sizeof(server_address)); + if(result == SOCKET_ERROR) { + closesocket(fd); + return -1; + } +#else // linux socket: + struct sockaddr_in server_address; - /* call bind optional, the system chooses - the address automatically */ + fd = socket(ifamily, itype, iprotocol); + if(fd == -1) return -1; + + // remove "port sticking" aka socket in TIME_WAIT + setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - struct sockaddr_in server_adress; server_adress.sin_family = AF_INET; server_adress.sin_port = htons(port); - if(!inet_aton(ip, &(server_adress.sin_addr))) return -1; + if(!inet_aton(ip, &(server_adress.sin_addr))) { + close(fd); + return -1; + } - int res = connect(client, (struct sockaddr*) &server_adress, + result = connect(client, (struct sockaddr*) &server_adress, sizeof(server_adress)); - if(res == -1) return -1; - - int flags = fcntl(client, F_GETFL, 0); - fcntl(client, F_SETFL, flags | O_NONBLOCK); + if(result == -1) { + close(fd); + return -1; + } - return client; + int flags = fcntl(fd, F_GETFL, 0); + fcntl(fd, F_SETFL, flags | O_NONBLOCK); +#endif + return 0; } int ClientBase::Run() -- cgit v1.2.3-18-g5258