win port wip

This commit is contained in:
Benjamin Sergeant
2018-10-08 18:24:40 -07:00
parent c3bf260330
commit b68a4c26f2
56 changed files with 4535 additions and 5 deletions

Binary file not shown.

Binary file not shown.

BIN
ixwebsocket/.IXSocket.h.un~ Normal file

Binary file not shown.

View File

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

274
ixwebsocket/IXSocket.cpp~ Normal file
View File

@ -0,0 +1,274 @@
/*
* IXSocket.cpp
* Author: Benjamin Sergeant
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
*/
#include "IXSocket.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <fcntl.h>
#include <sys/types.h>
#ifdef _WIN32
# include <WinSock2.h>
# include <WS2tcpip.h>
#pragma comment(lib, "ws2_32")
#else
# include <unistd.h>
# include <errno.h>
# include <netdb.h>
# include <netinet/tcp.h>
# include <sys/socket.h>
# include <sys/time.h>
# include <sys/select.h>
# include <sys/stat.h>
#endif
//
// Linux/Android has a special type of virtual files. select(2) will react
// when reading/writing to those files, unlike closing sockets.
//
// https://linux.die.net/man/2/eventfd
//
// eventfd was added in Linux kernel 2.x, and our oldest Android (Kitkat 4.4)
// is on Kernel 3.x
//
// cf Android/Kernel table here
// https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel
//
#ifdef __linux__
# include <sys/eventfd.h>
#endif
// Android needs extra headers for TCP_NODELAY and IPPROTO_TCP
#ifdef ANDROID
# include <linux/in.h>
# include <linux/tcp.h>
#endif
namespace ix
{
Socket::Socket() :
_sockfd(-1),
_eventfd(-1)
{
#ifdef __linux__
_eventfd = eventfd(0, 0);
assert(_eventfd != -1 && "Panic - eventfd not functioning on this platform");
#endif
}
Socket::~Socket()
{
close();
#ifdef __linux__
::close(_eventfd);
#endif
}
bool Socket::connectToAddress(const struct addrinfo *address,
int& sockfd,
std::string& errMsg)
{
sockfd = -1;
int fd = socket(address->ai_family,
address->ai_socktype,
address->ai_protocol);
if (fd < 0)
{
errMsg = "Cannot create a socket";
return false;
}
int maxRetries = 3;
for (int i = 0; i < maxRetries; ++i)
{
if (connect(fd, address->ai_addr, address->ai_addrlen) != -1)
{
sockfd = fd;
return true;
}
// EINTR means we've been interrupted, in which case we try again.
if (errno != EINTR) break;
}
closeSocket(fd);
sockfd = -1;
errMsg = strerror(errno);
return false;
}
int Socket::hostname_connect(const std::string& hostname,
int port,
std::string& errMsg)
{
struct addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
std::string sport = std::to_string(port);
struct addrinfo *res = nullptr;
int getaddrinfo_result = getaddrinfo(hostname.c_str(), sport.c_str(),
&hints, &res);
if (getaddrinfo_result)
{
errMsg = gai_strerror(getaddrinfo_result);
return -1;
}
int sockfd = -1;
// iterate through the records to find a working peer
struct addrinfo *address;
bool success = false;
for (address = res; address != nullptr; address = address->ai_next)
{
success = connectToAddress(address, sockfd, errMsg);
if (success)
{
break;
}
}
freeaddrinfo(res);
return sockfd;
}
void Socket::configure()
{
int flag = 1;
setsockopt(_sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm
fcntl(_sockfd, F_SETFL, O_NONBLOCK); // make socket non blocking
#ifdef SO_NOSIGPIPE
int value = 1;
setsockopt(_sockfd, SOL_SOCKET, SO_NOSIGPIPE,
(void *)&value, sizeof(value));
#endif
}
void Socket::poll(const OnPollCallback& onPollCallback)
{
if (_sockfd == -1)
{
onPollCallback();
return;
}
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(_sockfd, &rfds);
#ifdef __linux__
FD_SET(_eventfd, &rfds);
#endif
int sockfd = _sockfd;
int nfds = std::max(sockfd, _eventfd);
select(nfds + 1, &rfds, nullptr, nullptr, nullptr);
onPollCallback();
}
void Socket::wakeUpFromPollApple()
{
close(); // All OS but Linux will wake up select
// when closing the file descriptor watched by select
}
void Socket::wakeUpFromPollLinux()
{
std::string str("\n"); // this will wake up the thread blocked on select
const void* buf = reinterpret_cast<const void*>(str.c_str());
write(_eventfd, buf, str.size());
}
void Socket::wakeUpFromPoll()
{
#ifdef __APPLE__
wakeUpFromPollApple();
#elif defined(__linux__)
wakeUpFromPollLinux();
#endif
}
bool Socket::connect(const std::string& host,
int port,
std::string& errMsg)
{
std::lock_guard<std::mutex> lock(_socketMutex);
#ifdef __linux__
if (_eventfd == -1)
{
return false; // impossible to use this socket if eventfd is broken
}
#endif
_sockfd = Socket::hostname_connect(host, port, errMsg);
return _sockfd != -1;
}
void Socket::close()
{
std::lock_guard<std::mutex> lock(_socketMutex);
if (_sockfd == -1) return;
closeSocket(_sockfd);
_sockfd = -1;
}
int Socket::send(char* buffer, size_t length)
{
int flags = 0;
#ifdef MSG_NOSIGNAL
flags = MSG_NOSIGNAL;
#endif
return (int) ::send(_sockfd, buffer, length, flags);
}
int Socket::send(const std::string& buffer)
{
return send((char*)&buffer[0], buffer.size());
}
int Socket::recv(void* buffer, size_t length)
{
int flags = 0;
#ifdef MSG_NOSIGNAL
flags = MSG_NOSIGNAL;
#endif
return (int) ::recv(_sockfd, buffer, length, flags);
}
int Socket::getErrno() const
{
#ifdef _WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
void Socket::closeSocket(int fd)
{
#ifdef _WIN32
closesocket(fd);
#else
::close(fd);
#endif
}
}

View File

@ -11,6 +11,8 @@
#include <mutex>
#include <atomic>
struct addrinfo;
namespace ix
{
class Socket {
@ -44,6 +46,11 @@ namespace ix
void wakeUpFromPollApple();
void wakeUpFromPollLinux();
void closeSocket(int fd);
bool connectToAddress(const struct addrinfo *address,
int& sockfd,
std::string& errMsg);
std::atomic<int> _sockfd;
int _eventfd;
std::mutex _socketMutex;

59
ixwebsocket/IXSocket.h~ Normal file
View File

@ -0,0 +1,59 @@
/*
* IXSocket.h
* Author: Benjamin Sergeant
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
*/
#pragma once
#include <string>
#include <functional>
#include <mutex>
#include <atomic>
struct addrinfo*;
namespace ix
{
class Socket {
public:
using OnPollCallback = std::function<void()>;
Socket();
virtual ~Socket();
static int hostname_connect(const std::string& hostname,
int port,
std::string& errMsg);
void configure();
virtual void poll(const OnPollCallback& onPollCallback);
virtual void wakeUpFromPoll();
// Virtual methods
virtual bool connect(const std::string& url,
int port,
std::string& errMsg);
virtual void close();
virtual int send(char* buffer, size_t length);
virtual int send(const std::string& buffer);
virtual int recv(void* buffer, size_t length);
int getErrno() const;
protected:
void wakeUpFromPollApple();
void wakeUpFromPollLinux();
void closeSocket(int fd);
bool connectToAddress(const struct addrinfo *address,
int& sockfd,
std::string& errMsg);
std::atomic<int> _sockfd;
int _eventfd;
std::mutex _socketMutex;
};
}