2017-11-25 06:41:58 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2017 The Cartographer Authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef CARTOGRAPHER_GRPC_FRAMEWORK_RPC_H
|
|
|
|
#define CARTOGRAPHER_GRPC_FRAMEWORK_RPC_H
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <unordered_set>
|
|
|
|
|
|
|
|
#include "cartographer/common/mutex.h"
|
2017-11-29 21:05:31 +08:00
|
|
|
#include "cartographer_grpc/framework/execution_context.h"
|
2017-11-29 17:40:26 +08:00
|
|
|
#include "cartographer_grpc/framework/rpc_handler_interface.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "google/protobuf/message.h"
|
2017-11-25 06:41:58 +08:00
|
|
|
#include "grpc++/grpc++.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "grpc++/impl/codegen/async_stream.h"
|
2017-11-29 17:40:26 +08:00
|
|
|
#include "grpc++/impl/codegen/async_unary_call.h"
|
2017-11-28 17:50:30 +08:00
|
|
|
#include "grpc++/impl/codegen/proto_utils.h"
|
|
|
|
#include "grpc++/impl/codegen/service_type.h"
|
2017-11-25 06:41:58 +08:00
|
|
|
|
|
|
|
namespace cartographer_grpc {
|
|
|
|
namespace framework {
|
|
|
|
|
|
|
|
class Service;
|
|
|
|
class Rpc {
|
|
|
|
public:
|
2017-11-30 20:18:16 +08:00
|
|
|
enum class Event { NEW_CONNECTION = 0, READ, WRITE, FINISH, DONE };
|
2017-11-29 17:40:26 +08:00
|
|
|
struct RpcEvent {
|
|
|
|
const Event event;
|
2017-11-25 06:41:58 +08:00
|
|
|
Rpc* rpc;
|
2017-11-29 17:40:26 +08:00
|
|
|
// Indicates whether the event is pending completion. E.g. 'event = READ'
|
|
|
|
// and 'pending = true' means that a read has been requested but hasn't
|
|
|
|
// completed yet. While 'pending = false' indicates, that the read has
|
|
|
|
// completed and currently no read is in-flight.
|
|
|
|
bool pending;
|
2017-11-25 06:41:58 +08:00
|
|
|
};
|
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
Rpc(int method_index, ::grpc::ServerCompletionQueue* server_completion_queue,
|
2017-11-29 21:05:31 +08:00
|
|
|
ExecutionContext* execution_context,
|
2017-11-28 17:50:30 +08:00
|
|
|
const RpcHandlerInfo& rpc_handler_info, Service* service);
|
2017-11-29 17:40:26 +08:00
|
|
|
std::unique_ptr<Rpc> Clone();
|
|
|
|
void OnRequest();
|
|
|
|
void OnReadsDone();
|
|
|
|
void RequestNextMethodInvocation();
|
|
|
|
void RequestStreamingReadIfNeeded();
|
|
|
|
void Write(std::unique_ptr<::google::protobuf::Message> message);
|
|
|
|
Service* service() { return service_; }
|
|
|
|
RpcEvent* GetRpcEvent(Event event);
|
2017-11-25 06:41:58 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
Rpc(const Rpc&) = delete;
|
|
|
|
Rpc& operator=(const Rpc&) = delete;
|
2017-11-29 17:40:26 +08:00
|
|
|
void InitializeReadersAndWriters(
|
|
|
|
::grpc::internal::RpcMethod::RpcType rpc_type);
|
|
|
|
|
|
|
|
::grpc::internal::AsyncReaderInterface<::google::protobuf::Message>*
|
|
|
|
async_reader_interface();
|
|
|
|
::grpc::internal::ServerAsyncStreamingInterface* streaming_interface();
|
2017-11-25 06:41:58 +08:00
|
|
|
|
2017-11-28 17:50:30 +08:00
|
|
|
int method_index_;
|
|
|
|
::grpc::ServerCompletionQueue* server_completion_queue_;
|
2017-11-29 21:05:31 +08:00
|
|
|
ExecutionContext* execution_context_;
|
2017-11-25 06:41:58 +08:00
|
|
|
RpcHandlerInfo rpc_handler_info_;
|
2017-11-29 17:40:26 +08:00
|
|
|
Service* service_;
|
2017-11-25 06:41:58 +08:00
|
|
|
::grpc::ServerContext server_context_;
|
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
RpcEvent new_connection_event_;
|
|
|
|
RpcEvent read_event_;
|
|
|
|
RpcEvent write_event_;
|
2017-11-30 20:18:16 +08:00
|
|
|
RpcEvent finish_event_;
|
2017-11-29 17:40:26 +08:00
|
|
|
RpcEvent done_event_;
|
2017-11-25 06:41:58 +08:00
|
|
|
|
|
|
|
std::unique_ptr<google::protobuf::Message> request_;
|
|
|
|
std::unique_ptr<google::protobuf::Message> response_;
|
2017-11-28 17:50:30 +08:00
|
|
|
|
2017-11-29 17:40:26 +08:00
|
|
|
std::unique_ptr<RpcHandlerInterface> handler_;
|
|
|
|
|
2017-11-30 20:18:16 +08:00
|
|
|
std::unique_ptr<::grpc::ServerAsyncResponseWriter<google::protobuf::Message>>
|
|
|
|
server_async_response_writer_;
|
2017-11-28 17:50:30 +08:00
|
|
|
std::unique_ptr<::grpc::ServerAsyncReader<google::protobuf::Message,
|
|
|
|
google::protobuf::Message>>
|
|
|
|
server_async_reader_;
|
2017-11-25 06:41:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// This class keeps track of all in-flight RPCs for a 'Service'. Make sure that
|
|
|
|
// all RPCs have been terminated and removed from this object before it goes out
|
|
|
|
// of scope.
|
|
|
|
class ActiveRpcs {
|
|
|
|
public:
|
|
|
|
ActiveRpcs();
|
|
|
|
~ActiveRpcs() EXCLUDES(lock_);
|
|
|
|
|
|
|
|
Rpc* Add(std::unique_ptr<Rpc> rpc) EXCLUDES(lock_);
|
|
|
|
bool Remove(Rpc* rpc) EXCLUDES(lock_);
|
|
|
|
|
|
|
|
private:
|
|
|
|
cartographer::common::Mutex lock_;
|
|
|
|
std::unordered_set<Rpc*> rpcs_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace framework
|
|
|
|
} // namespace cartographer_grpc
|
|
|
|
|
|
|
|
#endif // CARTOGRAPHER_GRPC_FRAMEWORK_RPC_H
|