THRIFT-1031 Patch to compile Thrift for vc++ 9.0 and 10.0
=> some more improvements

Patch: James Dickson

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1174801 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/cpp/libthrift.vcxproj b/lib/cpp/libthrift.vcxproj
index 0e4c942..5896726 100644
--- a/lib/cpp/libthrift.vcxproj
+++ b/lib/cpp/libthrift.vcxproj
@@ -35,8 +35,8 @@
       <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
     </ClCompile>
     <ClCompile Include="src\concurrency\Util.cpp">
-      <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">HAVE_GETTIMEOFDAY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
-      <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">HAVE_GETTIMEOFDAY;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(PreprocessorDefinitions)</PreprocessorDefinitions>
     </ClCompile>
     <ClCompile Include="src\processor\PeekProcessor.cpp">
       <PreprocessorDefinitions Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">_CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
@@ -217,7 +217,8 @@
       <PrecompiledHeader>NotUsing</PrecompiledHeader>
       <WarningLevel>Level3</WarningLevel>
       <Optimization>Disabled</Optimization>
-      <PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;NOMINMAX;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ForcedIncludeFiles>force_inc.h;%(ForcedIncludeFiles)</ForcedIncludeFiles>
     </ClCompile>
     <Link>
       <SubSystem>Windows</SubSystem>
@@ -231,7 +232,8 @@
       <Optimization>MaxSpeed</Optimization>
       <FunctionLevelLinking>true</FunctionLevelLinking>
       <IntrinsicFunctions>true</IntrinsicFunctions>
-      <PreprocessorDefinitions>WIN32;HAVE_CONFIG_H;NOMINMAX;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <ForcedIncludeFiles>force_inc.h;%(ForcedIncludeFiles)</ForcedIncludeFiles>
     </ClCompile>
     <Link>
       <SubSystem>Windows</SubSystem>
diff --git a/lib/cpp/src/Thrift.h b/lib/cpp/src/Thrift.h
index 97f7987..60a66f4 100644
--- a/lib/cpp/src/Thrift.h
+++ b/lib/cpp/src/Thrift.h
@@ -20,6 +20,10 @@
 #ifndef _THRIFT_THRIFT_H_
 #define _THRIFT_THRIFT_H_ 1
 
+#ifdef _WIN32
+#include "windows/config.h"
+#endif
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
diff --git a/lib/cpp/src/transport/TSocket.cpp b/lib/cpp/src/transport/TSocket.cpp
index 48d0007..2db8f8b 100644
--- a/lib/cpp/src/transport/TSocket.cpp
+++ b/lib/cpp/src/transport/TSocket.cpp
@@ -181,10 +181,6 @@
 
 void TSocket::openConnection(struct addrinfo *res) {
 
-#ifdef _WIN32
-    TWinsockSingleton::create();
-#endif // _WIN32
-
   if (isOpen()) {
     return;
   }
@@ -276,7 +272,7 @@
     goto done;
   }
 
-  if (errno != EINPROGRESS) {
+  if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK)) {
     int errno_copy = errno;
     GlobalOutput.perror("TSocket::open() connect() " + getSocketInfo(), errno_copy);
     throw TTransportException(TTransportException::NOT_OPEN, "connect() failed", errno_copy);
@@ -346,6 +342,11 @@
 }
 
 void TSocket::local_open(){
+
+#ifdef _WIN32
+    TWinsockSingleton::create();
+#endif // _WIN32
+
   if (isOpen()) {
     return;
   }
diff --git a/lib/cpp/src/windows/TargetVersion.h b/lib/cpp/src/windows/TargetVersion.h
index 7374666..c8498b1 100644
--- a/lib/cpp/src/windows/TargetVersion.h
+++ b/lib/cpp/src/windows/TargetVersion.h
@@ -28,6 +28,9 @@
 #error This is a MSVC header only.
 #endif
 
+#include <winsdkver.h>
+
+#define _WIN32_WINNT 0x0601
 #include <SDKDDKVer.h>
 
 #endif //_THIRFT_WINDOWS_TARGETVERSION_H_
diff --git a/lib/cpp/src/windows/config.h b/lib/cpp/src/windows/config.h
index 29a01f6..0f9a304 100644
--- a/lib/cpp/src/windows/config.h
+++ b/lib/cpp/src/windows/config.h
@@ -28,6 +28,11 @@
 #error This is a MSVC header only.
 #endif
 
+#pragma warning(disable: 4996) // Depreciated posix name.
+#pragma warning(disable: 4250) // Inherits via dominance.
+
+#define HAVE_GETTIMEOFDAY 1
+
 #include "TargetVersion.h"
 #include "GetTimeOfDay.h"
 #include "Operators.h"
@@ -50,27 +55,31 @@
 // pthreads
 #include <pthread.h>
 
-//"asm/posix_types.h"
-typedef unsigned int __kernel_size_t;
-typedef int          __kernel_ssize_t;
-
-//"linux/types.h"
-typedef __kernel_size_t  size_t;
-typedef __kernel_ssize_t ssize_t;
-
-// Missing defines.
-#define __BYTE_ORDER __LITTLE_ENDIAN
+typedef ptrdiff_t ssize_t;
 
 // Missing functions.
 #define usleep(ms) Sleep(ms)
 
-#if WINVER == 0x0501
-#   define poll(fds, nfds, timeout) \
-    select(0, NULL, fds, NULL, timeout)
-#else
-#   define poll(fds, nfds, timeout) \
-    WSAPoll(fds, nfds, timeout)
-#endif // WINVER
+#define poll(fds, nfds, timeout) \
+    poll_win32(fds, nfds, timeout)
+
+inline int poll_win32(LPWSAPOLLFD fdArray, ULONG fds, INT timeout)
+{
+    fd_set read_fds;
+    fd_set write_fds;
+    fd_set except_fds;
+
+    FD_ZERO(&read_fds);
+    FD_ZERO(&write_fds);
+    FD_ZERO(&except_fds);
+
+    FD_SET(fdArray[0].fd, &read_fds);
+    FD_SET(fdArray[0].fd, &write_fds);
+    FD_SET(fdArray[0].fd, &except_fds);
+
+    timeval time_out = {timeout * 0.001, timeout * 1000};
+    return select(1, &read_fds, &write_fds, &except_fds, &time_out);
+}
 
 inline void close(SOCKET socket)
 {
diff --git a/lib/cpp/src/windows/force_inc.h b/lib/cpp/src/windows/force_inc.h
index aa81c42..a85b880 100644
--- a/lib/cpp/src/windows/force_inc.h
+++ b/lib/cpp/src/windows/force_inc.h
@@ -28,6 +28,10 @@
 #error This is a MSVC header only.
 #endif
 
+#define NOMINMAX
+
+#include "windows/config.h"
+
 #undef gai_strerror
 #define gai_strerror gai_strerrorA