TConnectedClient.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef _THRIFT_SERVER_TCONNECTEDCLIENT_H_
  20. #define _THRIFT_SERVER_TCONNECTEDCLIENT_H_ 1
  21. #include <thrift/stdcxx.h>
  22. #include <thrift/TProcessor.h>
  23. #include <thrift/protocol/TProtocol.h>
  24. #include <thrift/server/TServer.h>
  25. #include <thrift/transport/TTransport.h>
  26. namespace apache {
  27. namespace thrift {
  28. namespace server {
  29. /**
  30. * This represents a client connected to a TServer. The
  31. * processing loop for a client must provide some required
  32. * functionality common to all implementations so it is
  33. * encapsulated here.
  34. */
  35. class TConnectedClient : public apache::thrift::concurrency::Runnable {
  36. public:
  37. /**
  38. * Constructor.
  39. *
  40. * @param[in] processor the TProcessor
  41. * @param[in] inputProtocol the input TProtocol
  42. * @param[in] outputProtocol the output TProtocol
  43. * @param[in] eventHandler the server event handler
  44. * @param[in] client the TTransport representing the client
  45. */
  46. TConnectedClient(
  47. const stdcxx::shared_ptr<apache::thrift::TProcessor>& processor,
  48. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocol>& inputProtocol,
  49. const stdcxx::shared_ptr<apache::thrift::protocol::TProtocol>& outputProtocol,
  50. const stdcxx::shared_ptr<apache::thrift::server::TServerEventHandler>& eventHandler,
  51. const stdcxx::shared_ptr<apache::thrift::transport::TTransport>& client);
  52. /**
  53. * Destructor.
  54. */
  55. virtual ~TConnectedClient();
  56. /**
  57. * Drive the client until it is done.
  58. * The client processing loop is:
  59. *
  60. * [optional] call eventHandler->createContext once
  61. * [optional] call eventHandler->processContext per request
  62. * call processor->process per request
  63. * handle expected transport exceptions:
  64. * END_OF_FILE means the client is gone
  65. * INTERRUPTED means the client was interrupted
  66. * by TServerTransport::interruptChildren()
  67. * handle unexpected transport exceptions by logging
  68. * handle standard exceptions by logging
  69. * handle unexpected exceptions by logging
  70. * cleanup()
  71. */
  72. virtual void run() /* override */;
  73. protected:
  74. /**
  75. * Cleanup after a client. This happens if the client disconnects,
  76. * or if the server is stopped, or if an exception occurs.
  77. *
  78. * The cleanup processing is:
  79. * [optional] call eventHandler->deleteContext once
  80. * close the inputProtocol's TTransport
  81. * close the outputProtocol's TTransport
  82. * close the client
  83. */
  84. virtual void cleanup();
  85. private:
  86. stdcxx::shared_ptr<apache::thrift::TProcessor> processor_;
  87. stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> inputProtocol_;
  88. stdcxx::shared_ptr<apache::thrift::protocol::TProtocol> outputProtocol_;
  89. stdcxx::shared_ptr<apache::thrift::server::TServerEventHandler> eventHandler_;
  90. stdcxx::shared_ptr<apache::thrift::transport::TTransport> client_;
  91. /**
  92. * Context acquired from the eventHandler_ if one exists.
  93. */
  94. void* opaqueContext_;
  95. };
  96. }
  97. }
  98. }
  99. #endif // #ifndef _THRIFT_SERVER_TCONNECTEDCLIENT_H_