easywsclient for Windows

Revised a source file to be able to compile it in Linux and Windows.

Revised a description error of the include sentence for Windows.

followed the modification of the original version.

preparing for pull request.

preparing for pull request.

Convert tabs to spaces.
Add delete ws; to 'example-client.cpp'.

try2

try4
This commit is contained in:
kivatek
2013-09-27 01:06:15 +09:00
parent 8805fbf736
commit 015344684f
3 changed files with 279 additions and 36 deletions
+109 -36
View File
@@ -1,28 +1,75 @@
#include "easywsclient.hpp"
#include <fcntl.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <fcntl.h>
#include <WinSock2.h>
#include <WS2tcpip.h>
#pragma comment( lib, "ws2_32" )
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <io.h>
#ifndef _SSIZE_T_DEFINED
typedef int ssize_t;
#define _SSIZE_T_DEFINED
#endif
#ifndef _SOCKET_T_DEFINED
typedef SOCKET socket_t;
#define _SOCKET_T_DEFINED
#endif
#ifndef snprintf
#define snprintf _snprintf_s
#endif
#if _MSC_VER >=1600
// vs2010 or later
#include <stdint.h>
#else
typedef __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef __int64 int64_t;
typedef unsigned __int64 uint64_t;
#endif
#else
#include <fcntl.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdint.h>
#ifndef _SOCKET_T_DEFINED
typedef int socket_t;
#define _SOCKET_T_DEFINED
#endif
#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif
#ifndef SOCKET_ERROR
#define SOCKET_ERROR (-1)
#endif
#endif
#include <vector>
#include <string>
namespace { // private module-only namespace
int hostname_connect(std::string hostname, int port) {
socket_t hostname_connect(std::string hostname, int port) {
struct addrinfo hints;
struct addrinfo *result;
struct addrinfo *p;
int ret;
int sockfd = -1;
socket_t sockfd = INVALID_SOCKET;
char sport[16];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
@@ -36,12 +83,16 @@ int hostname_connect(std::string hostname, int port) {
for(p = result; p != NULL; p = p->ai_next)
{
sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (sockfd == -1) { continue; }
if (connect(sockfd, p->ai_addr, p->ai_addrlen) != -1) {
if (sockfd == INVALID_SOCKET) { continue; }
if (connect(sockfd, p->ai_addr, p->ai_addrlen) != SOCKET_ERROR) {
break;
}
#ifdef _WIN32
closesocket(sockfd);
#else
close(sockfd);
sockfd = -1;
#endif
sockfd = INVALID_SOCKET;
}
freeaddrinfo(result);
return sockfd;
@@ -100,13 +151,21 @@ class _RealWebSocket : public easywsclient::WebSocket
uint8_t masking_key[4];
};
inline int close(socket_t sockfd) {
#ifdef _WIN32
return ::closesocket(sockfd);
#else
return ::close(sockfd);
#endif
}
std::vector<uint8_t> rxbuf;
std::vector<uint8_t> txbuf;
int sockfd;
socket_t sockfd;
readyStateValues readyState;
_RealWebSocket(int sockfd) : sockfd(sockfd), readyState(OPEN) {
_RealWebSocket(socket_t sockfd) : sockfd(sockfd), readyState(OPEN) {
}
readyStateValues getReadyState() {
@@ -120,7 +179,11 @@ class _RealWebSocket : public easywsclient::WebSocket
int N = rxbuf.size();
ssize_t ret;
rxbuf.resize(N + 1500);
#ifdef _WIN32
ret = recv(sockfd, (int8_t*)&rxbuf[0] + N, 1500, 0);
#else
ret = recv(sockfd, &rxbuf[0] + N, 1500, 0);
#endif
if (false) { }
else if (ret < 0) {
rxbuf.resize(N);
@@ -128,7 +191,7 @@ class _RealWebSocket : public easywsclient::WebSocket
}
else if (ret == 0) {
rxbuf.resize(N);
::close(sockfd);
close(sockfd);
readyState = CLOSED;
fprintf(stderr, "Connection closed!\n");
break;
@@ -139,12 +202,16 @@ class _RealWebSocket : public easywsclient::WebSocket
}
while (txbuf.size()) {
int ret;
#ifdef _WIN32
ret = ::send(sockfd, (int8_t*)&txbuf[0], txbuf.size(), 0);
#else
ret = ::send(sockfd, &txbuf[0], txbuf.size(), 0);
#endif
if (ret > 0) { txbuf.erase(txbuf.begin(), txbuf.begin() + ret); }
else { break; }
}
if (!txbuf.size() && readyState == CLOSING) {
::close(sockfd);
close(sockfd);
readyState = CLOSED;
}
}
@@ -223,27 +290,28 @@ class _RealWebSocket : public easywsclient::WebSocket
// TODO: consider acquiring a lock on txbuf...
if (readyState == CLOSING || readyState == CLOSED) { return; }
std::vector<uint8_t> header;
header.assign(2 + (message.size() >= 126 ? 2 : 0) + (message.size() >= 65536 ? 6 : 0), 0);
uint64_t message_size = message.size();
header.assign(2 + (message_size >= 126 ? 2 : 0) + (message_size >= 65536 ? 6 : 0), 0);
header[0] = 0x80 | wsheader_type::TEXT_FRAME;
if (false) { }
else if (message.size() < 126) {
header[1] = message.size();
else if (message_size < 126) {
header[1] = message_size & 0xff;
}
else if (message.size() < 65536) {
else if (message_size < 65536) {
header[1] = 126;
header[2] = (message.size() >> 8) & 0xff;
header[3] = (message.size() >> 0) & 0xff;
header[2] = (message_size >> 8) & 0xff;
header[3] = (message_size >> 0) & 0xff;
}
else { // TODO: run coverage testing here
header[1] = 127;
header[2] = (message.size() >> 56) & 0xff;
header[3] = (message.size() >> 48) & 0xff;
header[4] = (message.size() >> 40) & 0xff;
header[5] = (message.size() >> 32) & 0xff;
header[6] = (message.size() >> 24) & 0xff;
header[7] = (message.size() >> 16) & 0xff;
header[8] = (message.size() >> 8) & 0xff;
header[9] = (message.size() >> 0) & 0xff;
header[2] = (message_size >> 56) & 0xff;
header[3] = (message_size >> 48) & 0xff;
header[4] = (message_size >> 40) & 0xff;
header[5] = (message_size >> 32) & 0xff;
header[6] = (message_size >> 24) & 0xff;
header[7] = (message_size >> 16) & 0xff;
header[8] = (message_size >> 8) & 0xff;
header[9] = (message_size >> 0) & 0xff;
}
txbuf.insert(txbuf.end(), header.begin(), header.end());
txbuf.insert(txbuf.end(), message.begin(), message.end());
@@ -294,8 +362,8 @@ WebSocket::pointer WebSocket::from_url(std::string url) {
return NULL;
}
fprintf(stderr, "easywsclient: connecting: host=%s port=%d path=/%s\n", host, port, path);
int sockfd = hostname_connect(host, port);
if (sockfd == -1) {
socket_t sockfd = hostname_connect(host, port);
if (sockfd == INVALID_SOCKET) {
fprintf(stderr, "Unable to connect to %s:%d\n", host, port);
return NULL;
}
@@ -323,7 +391,12 @@ WebSocket::pointer WebSocket::from_url(std::string url) {
}
int flag = 1;
setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, (char*) &flag, sizeof(flag)); // Disable Nagle's algorithm
#ifdef _WIN32
u_long on = 1;
ioctlsocket(sockfd, FIONBIO, &on);
#else
fcntl(sockfd, F_SETFL, O_NONBLOCK);
#endif
fprintf(stderr, "Connected to: %s\n", url.c_str());
return pointer(new _RealWebSocket(sockfd));
}