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_TSERVERTRANSPORT_H_
21 #define _THRIFT_TRANSPORT_TSERVERTRANSPORT_H_ 1
22 
23 #include <thrift/transport/TTransport.h>
24 #include <thrift/transport/TTransportException.h>
25 
26 namespace apache {
27 namespace thrift {
28 namespace transport {
29 
30 /**
31  * Server transport framework. A server needs to have some facility for
32  * creating base transports to read/write from.  The server is expected
33  * to keep track of TTransport children that it creates for purposes of
34  * controlling their lifetime.
35  */
36 class TServerTransport {
37 public:
38   virtual ~TServerTransport() = default;
39 
40   /**
41    * Whether this transport is open.
42    */
isOpen()43   virtual bool isOpen() const { return false; }
44 
45   /**
46    * Starts the server transport listening for new connections. Prior to this
47    * call most transports will not return anything when accept is called.
48    *
49    * @throws TTransportException if we were unable to listen
50    */
listen()51   virtual void listen() {}
52 
53   /**
54    * Gets a new dynamically allocated transport object and passes it to the
55    * caller. Note that it is the explicit duty of the caller to free the
56    * allocated object. The returned TTransport object must always be in the
57    * opened state. nullptr should never be returned, instead an Exception should
58    * always be thrown.
59    *
60    * @return A new TTransport object
61    * @throws TTransportException if there is an error
62    */
accept()63   std::shared_ptr<TTransport> accept() {
64     std::shared_ptr<TTransport> result = acceptImpl();
65     if (!result) {
66       throw TTransportException("accept() may not return nullptr");
67     }
68     return result;
69   }
70 
71   /**
72    * For "smart" TServerTransport implementations that work in a multi
73    * threaded context this can be used to break out of an accept() call.
74    * It is expected that the transport will throw a TTransportException
75    * with the INTERRUPTED error code.
76    *
77    * This will not make an attempt to interrupt any TTransport children.
78    */
interrupt()79   virtual void interrupt() {}
80 
81   /**
82    * This will interrupt the children created by the server transport.
83    * allowing them to break out of any blocking data reception call.
84    * It is expected that the children will throw a TTransportException
85    * with the INTERRUPTED error code.
86    */
interruptChildren()87   virtual void interruptChildren() {}
88 
89   /**
90   * Utility method
91   *
92   * @return server socket file descriptor
93   * @throw TTransportException If an error occurs
94   */
95 
getSocketFD()96   virtual THRIFT_SOCKET getSocketFD() { return -1; }
97 
98   /**
99    * Closes this transport such that future calls to accept will do nothing.
100    */
101   virtual void close() = 0;
102 
103 protected:
104   TServerTransport() = default;
105 
106   /**
107    * Subclasses should implement this function for accept.
108    *
109    * @return A newly allocated TTransport object
110    * @throw TTransportException If an error occurs
111    */
112   virtual std::shared_ptr<TTransport> acceptImpl() = 0;
113 };
114 }
115 }
116 } // apache::thrift::transport
117 
118 #endif // #ifndef _THRIFT_TRANSPORT_TSERVERTRANSPORT_H_
119