new(userspace/falco): grpc context and stream context

Co-authored-by: Lorenzo Fontana <lo@linux.com>
Signed-off-by: Leonardo Di Donato <leodidonato@gmail.com>
This commit is contained in:
Leonardo Di Donato 2019-09-03 10:26:56 +00:00 committed by Leo Di Donato
parent e394bcf119
commit 5d0266a09e
2 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,35 @@
/*
Copyright (C) 2016-2019 The Falco Authors
This file is part of falco.
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.
*/
#include "grpc_context.h"
context::context(grpc::ServerContext* ctx):
m_ctx(ctx)
{
// todo: set thread-specific prefix "[session id][request id]"
}
void context::get_metadata(std::string key, std::string& val)
{
const std::multimap<grpc::string_ref, grpc::string_ref>& client_metadata = m_ctx->client_metadata();
auto it = client_metadata.find(key);
if(it != client_metadata.end())
{
val.assign(it->second.data(), it->second.size());
}
}

View File

@ -0,0 +1,56 @@
/*
Copyright (C) 2016-2019 The Falco Authors
This file is part of falco.
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.
*/
#pragma once
#ifdef GRPC_INCLUDE_IS_GRPCPP
#include <grpcpp/grpcpp.h>
#else
#include <grpc++/grpc++.h>
#endif
class context
{
public:
context(grpc::ServerContext* ctx);
~context() = default;
void get_metadata(std::string key, std::string& val);
private:
grpc::ServerContext* m_ctx = nullptr;
};
class stream_context : public context
{
public:
stream_context(grpc::ServerContext* ctx): context(ctx) {}
~stream_context() = default;
private:
enum : char
{
STREAMING = 1,
SUCCESS,
ERROR
} m_status = STREAMING;
// Request-specific stream data
mutable void* m_stream = nullptr;
// Are there more responses to stream?
mutable bool m_has_more = false;
};