win code runs

This commit is contained in:
Benjamin Sergeant 2018-10-08 21:31:02 -07:00
parent 44e5ab7f7d
commit a9ffa7da11
4 changed files with 28 additions and 9 deletions

View File

@ -13,6 +13,7 @@
#include <sstream> #include <sstream>
#include <queue> #include <queue>
#include <ixwebsocket/IXWebSocket.h> #include <ixwebsocket/IXWebSocket.h>
#include <ixwebsocket/IXSocket.h>
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
@ -160,11 +161,9 @@ namespace
void interactiveMain() void interactiveMain()
{ {
std::string user(getenv("USER")); std::string user("foo");
WebSocketChat webSocketChat(user);
std::cout << "Type Ctrl-D to exit prompt..." << std::endl; std::cout << "Type Ctrl-D to exit prompt..." << std::endl;
WebSocketChat webSocketChat(user);
webSocketChat.start(); webSocketChat.start();
while (true) while (true)
@ -188,6 +187,9 @@ namespace
int main() int main()
{ {
std::cout << "main starting" << std::endl;
Socket::init();
std::cout << "socket initialized" << std::endl;
interactiveMain(); interactiveMain();
return 0; return 0;
} }

View File

@ -33,6 +33,8 @@
#include <fcntl.h> #include <fcntl.h>
#include <sys/types.h> #include <sys/types.h>
#include <algorithm>
// //
// Linux/Android has a special type of virtual files. select(2) will react // Linux/Android has a special type of virtual files. select(2) will react
// when reading/writing to those files, unlike closing sockets. // when reading/writing to those files, unlike closing sockets.
@ -155,7 +157,7 @@ namespace ix
#ifdef _WIN32 #ifdef _WIN32
unsigned long nonblocking = 1; unsigned long nonblocking = 1;
ioctlsocket(fd, FIONBIO, &nonblocking); ioctlsocket(_sockfd, FIONBIO, &nonblocking);
#else #else
fcntl(_sockfd, F_SETFL, O_NONBLOCK); // make socket non blocking fcntl(_sockfd, F_SETFL, O_NONBLOCK); // make socket non blocking
#endif #endif
@ -184,7 +186,7 @@ namespace ix
#endif #endif
int sockfd = _sockfd; int sockfd = _sockfd;
int nfds = std::max(sockfd, _eventfd); int nfds = (std::max)(sockfd, _eventfd);
select(nfds + 1, &rfds, nullptr, nullptr, nullptr); select(nfds + 1, &rfds, nullptr, nullptr, nullptr);
onPollCallback(); onPollCallback();
@ -261,7 +263,7 @@ namespace ix
flags = MSG_NOSIGNAL; flags = MSG_NOSIGNAL;
#endif #endif
return (int) ::recv(_sockfd, buffer, length, flags); return (int) ::recv(_sockfd, (char*) buffer, length, flags);
} }
int Socket::getErrno() const int Socket::getErrno() const
@ -279,6 +281,18 @@ namespace ix
closesocket(fd); closesocket(fd);
#else #else
::close(fd); ::close(fd);
#endif
}
// FIXME: need finalize
bool Socket::init()
{
#ifdef _WIN32
INT rc;
WSADATA wsaData;
rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
return rc != 0;
#endif #endif
} }
} }

View File

@ -22,6 +22,8 @@ namespace ix
Socket(); Socket();
virtual ~Socket(); virtual ~Socket();
static bool init();
int hostname_connect(const std::string& hostname, int hostname_connect(const std::string& hostname,
int port, int port,
std::string& errMsg); std::string& errMsg);

View File

@ -19,8 +19,9 @@
# endif # endif
#endif #endif
#include <unistd.h> // #include <unistd.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include <cstdlib> #include <cstdlib>
#include <vector> #include <vector>
@ -272,7 +273,7 @@ namespace ix {
{ {
int N = (int) _rxbuf.size(); int N = (int) _rxbuf.size();
ssize_t ret; int ret;
_rxbuf.resize(N + 1500); _rxbuf.resize(N + 1500);
ret = _socket->recv((char*)&_rxbuf[0] + N, 1500); ret = _socket->recv((char*)&_rxbuf[0] + N, 1500);