From 3375eb60ae34d727757dfba703c0663ebb3cf42e Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Wed, 5 Jun 2019 17:23:44 +0300 Subject: [PATCH] More windows warning fixes --- components/common/cpp/src/uri.cpp | 4 +-- components/net/cpp/include/ftl/net/common.hpp | 22 ++++++++++++++++ .../net/cpp/include/ftl/net/listener.hpp | 15 +++-------- components/net/cpp/include/ftl/net/peer.hpp | 13 +++------- .../net/cpp/include/ftl/net/ws_internal.hpp | 2 +- components/net/cpp/src/listener.cpp | 2 +- components/net/cpp/src/peer.cpp | 25 ++++++------------- components/net/cpp/src/ws_internal.cpp | 22 +++++----------- 8 files changed, 47 insertions(+), 58 deletions(-) create mode 100644 components/net/cpp/include/ftl/net/common.hpp diff --git a/components/common/cpp/src/uri.cpp b/components/common/cpp/src/uri.cpp index 0ecedffc6..ab306786b 100644 --- a/components/common/cpp/src/uri.cpp +++ b/components/common/cpp/src/uri.cpp @@ -123,8 +123,8 @@ string URI::getBaseURI(int n) { return r; } else if (m_pathseg.size()+n >= 0) { string r = m_protostr + string("://") + m_host + ((m_port != 0) ? string(":") + std::to_string(m_port) : ""); - int N = m_pathseg.size()+n; - for (int i=0; i<N; i++) { + size_t N = m_pathseg.size()+n; + for (size_t i=0; i<N; i++) { r += "/"; r += getPathSegment(i); } diff --git a/components/net/cpp/include/ftl/net/common.hpp b/components/net/cpp/include/ftl/net/common.hpp new file mode 100644 index 000000000..61914d81a --- /dev/null +++ b/components/net/cpp/include/ftl/net/common.hpp @@ -0,0 +1,22 @@ +#ifndef _FTL_NET_COMMON_HPP_ +#define _FTL_NET_COMMON_HPP_ + +#ifndef WIN32 +#include <unistd.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netdb.h> +#include <arpa/inet.h> +#define INVALID_SOCKET -1 +#define SOCKET_ERROR -1 +#define SOCKET int +#endif + +#ifdef WIN32 +#include <winsock2.h> +#include <windows.h> +typedef int socklen_t; +#endif + +#endif // _FTL_NET_COMMON_HPP_ diff --git a/components/net/cpp/include/ftl/net/listener.hpp b/components/net/cpp/include/ftl/net/listener.hpp index 4c3bb83ac..44690ff63 100644 --- a/components/net/cpp/include/ftl/net/listener.hpp +++ b/components/net/cpp/include/ftl/net/listener.hpp @@ -1,14 +1,7 @@ #ifndef _FTL_NET_LISTENER_HPP_ #define _FTL_NET_LISTENER_HPP_ -#ifndef WIN32 -#include <netinet/in.h> -#endif - -#ifdef WIN32 -//#include <windows.h> -#include <winsock2.h> -#endif +#include <ftl/net/common.hpp> #include <ftl/net/handlers.hpp> #include <ftl/net/peer.hpp> @@ -23,12 +16,12 @@ class Protocol; class Listener { public: explicit Listener(const char *uri); - explicit Listener(int sfd) : descriptor_(sfd), default_proto_(nullptr) {} + explicit Listener(SOCKET sfd) : descriptor_(sfd), default_proto_(nullptr) {} virtual ~Listener(); bool isListening() { return descriptor_ >= 0; } void close(); - int _socket() { return descriptor_; } + SOCKET _socket() { return descriptor_; } void setProtocol(Protocol *p) { default_proto_ = p; } @@ -36,7 +29,7 @@ class Listener { void onConnection(connecthandler_t h) { handler_connect_.push_back(h); }; private: - int descriptor_; + SOCKET descriptor_; Protocol *default_proto_; sockaddr_in slocalAddr; std::vector<connecthandler_t> handler_connect_; diff --git a/components/net/cpp/include/ftl/net/peer.hpp b/components/net/cpp/include/ftl/net/peer.hpp index 0a16dd271..07cffaf51 100644 --- a/components/net/cpp/include/ftl/net/peer.hpp +++ b/components/net/cpp/include/ftl/net/peer.hpp @@ -5,12 +5,7 @@ #define NOMINMAX #endif -#ifndef WIN32 -#define INVALID_SOCKET -1 -#include <netinet/in.h> -#else -#include <winsock2.h> -#endif +#include <ftl/net/common.hpp> //#define GLOG_NO_ABBREVIATED_SEVERITIES #include <loguru.hpp> @@ -74,7 +69,7 @@ class Peer { public: explicit Peer(const char *uri, ftl::net::Dispatcher *d=nullptr); - explicit Peer(int s, ftl::net::Dispatcher *d=nullptr); + explicit Peer(SOCKET s, ftl::net::Dispatcher *d=nullptr); ~Peer(); /** @@ -172,7 +167,7 @@ class Peer { * Get the internal OS dependent socket. * TODO(nick) Work out if this should be private. */ - int _socket() const { return sock_; }; + SOCKET _socket() const { return sock_; }; /** * Internal handlers for specific event types. This should be private but @@ -198,7 +193,7 @@ class Peer { private: // Data Status status_; - int sock_; + SOCKET sock_; ftl::URI::scheme_t scheme_; uint32_t version_; diff --git a/components/net/cpp/include/ftl/net/ws_internal.hpp b/components/net/cpp/include/ftl/net/ws_internal.hpp index 94354b990..5390f1028 100644 --- a/components/net/cpp/include/ftl/net/ws_internal.hpp +++ b/components/net/cpp/include/ftl/net/ws_internal.hpp @@ -42,7 +42,7 @@ int ws_dispatch(const char *data, size_t len, std::function<void(const wsheader_ * Websocket header constructor. Fills a buffer with the correct websocket * header for a given opcode, mask setting and message length. */ -size_t ws_prepare(wsheader_type::opcode_type, bool useMask, size_t len, char *buffer, size_t maxlen); +int ws_prepare(wsheader_type::opcode_type, bool useMask, size_t len, char *buffer, size_t maxlen); bool ws_connect(int sockfd, const ftl::URI &uri); diff --git a/components/net/cpp/src/listener.cpp b/components/net/cpp/src/listener.cpp index 01ab5efa7..b8f276a10 100644 --- a/components/net/cpp/src/listener.cpp +++ b/components/net/cpp/src/listener.cpp @@ -91,7 +91,7 @@ SOCKET tcpListen(URI &uri) { return ssock; } -int wsListen(URI &uri) { +SOCKET wsListen(URI &uri) { return INVALID_SOCKET; } diff --git a/components/net/cpp/src/peer.cpp b/components/net/cpp/src/peer.cpp index 4f76a98ff..370f413df 100644 --- a/components/net/cpp/src/peer.cpp +++ b/components/net/cpp/src/peer.cpp @@ -6,11 +6,11 @@ #define NOMINMAX #endif +#include <ftl/net/common.hpp> + #include <fcntl.h> #ifdef WIN32 -#include <winsock2.h> #include <Ws2tcpip.h> -#include <windows.h> #endif #ifdef WIN32 @@ -24,17 +24,6 @@ #include <ftl/config.h> #include "net_internal.hpp" -#ifndef WIN32 -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#define INVALID_SOCKET -1 -#define SOCKET_ERROR -1 -#endif - #include <iostream> #include <memory> #include <algorithm> @@ -68,7 +57,7 @@ ftl::UUID ftl::net::this_peer; static ctpl::thread_pool pool(5); // TODO(nick) Move to tcp_internal.cpp -static int tcpConnect(URI &uri) { +static SOCKET tcpConnect(URI &uri) { int rc; sockaddr_in destAddr; @@ -81,7 +70,7 @@ static int tcpConnect(URI &uri) { #endif //We want a TCP socket - int csocket = socket(AF_INET, SOCK_STREAM, 0); + SOCKET csocket = socket(AF_INET, SOCK_STREAM, 0); if (csocket == INVALID_SOCKET) { LOG(ERROR) << "Unable to create TCP socket"; @@ -140,7 +129,7 @@ static int tcpConnect(URI &uri) { return csocket; } -Peer::Peer(int s, Dispatcher *d) : sock_(s) { +Peer::Peer(SOCKET s, Dispatcher *d) : sock_(s) { status_ = (s == INVALID_SOCKET) ? kInvalid : kConnecting; _updateURI(); @@ -541,10 +530,10 @@ int Peer::_send() { auto send_size = send_buf_.vector_size(); int c = 0; for (int i = 0; i < send_size; i++) { - c += ftl::net::internal::send(sock_, (char*)send_vec[i].iov_base, send_vec[i].iov_len, 0); + c += ftl::net::internal::send(sock_, (char*)send_vec[i].iov_base, (int)send_vec[i].iov_len, 0); } #else - int c = ftl::net::internal::writev(sock_, send_buf_.vector(), send_buf_.vector_size()); + int c = ftl::net::internal::writev(sock_, send_buf_.vector(), (int)send_buf_.vector_size()); #endif send_buf_.clear(); diff --git a/components/net/cpp/src/ws_internal.cpp b/components/net/cpp/src/ws_internal.cpp index 5c0726de2..e8387d939 100644 --- a/components/net/cpp/src/ws_internal.cpp +++ b/components/net/cpp/src/ws_internal.cpp @@ -5,25 +5,15 @@ //#define GLOG_NO_ABBREVIATED_SEVERITIES #include <loguru.hpp> +#include <ftl/net/common.hpp> + #include <cstring> #include <ftl/net/ws_internal.hpp> #include <memory> -#ifndef WIN32 -#include <unistd.h> -#include <sys/types.h> -#include <sys/socket.h> -#include <netinet/in.h> -#include <netdb.h> -#include <arpa/inet.h> -#define INVALID_SOCKET -1 -#define SOCKET_ERROR -1 -#endif #ifdef WIN32 -#include <winsock2.h> #include <Ws2tcpip.h> -#include <windows.h> #endif #include <string> @@ -88,7 +78,7 @@ int ftl::net::ws_dispatch(const char *data, size_t len, std::function<void(const return (int)(ws.header_size+ws.N); } -size_t ftl::net::ws_prepare(wsheader_type::opcode_type op, bool useMask, size_t len, char *data, size_t maxlen) { +int ftl::net::ws_prepare(wsheader_type::opcode_type op, bool useMask, size_t len, char *data, size_t maxlen) { // TODO: // Masking key should (must) be derived from a high quality random // number generator, to mitigate attacks on non-WebSocket friendly @@ -138,10 +128,10 @@ size_t ftl::net::ws_prepare(wsheader_type::opcode_type op, bool useMask, size_t } } - return header_size; + return (int)header_size; } -bool ftl::net::ws_connect(int sockfd, const URI &uri) { +bool ftl::net::ws_connect(SOCKET sockfd, const URI &uri) { string http = ""; int status; int i; @@ -158,7 +148,7 @@ bool ftl::net::ws_connect(int sockfd, const URI &uri) { http += "Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==\r\n"; http += "Sec-WebSocket-Version: 13\r\n"; http += "\r\n"; - int rc = (int)::send(sockfd, http.c_str(), http.length(), 0); + int rc = ::send(sockfd, http.c_str(), (int)http.length(), 0); if (rc != (int)http.length()) { LOG(ERROR) << "Could not send Websocket http request..."; std::cout << http; -- GitLab