From c4e0cf6d48834d9eb53a27c8d704e957c23a182d Mon Sep 17 00:00:00 2001 From: zodiac403 Date: Tue, 31 Oct 2017 06:42:18 +0100 Subject: [PATCH] Fix Visual C++ Compiler Warnings: (#33) - Enable 'TreatWarningsAsErrors' in build config. - For Win, convert socket buffer sizes to INT. - For Win, suppress IPv6 warning. --- .gitignore | 1 + msvc15/tacopie.vcxproj | 12 ++++++++---- sources/network/common/tcp_socket.cpp | 15 +++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index cdbcb35..e0b9472 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,7 @@ target *.exe *.out *.app +.vs/ *.sln *.vcxproj *.suo diff --git a/msvc15/tacopie.vcxproj b/msvc15/tacopie.vcxproj index 8db4f4e..0653f53 100644 --- a/msvc15/tacopie.vcxproj +++ b/msvc15/tacopie.vcxproj @@ -76,13 +76,13 @@ SOFTWARE. StaticLibrary true - v141 + v140 Unicode StaticLibrary false - v141 + v140 true Unicode @@ -95,7 +95,7 @@ SOFTWARE. StaticLibrary false - v141 + v140 true Unicode @@ -133,6 +133,7 @@ SOFTWARE. Disabled WIN32;_DEBUG;_LIB;_WIN32;%(PreprocessorDefinitions) ..\includes;%(AdditionalIncludeDirectories) + true Windows @@ -146,6 +147,7 @@ SOFTWARE. Disabled _DEBUG;_LIB;_WIN32;_WIN64;%(PreprocessorDefinitions) ..\includes;%(AdditionalIncludeDirectories) + true Windows @@ -161,6 +163,7 @@ SOFTWARE. true WIN32;NDEBUG;_LIB;_WIN32;%(PreprocessorDefinitions) ..\includes;%(AdditionalIncludeDirectories) + true Windows @@ -178,6 +181,7 @@ SOFTWARE. true NDEBUG;_LIB;_WIN32;_WIN64;%(PreprocessorDefinitions) ..\includes;%(AdditionalIncludeDirectories) + true Windows @@ -188,4 +192,4 @@ SOFTWARE. - \ No newline at end of file + diff --git a/sources/network/common/tcp_socket.cpp b/sources/network/common/tcp_socket.cpp index efc65d8..2324d8f 100644 --- a/sources/network/common/tcp_socket.cpp +++ b/sources/network/common/tcp_socket.cpp @@ -43,6 +43,13 @@ #define SOCKET_ERROR -1 #endif /* SOCKET_ERROR */ +#if _WIN32 +#define __TACOPIE_LENGTH(size) static_cast(size) // for Windows, convert buffer size to `int` +#pragma warning ( disable: 4996 ) // for Windows, `inet_ntoa` is deprecated as it does not support IPv6 +#else +#define __TACOPIE_LENGTH(size) size // for Unix, keep buffer size as `size_t` +#endif /* _WIN32 */ + namespace tacopie { //! @@ -92,7 +99,7 @@ tcp_socket::recv(std::size_t size_to_read) { std::vector data(size_to_read, 0); - ssize_t rd_size = ::recv(m_fd, const_cast(data.data()), size_to_read, 0); + ssize_t rd_size = ::recv(m_fd, const_cast(data.data()), __TACOPIE_LENGTH(size_to_read), 0); if (rd_size == SOCKET_ERROR) { __TACOPIE_THROW(error, "recv() failure"); } @@ -108,7 +115,7 @@ tcp_socket::send(const std::vector& data, std::size_t size_to_write) { create_socket_if_necessary(); check_or_set_type(type::CLIENT); - ssize_t wr_size = ::send(m_fd, data.data(), size_to_write, 0); + ssize_t wr_size = ::send(m_fd, data.data(), __TACOPIE_LENGTH(size_to_write), 0); if (wr_size == SOCKET_ERROR) { __TACOPIE_THROW(error, "send() failure"); } @@ -124,7 +131,7 @@ tcp_socket::listen(std::size_t max_connection_queue) { create_socket_if_necessary(); check_or_set_type(type::SERVER); - if (::listen(m_fd, max_connection_queue) == SOCKET_ERROR) { __TACOPIE_THROW(debug, "listen() failure"); } + if (::listen(m_fd, __TACOPIE_LENGTH(max_connection_queue)) == SOCKET_ERROR) { __TACOPIE_THROW(debug, "listen() failure"); } } tcp_socket @@ -143,7 +150,7 @@ tcp_socket::accept(void) { } //! -//! check whether the current socket has an approriate type for that kind of operation +//! check whether the current socket has an appropriate type for that kind of operation //! if current type is UNKNOWN, update internal type with given type //!