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_TNONBLOCKINGSERVERTRANSPORT_H_
21 #define _THRIFT_TRANSPORT_TNONBLOCKINGSERVERTRANSPORT_H_ 1
22 
23 #include <thrift/transport/TSocket.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 TNonblockingServerTransport {
37 public:
38   virtual ~TNonblockingServerTransport() = default;
39 
40   /**
41    * Starts the server transport listening for new connections. Prior to this
42    * call most transports will not return anything when accept is called.
43    *
44    * @throws TTransportException if we were unable to listen
45    */
listen()46   virtual void listen() {}
47 
48   /**
49    * Gets a new dynamically allocated transport object and passes it to the
50    * caller. Note that it is the explicit duty of the caller to free the
51    * allocated object. The returned TTransport object must always be in the
52    * opened state. nullptr should never be returned, instead an Exception should
53    * always be thrown.
54    *
55    * @return A new TTransport object
56    * @throws TTransportException if there is an error
57    */
accept()58   std::shared_ptr<TSocket> accept() {
59     std::shared_ptr<TSocket> result = acceptImpl();
60     if (!result) {
61       throw TTransportException("accept() may not return nullptr");
62     }
63     return result;
64   }
65 
66   /**
67   * Utility method
68   *
69   * @return server socket file descriptor
70   * @throw TTransportException If an error occurs
71   */
72 
73   virtual THRIFT_SOCKET getSocketFD() = 0;
74 
75   virtual int getPort() = 0;
76 
77   virtual int getListenPort() = 0;
78 
79   /**
80    * Closes this transport such that future calls to accept will do nothing.
81    */
82   virtual void close() = 0;
83 
84 protected:
85   TNonblockingServerTransport() = default;
86 
87   /**
88    * Subclasses should implement this function for accept.
89    *
90    * @return A newly allocated TTransport object
91    * @throw TTransportException If an error occurs
92    */
93   virtual std::shared_ptr<TSocket> acceptImpl() = 0;
94 
95 };
96 }
97 }
98 } // apache::thrift::transport
99 
100 #endif // #ifndef _THRIFT_TRANSPORT_TNONBLOCKINGSERVERTRANSPORT_H_
101