From 84e4a3c1819346dc5e5fcc4c4701e6c43afa1041 Mon Sep 17 00:00:00 2001 From: Roger Meier Date: Fri, 16 Sep 2011 20:58:44 +0000 Subject: [PATCH] THRIFT-1031 Patch to compile Thrift for vc++ 9.0 and 10.0 Patch: James Dickson and Alexandre Parenteau git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1171777 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/windows/config.h | 42 +++ compiler/cpp/src/windows/version.h | 42 +++ lib/cpp/README_WINDOWS | 90 +++++++ lib/cpp/libthrift.vcxproj | 246 +++++++++++++++++ lib/cpp/libthrift.vcxproj.filters | 250 ++++++++++++++++++ lib/cpp/libthriftnb.vcxproj | 109 ++++++++ lib/cpp/libthriftnb.vcxproj.filters | 54 ++++ .../src/concurrency/PosixThreadFactory.cpp | 15 ++ lib/cpp/src/server/TNonblockingServer.h | 6 +- lib/cpp/src/transport/TServerSocket.cpp | 56 +++- lib/cpp/src/transport/TSocket.cpp | 59 ++++- lib/cpp/src/windows/Fcntl.cpp | 53 ++++ lib/cpp/src/windows/Fcntl.h | 41 +++ lib/cpp/src/windows/GetTimeOfDay.cpp | 92 +++++++ lib/cpp/src/windows/GetTimeOfDay.h | 33 +++ lib/cpp/src/windows/Operators.h | 40 +++ lib/cpp/src/windows/SocketPair.cpp | 96 +++++++ lib/cpp/src/windows/SocketPair.h | 33 +++ lib/cpp/src/windows/StdAfx.cpp | 20 ++ lib/cpp/src/windows/StdAfx.h | 38 +++ lib/cpp/src/windows/TWinsockSingleton.cpp | 63 +++++ lib/cpp/src/windows/TWinsockSingleton.h | 77 ++++++ lib/cpp/src/windows/TargetVersion.h | 33 +++ lib/cpp/src/windows/config.h | 80 ++++++ lib/cpp/src/windows/force_inc.h | 53 ++++ lib/cpp/src/windows/tr1/functional | 20 ++ lib/cpp/thrift.sln | 31 +++ 27 files changed, 1750 insertions(+), 22 deletions(-) create mode 100644 compiler/cpp/src/windows/config.h create mode 100644 compiler/cpp/src/windows/version.h create mode 100644 lib/cpp/README_WINDOWS create mode 100644 lib/cpp/libthrift.vcxproj create mode 100644 lib/cpp/libthrift.vcxproj.filters create mode 100644 lib/cpp/libthriftnb.vcxproj create mode 100644 lib/cpp/libthriftnb.vcxproj.filters create mode 100644 lib/cpp/src/windows/Fcntl.cpp create mode 100644 lib/cpp/src/windows/Fcntl.h create mode 100644 lib/cpp/src/windows/GetTimeOfDay.cpp create mode 100644 lib/cpp/src/windows/GetTimeOfDay.h create mode 100644 lib/cpp/src/windows/Operators.h create mode 100644 lib/cpp/src/windows/SocketPair.cpp create mode 100644 lib/cpp/src/windows/SocketPair.h create mode 100644 lib/cpp/src/windows/StdAfx.cpp create mode 100644 lib/cpp/src/windows/StdAfx.h create mode 100644 lib/cpp/src/windows/TWinsockSingleton.cpp create mode 100644 lib/cpp/src/windows/TWinsockSingleton.h create mode 100644 lib/cpp/src/windows/TargetVersion.h create mode 100644 lib/cpp/src/windows/config.h create mode 100644 lib/cpp/src/windows/force_inc.h create mode 100644 lib/cpp/src/windows/tr1/functional create mode 100644 lib/cpp/thrift.sln diff --git a/compiler/cpp/src/windows/config.h b/compiler/cpp/src/windows/config.h new file mode 100644 index 00000000..a69b5463 --- /dev/null +++ b/compiler/cpp/src/windows/config.h @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding cogoright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a cogo of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_CONFIG_H_ +#define _THRIFT_WINDOWS_CONFIG_H_ 1 + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error "This is a Windows header only" +#endif + +#include +#include +#include + +#define strtoll(begin_ptr, end_ptr, length) strtol(begin_ptr, end_ptr, length) + +#define PRIu64 "I64d" +#define PRIi64 "I64d" + +#pragma warning(disable:4996) + +#endif // _THRIFT_WINDOWS_CONFIG_H_ diff --git a/compiler/cpp/src/windows/version.h b/compiler/cpp/src/windows/version.h new file mode 100644 index 00000000..fab932f3 --- /dev/null +++ b/compiler/cpp/src/windows/version.h @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding cogoright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a cogo of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_VERSION_H_ +#define _THRIFT_WINDOWS_VERSION_H_ 1 + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error "This is a Windows header only" +#endif + +#define PATH_MAX MAX_PATH +#define THRIFT_VERSION "0.8.0-dev" + +#ifndef S_ISDIR +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#endif + +#ifndef S_ISREG +#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG) +#endif + +#endif // _THRIFT_WINDOWS_VERSION_H_ diff --git a/lib/cpp/README_WINDOWS b/lib/cpp/README_WINDOWS new file mode 100644 index 00000000..27520bcf --- /dev/null +++ b/lib/cpp/README_WINDOWS @@ -0,0 +1,90 @@ +Thrift C++ Software Library + +License +======= + +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you under the Apache License, Version 2.0 (the +"License"); you may not use this file except in compliance +with the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, +software distributed under the License is distributed on an +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. See the License for the +specific language governing permissions and limitations +under the License. + +Using Thrift with C++ +===================== + +You need to define an enviroment variable called THIRD_PARTY. The project +assumes that you have extracted the dependancies into their default structure +into the path defined by THIRD_PARTY. + +e.g. $(THIRD_PARTY)/boost/boost_1_47_0/ + +Thrift is divided into two libraries. + +libthrift + The core Thrift library contains all the core Thrift code. It requires + boost shared pointers and pthreads_win32. + +libthriftnb + This library contains the Thrift nonblocking server, which uses libevent. + To link this library you will also need to link libevent. + +You MUST apply this patch to make generated code compile. +https://issues.apache.org/jira/browse/THRIFT-1139 + +Linking Against Thrift +====================== + +You need to link your project that uses thrift against all the thrift +dependancies; in the case of libthrift, pthreads_win32, boost and for +libthriftnb, libevent. + +In the project properties you must also set HAVE_CONFIG_H as force include +the config header: "windows/confg.h" + +Dependencies +============ + +boost shared pointers +http://www.boost.org/libs/smart_ptr/smart_ptr.htm + +libevent (for libthriftnb only) +http://monkey.org/~provos/libevent/ + +pthreads win32 +http://sources.redhat.com/pthreads-win32/ + +Known issues +============ + +- Endianess has not been fully tested, may not work with doubles. +- Currently does not support the non-blocking connect path. +- Only supports the creation of clients, server sockets are not yet ported. +- Does not support named pipes. (Supported in unix through unix domain sockets). + +TODO +==== + +- Port remaining classes in libthrift: + - TFDTransport + - TFileTransport + - THttpClient + - THttpServer + - TSimpleFileTransport + - TSSLSocket + +- Port test cases. (Not even started this. Run test cases in release mode?) + +- Autolink libraries depending on debug\release build. + +- Auto versioning. diff --git a/lib/cpp/libthrift.vcxproj b/lib/cpp/libthrift.vcxproj new file mode 100644 index 00000000..0e4c9427 --- /dev/null +++ b/lib/cpp/libthrift.vcxproj @@ -0,0 +1,246 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + config.h + config.h + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + HAVE_GETTIMEOFDAY;%(PreprocessorDefinitions) + HAVE_GETTIMEOFDAY;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + true + true + + + true + true + + + true + true + + + true + true + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + force_inc.h + force_inc.h + + + true + true + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + force_inc.h + force_inc.h + + + true + true + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + + + + NotUsing + NotUsing + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {DD26F57E-60F2-4F37-A616-D219A9BF338F} + Win32Proj + thrift + libthrift + + + + StaticLibrary + true + MultiByte + + + StaticLibrary + false + true + MultiByte + + + + + + + + + + + + + $(ProjectDir)\src\;$(ProjectDir)\src\windows\;$(ProjectDir)\src\transport\;$(THIRD_PARTY)\boost\boost_1_47_0\;$(THIRD_PARTY)\pthreads\;$(THIRD_PARTY)\openssl\OpenSSL-Win32\include\;$(IncludePath) + + + $(ProjectDir)\src\;$(ProjectDir)\src\windows\;$(ProjectDir)\src\transport\;$(THIRD_PARTY)\boost\boost_1_47_0\;$(THIRD_PARTY)\pthreads\;$(THIRD_PARTY)\openssl\OpenSSL-Win32\include\;$(IncludePath) + + + + NotUsing + Level3 + Disabled + WIN32;HAVE_CONFIG_H;NOMINMAX;_DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + + + + + Level3 + NotUsing + MaxSpeed + true + true + WIN32;HAVE_CONFIG_H;NOMINMAX;NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + + \ No newline at end of file diff --git a/lib/cpp/libthrift.vcxproj.filters b/lib/cpp/libthrift.vcxproj.filters new file mode 100644 index 00000000..8d3ce66a --- /dev/null +++ b/lib/cpp/libthrift.vcxproj.filters @@ -0,0 +1,250 @@ + + + + + transport + + + + + windows + + + concurrency + + + concurrency + + + transport + + + windows + + + concurrency + + + concurrency + + + concurrency + + + concurrency + + + protocal + + + protocal + + + protocal + + + protocal + + + transport + + + transport + + + transport + + + transport + + + transport + + + transport + + + transport + + + transport + + + server + + + server + + + async + + + processor + + + transport + + + transport + + + windows + + + windows + + + windows + + + + + transport + + + transport + + + protocal + + + + + + windows + + + windows + + + concurrency + + + concurrency + + + concurrency + + + transport + + + transport + + + transport + + + windows + + + transport + + + windows + + + protocal + + + protocal + + + server + + + server + + + server + + + async + + + processor + + + transport + + + transport + + + transport + + + transport + + + transport + + + transport + + + transport + + + protocal + + + protocal + + + protocal + + + transport + + + windows + + + windows + + + windows + + + windows + + + windows + + + + + {07ced19b-b72a-4105-9ffb-6d2bcf64497e} + + + {e9f61404-1148-4103-bd6f-e5869d37fa79} + + + {2814002a-3c68-427e-b0eb-33acd2f406ae} + + + {addd4707-dbaa-4d0c-bef6-fff8be7b495a} + + + {f55a8e9b-6959-487f-a396-c31b4d6c61d6} + + + {d526885b-1b3e-4ee3-8027-e694fe98ad63} + + + {8f428da8-5a83-44fb-9578-de935fb415e1} + + + {eea10406-3380-4f2d-9365-c26fa2875dae} + + + + + protocal + + + windows\tr1 + + + \ No newline at end of file diff --git a/lib/cpp/libthriftnb.vcxproj b/lib/cpp/libthriftnb.vcxproj new file mode 100644 index 00000000..f672790c --- /dev/null +++ b/lib/cpp/libthriftnb.vcxproj @@ -0,0 +1,109 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + config.h + config.h + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + _CRT_SECURE_NO_WARNINGS;%(PreprocessorDefinitions) + + + + + + + + + + + + + + {D8696CCE-7D46-4659-B432-91754A41DEB0} + Win32Proj + libthriftnb + + + + StaticLibrary + true + MultiByte + + + StaticLibrary + false + true + MultiByte + + + + + + + + + + + + + $(ProjectDir)\src\;$(ProjectDir)\src\windows\;$(THIRD_PARTY)\boost\boost_1_47_0\;$(THIRD_PARTY)\pthreads\;$(THIRD_PARTY)\libevent\libevent-2.0.13-stable\WIN32-Code\;$(THIRD_PARTY)\libevent\libevent-2.0.13-stable\include;$(THIRD_PARTY)\libevent\libevent-2.0.13-stable\;$(IncludePath) + + + $(ProjectDir)\src\;$(ProjectDir)\src\windows\;$(THIRD_PARTY)\boost\boost_1_47_0\;$(THIRD_PARTY)\pthreads\;$(THIRD_PARTY)\libevent\libevent-2.0.13-stable\WIN32-Code\;$(THIRD_PARTY)\libevent\libevent-2.0.13-stable\include;$(THIRD_PARTY)\libevent\libevent-2.0.13-stable\;$(IncludePath) + + + + + + Level3 + Disabled + WIN32;HAVE_CONFIG_H;_DEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;HAVE_CONFIG_H;NDEBUG;_LIB;%(PreprocessorDefinitions) + + + Windows + true + true + true + + + + + + \ No newline at end of file diff --git a/lib/cpp/libthriftnb.vcxproj.filters b/lib/cpp/libthriftnb.vcxproj.filters new file mode 100644 index 00000000..943cce07 --- /dev/null +++ b/lib/cpp/libthriftnb.vcxproj.filters @@ -0,0 +1,54 @@ + + + + + {bf449d92-4be8-4f6f-a010-c536f57c6f13} + + + {0294d0a6-ce46-4be8-a659-826d6e98ae41} + + + {60fc9e5e-0866-4aba-8662-439bb4a461d3} + + + + + server + + + async + + + async + + + async + + + windows + + + + + server + + + async + + + async + + + async + + + windows + + + windows + + + windows + + + \ No newline at end of file diff --git a/lib/cpp/src/concurrency/PosixThreadFactory.cpp b/lib/cpp/src/concurrency/PosixThreadFactory.cpp index d42288c3..88849475 100644 --- a/lib/cpp/src/concurrency/PosixThreadFactory.cpp +++ b/lib/cpp/src/concurrency/PosixThreadFactory.cpp @@ -68,7 +68,11 @@ class PthreadThread: public Thread { public: PthreadThread(int policy, int priority, int stackSize, bool detached, shared_ptr runnable) : + +#ifndef _WIN32 pthread_(0), +#endif // _WIN32 + state_(uninitialized), policy_(policy), priority_(priority), @@ -152,7 +156,12 @@ class PthreadThread: public Thread { } Thread::id_t getId() { + +#ifndef _WIN32 return (Thread::id_t)pthread_; +#else + return (Thread::id_t)pthread_.p; +#endif // _WIN32 } shared_ptr runnable() const { return Thread::runnable(); } @@ -286,7 +295,13 @@ class PosixThreadFactory::Impl { void setDetached(bool value) { detached_ = value; } Thread::id_t getCurrentThreadId() const { + +#ifndef _WIN32 return (Thread::id_t)pthread_self(); +#else + return (Thread::id_t)pthread_self().p; +#endif // _WIN32 + } }; diff --git a/lib/cpp/src/server/TNonblockingServer.h b/lib/cpp/src/server/TNonblockingServer.h index a26fcc5e..da36045d 100644 --- a/lib/cpp/src/server/TNonblockingServer.h +++ b/lib/cpp/src/server/TNonblockingServer.h @@ -60,7 +60,11 @@ using apache::thrift::concurrency::ThreadManager; #endif #ifndef SOCKOPT_CAST_T -#define SOCKOPT_CAST_T void +# ifndef _WIN32 +# define SOCKOPT_CAST_T void +# else +# define SOCKOPT_CAST_T char +# endif // _WIN32 #endif template diff --git a/lib/cpp/src/transport/TServerSocket.cpp b/lib/cpp/src/transport/TServerSocket.cpp index efe08fe1..dfa55b43 100644 --- a/lib/cpp/src/transport/TServerSocket.cpp +++ b/lib/cpp/src/transport/TServerSocket.cpp @@ -52,6 +52,24 @@ #define AF_LOCAL AF_UNIX #endif +#ifndef SOCKOPT_CAST_T +# ifndef _WIN32 +# define SOCKOPT_CAST_T void +# else +# define SOCKOPT_CAST_T char +# endif // _WIN32 +#endif + +template +inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) { + return reinterpret_cast(v); +} + +template +inline SOCKOPT_CAST_T* cast_sockopt(T* v) { + return reinterpret_cast(v); +} + namespace apache { namespace thrift { namespace transport { using namespace std; @@ -182,7 +200,7 @@ void TServerSocket::listen() { // Set reusaddress to prevent 2MSL delay on accept int one = 1; if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_REUSEADDR, - &one, sizeof(one))) { + cast_sockopt(&one), sizeof(one))) { int errno_copy = errno; GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_REUSEADDR ", errno_copy); close(); @@ -192,7 +210,7 @@ void TServerSocket::listen() { // Set TCP buffer sizes if (tcpSendBuffer_ > 0) { if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_SNDBUF, - &tcpSendBuffer_, sizeof(tcpSendBuffer_))) { + cast_sockopt(&tcpSendBuffer_), sizeof(tcpSendBuffer_))) { int errno_copy = errno; GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_SNDBUF ", errno_copy); close(); @@ -202,7 +220,7 @@ void TServerSocket::listen() { if (tcpRecvBuffer_ > 0) { if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_RCVBUF, - &tcpRecvBuffer_, sizeof(tcpRecvBuffer_))) { + cast_sockopt(&tcpRecvBuffer_), sizeof(tcpRecvBuffer_))) { int errno_copy = errno; GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_RCVBUF ", errno_copy); close(); @@ -225,7 +243,7 @@ void TServerSocket::listen() { if (res->ai_family == AF_INET6 && path_.empty()) { int zero = 0; if (-1 == setsockopt(serverSocket_, IPPROTO_IPV6, IPV6_V6ONLY, - &zero, sizeof(zero))) { + cast_sockopt(&zero), sizeof(zero))) { GlobalOutput.perror("TServerSocket::listen() IPV6_V6ONLY ", errno); } } @@ -234,7 +252,7 @@ void TServerSocket::listen() { // Turn linger off, don't want to block on calls to close struct linger ling = {0, 0}; if (-1 == setsockopt(serverSocket_, SOL_SOCKET, SO_LINGER, - &ling, sizeof(ling))) { + cast_sockopt(&ling), sizeof(ling))) { int errno_copy = errno; GlobalOutput.perror("TServerSocket::listen() setsockopt() SO_LINGER ", errno_copy); close(); @@ -245,7 +263,7 @@ void TServerSocket::listen() { if (path_.empty()) { // TCP Nodelay, speed over bandwidth if (-1 == setsockopt(serverSocket_, IPPROTO_TCP, TCP_NODELAY, - &one, sizeof(one))) { + cast_sockopt(&one), sizeof(one))) { int errno_copy = errno; GlobalOutput.perror("TServerSocket::listen() setsockopt() TCP_NODELAY ", errno_copy); close(); @@ -273,6 +291,9 @@ void TServerSocket::listen() { int retries = 0; if (! path_.empty()) { + +#ifndef _WIN32 + // Unix Domain Socket struct sockaddr_un address; socklen_t len; @@ -303,6 +324,12 @@ void TServerSocket::listen() { // free addrinfo freeaddrinfo(res0); + +#else + GlobalOutput.perror("TSocket::open() Unix Domain socket path not supported on windows", -99); + throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path not supported"); +#endif + } // throw an error if we failed to bind properly @@ -368,7 +395,7 @@ shared_ptr TServerSocket::acceptImpl() { // Check for an interrupt signal if (intSock2_ >= 0 && (fds[1].revents & POLLIN)) { int8_t buf; - if (-1 == recv(intSock2_, &buf, sizeof(int8_t), 0)) { + if (-1 == recv(intSock2_, cast_sockopt(&buf), sizeof(int8_t), 0)) { GlobalOutput.perror("TServerSocket::acceptImpl() recv() interrupt ", errno); } throw TTransportException(TTransportException::INTERRUPTED); @@ -429,7 +456,7 @@ shared_ptr TServerSocket::createSocket(int clientSocket) { void TServerSocket::interrupt() { if (intSock1_ >= 0) { int8_t byte = 0; - if (-1 == send(intSock1_, &byte, sizeof(int8_t), 0)) { + if (-1 == send(intSock1_, cast_sockopt(&byte), sizeof(int8_t), 0)) { GlobalOutput.perror("TServerSocket::interrupt() send() ", errno); } } @@ -437,11 +464,18 @@ void TServerSocket::interrupt() { void TServerSocket::close() { if (serverSocket_ >= 0) { - shutdown(serverSocket_, SHUT_RDWR); - ::close(serverSocket_); + +#ifdef _WIN32 + shutdown(serverSocket_, SD_BOTH); + ::closesocket(serverSocket_); +#else + shutdown(serverSocket_, SHUT_RDWR); + ::close(serverSocket_); +#endif + } if (intSock1_ >= 0) { - ::close(intSock1_); + ::close(intSock1_); } if (intSock2_ >= 0) { ::close(intSock2_); diff --git a/lib/cpp/src/transport/TSocket.cpp b/lib/cpp/src/transport/TSocket.cpp index bee9d80d..48d00075 100644 --- a/lib/cpp/src/transport/TSocket.cpp +++ b/lib/cpp/src/transport/TSocket.cpp @@ -49,6 +49,24 @@ #include "TSocket.h" #include "TTransportException.h" +#ifndef SOCKOPT_CAST_T +# ifndef _WIN32 +# define SOCKOPT_CAST_T void +# else +# define SOCKOPT_CAST_T char +# endif // _WIN32 +#endif + +template +inline const SOCKOPT_CAST_T* const_cast_sockopt(const T* v) { + return reinterpret_cast(v); +} + +template +inline SOCKOPT_CAST_T* cast_sockopt(T* v) { + return reinterpret_cast(v); +} + namespace apache { namespace thrift { namespace transport { using namespace std; @@ -141,7 +159,7 @@ bool TSocket::peek() { return false; } uint8_t buf; - int r = recv(socket_, &buf, 1, MSG_PEEK); + int r = recv(socket_, cast_sockopt(&buf), 1, MSG_PEEK); if (r == -1) { int errno_copy = errno; #if defined __FreeBSD__ || defined __MACH__ @@ -162,6 +180,11 @@ bool TSocket::peek() { } void TSocket::openConnection(struct addrinfo *res) { + +#ifdef _WIN32 + TWinsockSingleton::create(); +#endif // _WIN32 + if (isOpen()) { return; } @@ -222,6 +245,9 @@ void TSocket::openConnection(struct addrinfo *res) { // Connect the socket int ret; if (! path_.empty()) { + +#ifndef _WIN32 + struct sockaddr_un address; socklen_t len; @@ -235,6 +261,12 @@ void TSocket::openConnection(struct addrinfo *res) { snprintf(address.sun_path, sizeof(address.sun_path), "%s", path_.c_str()); len = sizeof(address); ret = connect(socket_, (struct sockaddr *) &address, len); + +#else + GlobalOutput.perror("TSocket::open() Unix Domain socket path not supported on windows", -99); + throw TTransportException(TTransportException::NOT_OPEN, " Unix Domain socket path not supported"); +#endif + } else { ret = connect(socket_, res->ai_addr, res->ai_addrlen); } @@ -262,7 +294,7 @@ void TSocket::openConnection(struct addrinfo *res) { int val; socklen_t lon; lon = sizeof(int); - int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, (void *)&val, &lon); + int ret2 = getsockopt(socket_, SOL_SOCKET, SO_ERROR, cast_sockopt(&val), &lon); if (ret2 == -1) { int errno_copy = errno; GlobalOutput.perror("TSocket::open() getsockopt() " + getSocketInfo(), errno_copy); @@ -366,8 +398,15 @@ void TSocket::local_open(){ void TSocket::close() { if (socket_ >= 0) { - shutdown(socket_, SHUT_RDWR); - ::close(socket_); + +#ifdef _WIN32 + shutdown(socket_, SD_BOTH); + ::closesocket(socket_); +#else + shutdown(socket_, SHUT_RDWR); + ::close(socket_); +#endif + } socket_ = -1; } @@ -408,7 +447,7 @@ uint32_t TSocket::read(uint8_t* buf, uint32_t len) { // an EAGAIN is due to a timeout or an out-of-resource condition. begin.tv_sec = begin.tv_usec = 0; } - int got = recv(socket_, buf, len, 0); + int got = recv(socket_, cast_sockopt(buf), len, 0); int errno_copy = errno; //gettimeofday can change errno ++g_socket_syscalls; @@ -518,7 +557,7 @@ uint32_t TSocket::write_partial(const uint8_t* buf, uint32_t len) { flags |= MSG_NOSIGNAL; #endif // ifdef MSG_NOSIGNAL - int b = send(socket_, buf + sent, len - sent, flags); + int b = send(socket_, const_cast_sockopt(buf + sent), len - sent, flags); ++g_socket_syscalls; if (b < 0) { @@ -568,7 +607,7 @@ void TSocket::setLinger(bool on, int linger) { } struct linger l = {(lingerOn_ ? 1 : 0), lingerVal_}; - int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, &l, sizeof(l)); + int ret = setsockopt(socket_, SOL_SOCKET, SO_LINGER, cast_sockopt(&l), sizeof(l)); if (ret == -1) { int errno_copy = errno; // Copy errno because we're allocating memory. GlobalOutput.perror("TSocket::setLinger() setsockopt() " + getSocketInfo(), errno_copy); @@ -583,7 +622,7 @@ void TSocket::setNoDelay(bool noDelay) { // Set socket to NODELAY int v = noDelay_ ? 1 : 0; - int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); + int ret = setsockopt(socket_, IPPROTO_TCP, TCP_NODELAY, cast_sockopt(&v), sizeof(v)); if (ret == -1) { int errno_copy = errno; // Copy errno because we're allocating memory. GlobalOutput.perror("TSocket::setNoDelay() setsockopt() " + getSocketInfo(), errno_copy); @@ -612,7 +651,7 @@ void TSocket::setRecvTimeout(int ms) { // Copy because poll may modify struct timeval r = recvTimeval_; - int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, &r, sizeof(r)); + int ret = setsockopt(socket_, SOL_SOCKET, SO_RCVTIMEO, cast_sockopt(&r), sizeof(r)); if (ret == -1) { int errno_copy = errno; // Copy errno because we're allocating memory. GlobalOutput.perror("TSocket::setRecvTimeout() setsockopt() " + getSocketInfo(), errno_copy); @@ -634,7 +673,7 @@ void TSocket::setSendTimeout(int ms) { struct timeval s = {(int)(sendTimeout_/1000), (int)((sendTimeout_%1000)*1000)}; - int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, &s, sizeof(s)); + int ret = setsockopt(socket_, SOL_SOCKET, SO_SNDTIMEO, cast_sockopt(&s), sizeof(s)); if (ret == -1) { int errno_copy = errno; // Copy errno because we're allocating memory. GlobalOutput.perror("TSocket::setSendTimeout() setsockopt() " + getSocketInfo(), errno_copy); diff --git a/lib/cpp/src/windows/Fcntl.cpp b/lib/cpp/src/windows/Fcntl.cpp new file mode 100644 index 00000000..bd277735 --- /dev/null +++ b/lib/cpp/src/windows/Fcntl.cpp @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "Fcntl.h" + +// Win32 +#include + +int fcntl(int fd, int cmd, int flags) +{ + if(cmd != F_GETFL && cmd != F_SETFL) + { + return -1; + } + + if(flags != O_NONBLOCK && flags != 0) + { + return -1; + } + + if(cmd == F_GETFL) + { + return 0; + } + + int res; + if(flags) + { + res = ioctlsocket(fd, FIONBIO, reinterpret_cast(&(flags = 1))); + } + else + { + res = ioctlsocket(fd, FIONBIO, reinterpret_cast(&(flags = 0))); + } + + return res; +} diff --git a/lib/cpp/src/windows/Fcntl.h b/lib/cpp/src/windows/Fcntl.h new file mode 100644 index 00000000..40b70d6a --- /dev/null +++ b/lib/cpp/src/windows/Fcntl.h @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_FCNTL_H_ +#define _THRIFT_WINDOWS_FCNTL_H_ 1 + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +#define O_NONBLOCK 1 + +enum +{ + F_GETFL, + F_SETFL, +}; + +int fcntl(int fd, int cmd, int flags); + +#endif // _THRIFT_WINDOWS_FCNTL_H_ diff --git a/lib/cpp/src/windows/GetTimeOfDay.cpp b/lib/cpp/src/windows/GetTimeOfDay.cpp new file mode 100644 index 00000000..6201eda5 --- /dev/null +++ b/lib/cpp/src/windows/GetTimeOfDay.cpp @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "GetTimeOfDay.h" +#include "config.h" + +// win32 +#include + +#if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) +# define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 +#else +# define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL +#endif + +struct timezone +{ + int tz_minuteswest; /* minutes W of Greenwich */ + int tz_dsttime; /* type of dst correction */ +}; + +int gettimeofday(struct timeval * tv, struct timezone * tz) +{ + FILETIME ft; + unsigned __int64 tmpres(0); + static int tzflag; + + if (NULL != tv) + { + GetSystemTimeAsFileTime(&ft); + + tmpres |= ft.dwHighDateTime; + tmpres <<= 32; + tmpres |= ft.dwLowDateTime; + + /*converting file time to unix epoch*/ + tmpres -= DELTA_EPOCH_IN_MICROSECS; + tmpres /= 10; /*convert into microseconds*/ + tv->tv_sec = (long)(tmpres / 1000000UL); + tv->tv_usec = (long)(tmpres % 1000000UL); + } + + if (NULL != tz) + { + if (!tzflag) + { + _tzset(); + tzflag++; + } + + long time_zone(0); + errno_t err(_get_timezone(&time_zone)); + if (err == NO_ERROR) + { + tz->tz_minuteswest = time_zone / 60; + } + else + { + return -1; + } + + int day_light(0); + err = (_get_daylight(&day_light)); + if (err == NO_ERROR) + { + tz->tz_dsttime = day_light; + return 0; + } + else + { + return -1; + } + } + + return -1; +} diff --git a/lib/cpp/src/windows/GetTimeOfDay.h b/lib/cpp/src/windows/GetTimeOfDay.h new file mode 100644 index 00000000..f6bdf1cd --- /dev/null +++ b/lib/cpp/src/windows/GetTimeOfDay.h @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_GETTIMEOFDAY_H_ +#define _THRIFT_WINDOWS_GETTIMEOFDAY_H_ + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +int gettimeofday(struct timeval * tv, struct timezone * tz); + +#endif // _THRIFT_WINDOWS_GETTIMEOFDAY_H_ diff --git a/lib/cpp/src/windows/Operators.h b/lib/cpp/src/windows/Operators.h new file mode 100644 index 00000000..95d8e3e8 --- /dev/null +++ b/lib/cpp/src/windows/Operators.h @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_OPERATORS_H_ +#define _THRIFT_WINDOWS_OPERATORS_H_ + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +namespace apache { namespace thrift { + +class TEnumIterator; + +inline bool operator == (const TEnumIterator&, const TEnumIterator&) +{ + // Not entirely sure what the test should be here. It is only to enable + // iterator debugging and is not used in release mode. + return true; +} + +}} // apache::thrift + +#endif // _THRIFT_WINDOWS_OPERATORS_H_ diff --git a/lib/cpp/src/windows/SocketPair.cpp b/lib/cpp/src/windows/SocketPair.cpp new file mode 100644 index 00000000..5eee4ae9 --- /dev/null +++ b/lib/cpp/src/windows/SocketPair.cpp @@ -0,0 +1,96 @@ +/* socketpair.c + * Copyright 2007 by Nathan C. Myers ; some rights reserved. + * This code is Free Software. It may be copied freely, in original or + * modified form, subject only to the restrictions that (1) the author is + * relieved from all responsibilities for any use for any purpose, and (2) + * this copyright notice must be retained, unchanged, in its entirety. If + * for any reason the author might be held responsible for any consequences + * of copying or use, license is withheld. + */ + +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "SocketPair.h" + +// stl +#include + +// Win32 +#include +#include + +int socketpair(int d, int type, int protocol, int sv[2]) +{ + union { + struct sockaddr_in inaddr; + struct sockaddr addr; + } a; + SOCKET listener; + int e; + socklen_t addrlen = sizeof(a.inaddr); + DWORD flags = 0; + int reuse = 1; + + if (sv == 0) { + WSASetLastError(WSAEINVAL); + return SOCKET_ERROR; + } + + listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if (listener == INVALID_SOCKET) + return SOCKET_ERROR; + + memset(&a, 0, sizeof(a)); + a.inaddr.sin_family = AF_INET; + a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + a.inaddr.sin_port = 0; + + sv[0] = sv[1] = INVALID_SOCKET; + do { + if (setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, + (char*) &reuse, (socklen_t) sizeof(reuse)) == -1) + break; + if (bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR) + break; + if (getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR) + break; + if (listen(listener, 1) == SOCKET_ERROR) + break; + sv[0] = WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags); + if (sv[0] == INVALID_SOCKET) + break; + if (connect(sv[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR) + break; + sv[1] = accept(listener, NULL, NULL); + if (sv[1] == INVALID_SOCKET) + break; + + closesocket(listener); + return 0; + + } while (0); + + e = WSAGetLastError(); + closesocket(listener); + closesocket(sv[0]); + closesocket(sv[1]); + WSASetLastError(e); + return SOCKET_ERROR; +} diff --git a/lib/cpp/src/windows/SocketPair.h b/lib/cpp/src/windows/SocketPair.h new file mode 100644 index 00000000..27ec9b18 --- /dev/null +++ b/lib/cpp/src/windows/SocketPair.h @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_SOCKETPAIR_H_ +#define _THRIFT_WINDOWS_SOCKETPAIR_H_ 1 + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +int socketpair(int d, int type, int protocol, int sv[2]); + +#endif // _THRIFT_WINDOWS_SOCKETPAIR_H_ diff --git a/lib/cpp/src/windows/StdAfx.cpp b/lib/cpp/src/windows/StdAfx.cpp new file mode 100644 index 00000000..5e49487b --- /dev/null +++ b/lib/cpp/src/windows/StdAfx.cpp @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "stdafx.h" diff --git a/lib/cpp/src/windows/StdAfx.h b/lib/cpp/src/windows/StdAfx.h new file mode 100644 index 00000000..b953b22b --- /dev/null +++ b/lib/cpp/src/windows/StdAfx.h @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_STDAFX_H_ +#define _THRIFT_WINDOWS_STDAFX_H_ + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +#include "TargetVersion.h" +#include "Config.h" + +// Exclude rarely-used stuff from Windows headers +#define WIN32_LEAN_AND_MEAN +#include + +#endif // _THRIFT_WINDOWS_STDAFX_H_ diff --git a/lib/cpp/src/windows/TWinsockSingleton.cpp b/lib/cpp/src/windows/TWinsockSingleton.cpp new file mode 100644 index 00000000..aae25ab5 --- /dev/null +++ b/lib/cpp/src/windows/TWinsockSingleton.cpp @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "StdAfx.h" +#include "TWinsockSingleton.h" + +// boost +#include + +namespace apache { namespace thrift { namespace transport { + +TWinsockSingleton::instance_ptr TWinsockSingleton::instance_ptr_(NULL); +boost::once_flag TWinsockSingleton::flags_ = BOOST_ONCE_INIT; + +//------------------------------------------------------------------------------ +TWinsockSingleton::TWinsockSingleton(void) +{ + WORD version(MAKEWORD(2, 2)); + WSAData data = {0}; + + int error(WSAStartup(version, &data)); + if (error != 0) + { + BOOST_ASSERT(false); + throw std::runtime_error("Failed to initialise Winsock."); + } +} + +//------------------------------------------------------------------------------ +TWinsockSingleton::~TWinsockSingleton(void) +{ + WSACleanup(); +} + +//------------------------------------------------------------------------------ +void TWinsockSingleton::create(void) +{ + boost::call_once(init, flags_); +} + +//------------------------------------------------------------------------------ +void TWinsockSingleton::init(void) +{ + instance_ptr_.reset(new TWinsockSingleton); +} + +}}} // apache::thrift::transport diff --git a/lib/cpp/src/windows/TWinsockSingleton.h b/lib/cpp/src/windows/TWinsockSingleton.h new file mode 100644 index 00000000..134c7b0d --- /dev/null +++ b/lib/cpp/src/windows/TWinsockSingleton.h @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_TRANSPORT_WINDOWS_TWINSOCKSINGLETON_H_ +#define _THRIFT_TRANSPORT_WINDOWS_TWINSOCKSINGLETON_H_ 1 + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +// boost +#include +#include +#include + +namespace apache { namespace thrift { namespace transport { + +/** + * Winsock2 must be intialised once only in order to create sockets. This class + * performs a one time initialisation when create is called. + */ +class TWinsockSingleton : private boost::noncopyable +{ + +public: + + typedef boost::scoped_ptr instance_ptr; + +private: + + friend void boost::call_once(void (*func)(void), boost::once_flag& flag); + +private: + + TWinsockSingleton(void); + +public: + + ~TWinsockSingleton(void); + +public: + + static void create(void); + +private: + + static void init(void); + +private: + + static instance_ptr instance_ptr_; + static boost::once_flag flags_; +}; + +}}} // apache::thrift::transport + +#endif // _THRIFT_TRANSPORT_WINDOWS_TWINSOCKSINGLETON_H_ diff --git a/lib/cpp/src/windows/TargetVersion.h b/lib/cpp/src/windows/TargetVersion.h new file mode 100644 index 00000000..73746660 --- /dev/null +++ b/lib/cpp/src/windows/TargetVersion.h @@ -0,0 +1,33 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THIRFT_WINDOWS_TARGETVERSION_H_ +#define _THIRFT_WINDOWS_TARGETVERSION_H_ + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +#include + +#endif //_THIRFT_WINDOWS_TARGETVERSION_H_ diff --git a/lib/cpp/src/windows/config.h b/lib/cpp/src/windows/config.h new file mode 100644 index 00000000..29a01f69 --- /dev/null +++ b/lib/cpp/src/windows/config.h @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_CONFIG_H_ +#define _THRIFT_WINDOWS_CONFIG_H_ 1 + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +#include "TargetVersion.h" +#include "GetTimeOfDay.h" +#include "Operators.h" +#include "TWinsockSingleton.h" +#include "Fcntl.h" +#include "SocketPair.h" + +// boost +#include + +typedef boost::int64_t int64_t; +typedef boost::uint32_t uint32_t; +typedef boost::uint8_t uint8_t; + +// windows +#include +#include +#pragma comment(lib, "Ws2_32.lib") + +// pthreads +#include + +//"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 + +// 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 + +inline void close(SOCKET socket) +{ + ::closesocket(socket); +} + +#endif // _THRIFT_WINDOWS_CONFIG_H_ diff --git a/lib/cpp/src/windows/force_inc.h b/lib/cpp/src/windows/force_inc.h new file mode 100644 index 00000000..aa81c42b --- /dev/null +++ b/lib/cpp/src/windows/force_inc.h @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef _THRIFT_WINDOWS_FORCEINC_H_ +#define _THRIFT_WINDOWS_FORCEINC_H_ + +#if defined(_MSC_VER) && (_MSC_VER > 1200) +#pragma once +#endif // _MSC_VER + +#ifndef _WIN32 +#error This is a MSVC header only. +#endif + +#undef gai_strerror +#define gai_strerror gai_strerrorA + +#undef errno +#undef EINTR +#undef EINPROGRESS +#undef ECONNRESET +#undef ENOTCONN +#undef ETIMEDOUT +#undef EWOULDBLOCK +#undef EAGAIN +#undef EPIPE +#define errno ::WSAGetLastError() +#define EINPROGRESS WSAEINPROGRESS +#define EAGAIN WSAEWOULDBLOCK +#define EINTR WSAEINTR +#define ECONNRESET WSAECONNRESET +#define ENOTCONN WSAENOTCONN +#define ETIMEDOUT WSAETIMEDOUT +#define EWOULDBLOCK WSAEWOULDBLOCK +#define EPIPE WSAECONNRESET + +#endif // _THRIFT_WINDOWS_FORCEINC_H_ diff --git a/lib/cpp/src/windows/tr1/functional b/lib/cpp/src/windows/tr1/functional new file mode 100644 index 00000000..ebee3845 --- /dev/null +++ b/lib/cpp/src/windows/tr1/functional @@ -0,0 +1,20 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include diff --git a/lib/cpp/thrift.sln b/lib/cpp/thrift.sln new file mode 100644 index 00000000..244499df --- /dev/null +++ b/lib/cpp/thrift.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual Studio 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libthriftnb", "libthriftnb.vcxproj", "{D8696CCE-7D46-4659-B432-91754A41DEB0}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libthrift", "libthrift.vcxproj", "{DD26F57E-60F2-4F37-A616-D219A9BF338F}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "doc", "doc", "{006984E0-7CC1-47E2-ACD2-A40FE4D38693}" + ProjectSection(SolutionItems) = preProject + README_WINDOWS = README_WINDOWS + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D8696CCE-7D46-4659-B432-91754A41DEB0}.Debug|Win32.ActiveCfg = Debug|Win32 + {D8696CCE-7D46-4659-B432-91754A41DEB0}.Debug|Win32.Build.0 = Debug|Win32 + {D8696CCE-7D46-4659-B432-91754A41DEB0}.Release|Win32.ActiveCfg = Release|Win32 + {D8696CCE-7D46-4659-B432-91754A41DEB0}.Release|Win32.Build.0 = Release|Win32 + {DD26F57E-60F2-4F37-A616-D219A9BF338F}.Debug|Win32.ActiveCfg = Debug|Win32 + {DD26F57E-60F2-4F37-A616-D219A9BF338F}.Debug|Win32.Build.0 = Debug|Win32 + {DD26F57E-60F2-4F37-A616-D219A9BF338F}.Release|Win32.ActiveCfg = Release|Win32 + {DD26F57E-60F2-4F37-A616-D219A9BF338F}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal -- 2.17.1