compile errors

This commit is contained in:
Benjamin Sergeant 2018-10-08 18:42:56 -07:00
parent 644d988c29
commit 37e899e872
2 changed files with 30 additions and 9 deletions

View File

@ -16,6 +16,9 @@
#ifdef _WIN32 #ifdef _WIN32
# include <WinSock2.h> # include <WinSock2.h>
# include <WS2tcpip.h>
#pragma comment(lib, "ws2_32")
# include <io.h>
#else #else
# include <unistd.h> # include <unistd.h>
# include <errno.h> # include <errno.h>
@ -70,7 +73,7 @@ namespace ix
#endif #endif
} }
bool connectToAddress(const struct addrinfo *address, bool Socket::connectToAddress(const struct addrinfo *address,
int& sockfd, int& sockfd,
std::string& errMsg) std::string& errMsg)
{ {
@ -88,7 +91,7 @@ namespace ix
int maxRetries = 3; int maxRetries = 3;
for (int i = 0; i < maxRetries; ++i) for (int i = 0; i < maxRetries; ++i)
{ {
if (connect(fd, address->ai_addr, address->ai_addrlen) != -1) if (::connect(fd, address->ai_addr, address->ai_addrlen) != -1)
{ {
sockfd = fd; sockfd = fd;
return true; return true;
@ -98,7 +101,7 @@ namespace ix
if (errno != EINTR) break; if (errno != EINTR) break;
} }
::close(fd); closeSocket(fd);
sockfd = -1; sockfd = -1;
errMsg = strerror(errno); errMsg = strerror(errno);
return false; return false;
@ -223,7 +226,7 @@ namespace ix
if (_sockfd == -1) return; if (_sockfd == -1) return;
::close(_sockfd); closeSocket(_sockfd);
_sockfd = -1; _sockfd = -1;
} }
@ -258,6 +261,15 @@ namespace ix
return WSAGetLastError(); return WSAGetLastError();
#else #else
return errno; return errno;
#endif
}
void Socket::closeSocket(int fd)
{
#ifdef _WIN32
closesocket(fd);
#else
::close(fd);
#endif #endif
} }
} }

View File

@ -11,6 +11,8 @@
#include <mutex> #include <mutex>
#include <atomic> #include <atomic>
struct addrinfo;
namespace ix namespace ix
{ {
class Socket { class Socket {
@ -20,7 +22,7 @@ namespace ix
Socket(); Socket();
virtual ~Socket(); virtual ~Socket();
static int hostname_connect(const std::string& hostname, int hostname_connect(const std::string& hostname,
int port, int port,
std::string& errMsg); std::string& errMsg);
void configure(); void configure();
@ -44,9 +46,16 @@ namespace ix
void wakeUpFromPollApple(); void wakeUpFromPollApple();
void wakeUpFromPollLinux(); void wakeUpFromPollLinux();
void closeSocket(int fd);
std::atomic<int> _sockfd; std::atomic<int> _sockfd;
int _eventfd; int _eventfd;
std::mutex _socketMutex; std::mutex _socketMutex;
private:
bool connectToAddress(const struct addrinfo *address,
int& sockfd,
std::string& errMsg);
}; };
} }