1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #ifndef _THRIFT_TRANSPORT_TPIPE_H_
21 #define _THRIFT_TRANSPORT_TPIPE_H_ 1
22 
23 #include <thrift/transport/TTransport.h>
24 #include <thrift/transport/TVirtualTransport.h>
25 #ifndef _WIN32
26 #include <thrift/transport/TSocket.h>
27 #endif
28 #ifdef _WIN32
29 #include <thrift/windows/Sync.h>
30 #endif
31 #include <boost/noncopyable.hpp>
32 #ifdef _WIN32
33 #include <thrift/windows/Sync.h>
34 #endif
35 
36 namespace apache {
37 namespace thrift {
38 namespace transport {
39 
40 /**
41  * Windows Pipes implementation of the TTransport interface.
42  * Don't destroy a TPipe at global scope, as that will cause a thread join
43  * during DLLMain.  That also means that client objects using TPipe shouldn't be at global
44  * scope.
45  */
46 #ifdef _WIN32
47 class TPipeImpl;
48 
49 class TPipe : public TVirtualTransport<TPipe> {
50 public:
51   // Constructs a new pipe object.
52   TPipe(std::shared_ptr<TConfiguration> config = nullptr);
53   // Named pipe constructors -
54   explicit TPipe(HANDLE Pipe, std::shared_ptr<TConfiguration> config = nullptr);       // HANDLE is a void*
55   explicit TPipe(TAutoHandle& Pipe, std::shared_ptr<TConfiguration> config = nullptr); // this ctor will clear out / move from Pipe
56   // need a const char * overload so string literals don't go to the HANDLE overload
57   explicit TPipe(const char* pipename, std::shared_ptr<TConfiguration> config = nullptr);
58   explicit TPipe(const std::string& pipename, std::shared_ptr<TConfiguration> config = nullptr);
59   // Anonymous pipe -
60   TPipe(HANDLE PipeRd, HANDLE PipeWrt, std::shared_ptr<TConfiguration> config = nullptr);
61 
62   // Destroys the pipe object, closing it if necessary.
63   virtual ~TPipe();
64 
65   // Returns whether the pipe is open & valid.
66   bool isOpen() const override;
67 
68   // Checks whether more data is available in the pipe.
69   bool peek() override;
70 
71   // Creates and opens the named/anonymous pipe.
72   void open() override;
73 
74   // Shuts down communications on the pipe.
75   void close() override;
76 
77   // Reads from the pipe.
78   virtual uint32_t read(uint8_t* buf, uint32_t len);
79 
80   // Writes to the pipe.
81   virtual void write(const uint8_t* buf, uint32_t len);
82 
83   // Accessors
84   std::string getPipename();
85   void setPipename(const std::string& pipename);
86   HANDLE getPipeHandle(); // doubles as the read handle for anon pipe
87   void setPipeHandle(HANDLE pipehandle);
88   HANDLE getWrtPipeHandle();
89   void setWrtPipeHandle(HANDLE pipehandle);
90   long getConnTimeout();
91   void setConnTimeout(long seconds);
92 
93   // this function is intended to be used in generic / template situations,
94   // so its name needs to be the same as TPipeServer's
95   HANDLE getNativeWaitHandle();
96 
97 private:
98   std::shared_ptr<TPipeImpl> impl_;
99 
100   std::string pipename_;
101 
102   long TimeoutSeconds_;
103   bool isAnonymous_;
104 };
105 
106 #else
107 typedef TSocket TPipe;
108 #endif
109 }
110 }
111 } // apache::thrift::transport
112 
113 #endif // #ifndef _THRIFT_TRANSPORT_TPIPE_H_
114