THRIFT-1558 Named Pipe and Anonymous Pipe transport for Windows
Patch: Peace
add pipe crossplatform example to contrib

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1351477 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/contrib/transport-sample/server/ReadMe.txt b/contrib/transport-sample/server/ReadMe.txt
new file mode 100644
index 0000000..53c0ee5
--- /dev/null
+++ b/contrib/transport-sample/server/ReadMe.txt
@@ -0,0 +1,40 @@
+========================================================================
+    CONSOLE APPLICATION : server Project Overview
+========================================================================
+
+AppWizard has created this server application for you.
+
+This file contains a summary of what you will find in each of the files that
+make up your server application.
+
+
+server.vcxproj
+    This is the main project file for VC++ projects generated using an Application Wizard.
+    It contains information about the version of Visual C++ that generated the file, and
+    information about the platforms, configurations, and project features selected with the
+    Application Wizard.
+
+server.vcxproj.filters
+    This is the filters file for VC++ projects generated using an Application Wizard. 
+    It contains information about the association between the files in your project 
+    and the filters. This association is used in the IDE to show grouping of files with
+    similar extensions under a specific node (for e.g. ".cpp" files are associated with the
+    "Source Files" filter).
+
+server.cpp
+    This is the main application source file.
+
+/////////////////////////////////////////////////////////////////////////////
+Other standard files:
+
+StdAfx.h, StdAfx.cpp
+    These files are used to build a precompiled header (PCH) file
+    named server.pch and a precompiled types file named StdAfx.obj.
+
+/////////////////////////////////////////////////////////////////////////////
+Other notes:
+
+AppWizard uses "TODO:" comments to indicate parts of the source code you
+should add to or customize.
+
+/////////////////////////////////////////////////////////////////////////////
diff --git a/contrib/transport-sample/server/server.cpp b/contrib/transport-sample/server/server.cpp
new file mode 100644
index 0000000..dba8368
--- /dev/null
+++ b/contrib/transport-sample/server/server.cpp
@@ -0,0 +1,168 @@
+// server.cpp : Defines the entry point for the console application.
+//
+// sample server command line app using Thrift IPC.
+//
+// This is a simple demonstration of full duplex RPC. That is, each
+// side runs both a client and server to enable bidirectional event 
+// signaling.
+//
+
+#ifdef _WIN32
+#  include "stdafx.h"
+#else
+#  include "config.h"
+#endif
+
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//Include this before the generated includes
+#include "ThriftCommon.h"
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//Tailor these to your generated files
+#include "../gen-cpp/SampleService.h"
+#include "../gen-cpp/SampleCallback.h"
+
+using namespace Sample; //declared in .thrift file
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+int16_t ClientPort_;
+std::string ClientPipeName_;
+void S2CThreadProc();
+
+//-----------------------------------------------------------------------------
+// RPC implementations
+//
+class SampleServiceHandler : virtual public SampleServiceIf {
+ public:
+  SampleServiceHandler() {
+    // Your initialization goes here
+  }
+
+  void HelloThere(std::string& _return, const std::string& HelloString) {
+    // Your implementation goes here
+    printf("<<<HelloThere() received string: %s\n", HelloString.c_str());
+	_return = "Good thank you.";
+  }
+
+  void ServerDoSomething() {
+    // Your implementation goes here
+    printf("ServerDoSomething(): Simulating work for 5 seconds\n");
+    Sleep(5000);
+    printf("ServerDoSomething(): Done\n");
+  }
+
+  void ClientSideListenPort(const int16_t ClientListenPort)
+  {
+	ClientPort_ = ClientListenPort;
+	ClientPipeName_ = "";
+#ifdef _WIN32
+	printf(">>>Connecting to client on port %d\n", ClientPort_);
+	boost::thread Connect2ClientThread(S2CThreadProc);
+#endif
+  }
+
+  void ClientSidePipeName(const std::string& ClientPipeName)
+  {
+	ClientPipeName_ = ClientPipeName;
+	ClientPort_ = 0;
+#ifdef _WIN32
+	printf(">>>Connecting to client pipe %s\n", ClientPipeName_.c_str());
+	boost::thread Connect2ClientThread(S2CThreadProc);
+#endif
+  }
+};
+//-----------------------------------------------------------------------------
+
+#ifdef _WIN32
+int _tmain(int argc, _TCHAR* argv[])
+#else
+int main(int argc, char **argv)
+#endif
+{
+	int port;
+	std::string pipename; //e.g. "affpipe"
+
+	bool usage = false;
+
+	//Process command line params
+	if(argc > 1)
+	{
+		if(_tcscmp(argv[1], TEXT("-sp")) == 0)
+		{	//Socket Port specified
+			port = _tstoi(argv[2]);
+#ifdef _WIN32
+			TWinsockSingleton::create();
+#endif
+			// Start the thrift server which is a blocking call.
+			thriftcommon::RunThriftServer<SampleServiceHandler, SampleServiceProcessor>(10, port);
+		}
+		else if(_tcscmp(argv[1], TEXT("-np")) == 0)
+		{	//Named Pipe specified
+#ifdef _WIN32
+			std::wstring wpipe(argv[2]);
+			pipename.resize(wpipe.length());
+			std::copy(wpipe.begin(), wpipe.end(), pipename.begin());
+#else
+			pipename = argv[2];
+#endif
+			printf("Using Named Pipe %s\n", pipename.c_str());
+
+			//Thrift over Named Pipe.
+			thriftcommon::RunThriftServer<SampleServiceHandler, SampleServiceProcessor>(10, pipename);
+		}
+		else if(_tcscmp(argv[1], TEXT("-ap")) == 0)
+		{	//Anonymous Pipe specified
+			//This is more involved because the child needs to be launched 
+			//after the transport is created but before the blocking server 
+			//call.
+#ifdef _WIN32
+			boost::shared_ptr<TServerTransport> transport(new TPipeServer()); //Anonymous pipe
+			thriftcommon::LaunchAnonPipeChild(".\\client.exe", transport);
+			boost::shared_ptr<SampleServiceHandler> handler(new SampleServiceHandler());
+			thriftcommon::RunThriftServer<SampleServiceHandler, SampleServiceProcessor>(handler, 10, transport);
+#else
+			printf("Anonymous pipes not (yet) supported under *NIX\n");
+#endif
+		}
+		else
+			usage = true;
+	}
+	else
+		usage = true;
+
+	if(usage)
+	{
+		printf("Thrift sample server usage:\n\n");
+		printf("Socket Port :   -sp <port#>\n");
+		printf("Named Pipe :    -np <pipename> (e.g. affpipe)\n");
+		printf("Anonymous Pipe: -ap\n");
+	}
+	return 0;
+}
+
+
+//Thread Routine that connects to the 'client'.
+void S2CThreadProc()
+{
+	//Master server's connection to client-side's server.
+	boost::shared_ptr<SampleCallbackClient> clientsrv; //Client class from Thrift-generated code.
+	boost::shared_ptr<TTransport> transport;
+	if(ClientPort_ != 0)
+		thriftcommon::ConnectToServer<SampleCallbackClient, TTransport>(clientsrv, transport, ClientPort_);
+	if(!ClientPipeName_.empty())
+		thriftcommon::ConnectToServer<SampleCallbackClient, TTransport>(clientsrv, transport, ClientPipeName_);
+
+	try {
+		transport->open();
+
+		clientsrv->pingclient();
+		Sleep(1500);
+		clientsrv->pingclient();
+		Sleep(1500);
+		clientsrv->pingclient();
+
+		transport->close();
+	} catch (TException &tx) {
+		printf("ERROR: %s\n", tx.what());
+	}
+}
+
diff --git a/contrib/transport-sample/server/server.vcxproj b/contrib/transport-sample/server/server.vcxproj
new file mode 100644
index 0000000..8e39b26
--- /dev/null
+++ b/contrib/transport-sample/server/server.vcxproj
@@ -0,0 +1,106 @@
+?<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{72FCAF29-506D-4164-9FA6-F54C5C28E79D}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>server</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>Application</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>Unicode</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <LinkIncremental>true</LinkIncremental>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <LinkIncremental>false</LinkIncremental>
+  </PropertyGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <PrecompiledHeader>NotUsing</PrecompiledHeader>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>../;../../../lib/cpp/src;../../../../Boost/;../../../../Boost/boost/tr1;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalLibraryDirectories>../../../lib/cpp/$(Configuration);../../../../Boost/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+      <AdditionalDependencies>libthrift.lib;ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>NotUsing</PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+      <AdditionalIncludeDirectories>../;../../../lib/cpp/src;../../../../Boost/;../../../../Boost/boost/tr1;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
+    </ClCompile>
+    <Link>
+      <SubSystem>Console</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalLibraryDirectories>../../../lib/cpp/$(Configuration);../../../../Boost/lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
+      <AdditionalDependencies>libthrift.lib;ws2_32.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemGroup>
+    <None Include="ReadMe.txt" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="..\gen-cpp\SampleCallback.h" />
+    <ClInclude Include="..\gen-cpp\SampleService.h" />
+    <ClInclude Include="..\gen-cpp\Sample_constants.h" />
+    <ClInclude Include="..\gen-cpp\Sample_types.h" />
+    <ClInclude Include="stdafx.h" />
+    <ClInclude Include="targetver.h" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="..\ThriftCommon.cpp" />
+    <ClCompile Include="..\gen-cpp\SampleCallback.cpp" />
+    <ClCompile Include="..\gen-cpp\SampleService.cpp" />
+    <ClCompile Include="..\gen-cpp\Sample_constants.cpp" />
+    <ClCompile Include="..\gen-cpp\Sample_types.cpp" />
+    <ClCompile Include="server.cpp" />
+    <ClCompile Include="stdafx.cpp">
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
+      <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
+    </ClCompile>
+  </ItemGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>
\ No newline at end of file
diff --git a/contrib/transport-sample/server/server.vcxproj.filters b/contrib/transport-sample/server/server.vcxproj.filters
new file mode 100644
index 0000000..8bb6dfb
--- /dev/null
+++ b/contrib/transport-sample/server/server.vcxproj.filters
@@ -0,0 +1,66 @@
+?<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup>
+    <Filter Include="Source Files">
+      <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
+      <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
+    </Filter>
+    <Filter Include="Header Files">
+      <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
+      <Extensions>h;hpp;hxx;hm;inl;inc;xsd</Extensions>
+    </Filter>
+    <Filter Include="Resource Files">
+      <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
+      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
+    </Filter>
+    <Filter Include="Source Files\gen-cpp">
+      <UniqueIdentifier>{dab66db8-bc45-4518-aad2-7a75696226e3}</UniqueIdentifier>
+    </Filter>
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="ReadMe.txt" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="stdafx.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="targetver.h">
+      <Filter>Header Files</Filter>
+    </ClInclude>
+    <ClInclude Include="..\gen-cpp\Sample_types.h">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClInclude>
+    <ClInclude Include="..\gen-cpp\SampleService.h">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClInclude>
+    <ClInclude Include="..\gen-cpp\Sample_constants.h">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClInclude>
+    <ClInclude Include="..\gen-cpp\SampleCallback.h">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClInclude>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="stdafx.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="server.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+    <ClCompile Include="..\gen-cpp\Sample_types.cpp">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClCompile>
+    <ClCompile Include="..\gen-cpp\SampleService.cpp">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClCompile>
+    <ClCompile Include="..\gen-cpp\Sample_constants.cpp">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClCompile>
+    <ClCompile Include="..\gen-cpp\SampleCallback.cpp">
+      <Filter>Source Files\gen-cpp</Filter>
+    </ClCompile>
+    <ClCompile Include="..\ThriftCommon.cpp">
+      <Filter>Source Files</Filter>
+    </ClCompile>
+  </ItemGroup>
+</Project>
diff --git a/contrib/transport-sample/server/stdafx.cpp b/contrib/transport-sample/server/stdafx.cpp
new file mode 100644
index 0000000..c25ff61
--- /dev/null
+++ b/contrib/transport-sample/server/stdafx.cpp
@@ -0,0 +1,8 @@
+// stdafx.cpp : source file that includes just the standard includes
+// server.pch will be the pre-compiled header
+// stdafx.obj will contain the pre-compiled type information
+
+#include "stdafx.h"
+
+// TODO: reference any additional headers you need in STDAFX.H
+// and not in this file
diff --git a/contrib/transport-sample/server/stdafx.h b/contrib/transport-sample/server/stdafx.h
new file mode 100644
index 0000000..b005a83
--- /dev/null
+++ b/contrib/transport-sample/server/stdafx.h
@@ -0,0 +1,15 @@
+// stdafx.h : include file for standard system include files,
+// or project specific include files that are used frequently, but
+// are changed infrequently
+//
+
+#pragma once
+
+#include "targetver.h"
+
+#include <stdio.h>
+#include <tchar.h>
+
+
+
+// TODO: reference additional headers your program requires here
diff --git a/contrib/transport-sample/server/targetver.h b/contrib/transport-sample/server/targetver.h
new file mode 100644
index 0000000..87c0086
--- /dev/null
+++ b/contrib/transport-sample/server/targetver.h
@@ -0,0 +1,8 @@
+#pragma once
+
+// Including SDKDDKVer.h defines the highest available Windows platform.
+
+// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and
+// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h.
+
+#include <SDKDDKVer.h>