first commit

This commit is contained in:
gxt_kt 2024-12-09 19:29:55 +08:00
commit 1c2342574c
12 changed files with 6175 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
*build*
bin/
cmake-build*
.cache
.idea
.vscode
.cache
*__pycache__*

56
CMakeLists.txt Normal file
View File

@ -0,0 +1,56 @@
cmake_minimum_required(VERSION 3.0)
project(grpc)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(CMAKE_BUILD_TYPE Release) # Debug Release RelWithDebInfo
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fno-elide-constructors")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-elide-constructors")
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC")
if (${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
message("Building for Linux platform")
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
message("Building for macOS platform")
set(CMAKE_C_COMPILER "/opt/homebrew/bin/aarch64-apple-darwin23-gcc-14") # gcc clang` c
set(CMAKE_CXX_COMPILER "/opt/homebrew/bin/aarch64-apple-darwin23-g++-14") # g++ clang++` c++
elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
message("Building for Windows platform")
else()
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
endif()
find_package(debugstream)
include_directories(include)
find_package(Protobuf REQUIRED CONFIG)
find_package(gRPC CONFIG REQUIRED)
include_directories(protoc)
set(LIB
gRPC::grpc++
gRPC::grpc++_reflection
protobuf::libprotobuf
)
# add_executable(grpc src/main.cc)
# target_link_libraries(libhv ${LIB})
file(GLOB_RECURSE PROTOC_SRCS "protoc/*.cc")
add_executable(grpc_server src/grpc_server_main.cc
${PROTOC_SRCS}
)
add_executable(grpc_client src/grpc_client_main.cc
${PROTOC_SRCS}
)
target_link_libraries(grpc_server ${LIB} debugstream)
target_link_libraries(grpc_client ${LIB} debugstream)

2
include/common.h Normal file
View File

@ -0,0 +1,2 @@
#pragma once
#include <debugstream/debugstream.h>

26
include/protoc.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <grpc/grpc.h>
#include <grpcpp/security/server_credentials.h>
#include <grpcpp/server.h>
#include <grpcpp/server_builder.h>
#include <grpcpp/server_context.h>
#include "market_data.grpc.pb.h"
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::ServerReader;
using grpc::ServerReaderWriter;
using grpc::ServerWriter;
using grpc::Status;
inline long GetTimeUs() {
// 获取当前时间点
auto now = std::chrono::high_resolution_clock::now();
// 将时间点转换为微秒
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
now.time_since_epoch());
return duration.count();
}

123
market_data.proto Normal file
View File

@ -0,0 +1,123 @@
syntax = "proto3";
option java_multiple_files = true;
option java_package = "io.grpc.example.market_data";
option java_outer_classname = "market_data";
option objc_class_prefix = "HLW";
package market_data;
service TestService {
rpc GetData1(CThostFtdcDepthMarketDataField) returns(MsgReply) {}
rpc GetData2(ResMsg) returns(MsgReply) {}
}
message CThostFtdcDepthMarketDataField {
//
string TradingDay = 1;
//
string reserve1 = 2;
//
string ExchangeID = 3;
//
string reserve2 = 4;
//
double LastPrice = 5;
//
double PreSettlementPrice = 6;
//
double PreClosePrice = 7;
//
double PreOpenInterest = 8;
//
double OpenPrice = 9;
//
double HighestPrice = 10;
//
double LowestPrice = 11;
//
int32 Volume = 12;
//
double Turnover = 13;
//
double OpenInterest = 14;
//
double ClosePrice = 15;
//
double SettlementPrice = 16;
//
double UpperLimitPrice = 17;
//
double LowerLimitPrice = 18;
//
double PreDelta = 19;
//
double CurrDelta = 20;
//
string UpdateTime = 21;
//
int32 UpdateMillisec = 22;
//
double BidPrice1 = 23;
//
int32 BidVolume1 = 24;
//
double AskPrice1 = 25;
//
int32 AskVolume1 = 26;
//
double BidPrice2 = 27;
//
int32 BidVolume2 = 28;
//
double AskPrice2 = 29;
//
int32 AskVolume2 = 30;
//
double BidPrice3 = 31;
//
int32 BidVolume3 = 32;
//
double AskPrice3 = 33;
//
int32 AskVolume3 = 34;
//
double BidPrice4 = 35;
//
int32 BidVolume4 = 36;
//
double AskPrice4 = 37;
//
int32 AskVolume4 = 38;
//
double BidPrice5 = 39;
//
int32 BidVolume5 = 40;
//
double AskPrice5 = 41;
//
int32 AskVolume5 = 42;
//
double AveragePrice = 43;
//
string ActionDay = 44;
//
string InstrumentID = 45;
//
string ExchangeInstID = 46;
}
message ResMsg {
string signame = 1;
string InstrumentID = 2;
int32 sigflag = 3;
double sigvalue = 4;
string offset = 5; // char string
string dir = 6; // char string
int32 vol = 7;
repeated double other_params = 8;
repeated string other_msg = 9;
}
message MsgReply { string message = 1; }

View File

@ -0,0 +1,128 @@
// Generated by the gRPC C++ plugin.
// If you make any local change, they will be lost.
// source: market_data.proto
#include "market_data.pb.h"
#include "market_data.grpc.pb.h"
#include <functional>
#include <grpcpp/support/async_stream.h>
#include <grpcpp/support/async_unary_call.h>
#include <grpcpp/impl/channel_interface.h>
#include <grpcpp/impl/client_unary_call.h>
#include <grpcpp/support/client_callback.h>
#include <grpcpp/support/message_allocator.h>
#include <grpcpp/support/method_handler.h>
#include <grpcpp/impl/rpc_service_method.h>
#include <grpcpp/support/server_callback.h>
#include <grpcpp/impl/server_callback_handlers.h>
#include <grpcpp/server_context.h>
#include <grpcpp/impl/service_type.h>
#include <grpcpp/support/sync_stream.h>
namespace market_data {
static const char* TestService_method_names[] = {
"/market_data.TestService/GetData1",
"/market_data.TestService/GetData2",
};
std::unique_ptr< TestService::Stub> TestService::NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options) {
(void)options;
std::unique_ptr< TestService::Stub> stub(new TestService::Stub(channel, options));
return stub;
}
TestService::Stub::Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options)
: channel_(channel), rpcmethod_GetData1_(TestService_method_names[0], options.suffix_for_stats(),::grpc::internal::RpcMethod::NORMAL_RPC, channel)
, rpcmethod_GetData2_(TestService_method_names[1], options.suffix_for_stats(),::grpc::internal::RpcMethod::NORMAL_RPC, channel)
{}
::grpc::Status TestService::Stub::GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::market_data::MsgReply* response) {
return ::grpc::internal::BlockingUnaryCall< ::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(channel_.get(), rpcmethod_GetData1_, context, request, response);
}
void TestService::Stub::async::GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response, std::function<void(::grpc::Status)> f) {
::grpc::internal::CallbackUnaryCall< ::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(stub_->channel_.get(), stub_->rpcmethod_GetData1_, context, request, response, std::move(f));
}
void TestService::Stub::async::GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response, ::grpc::ClientUnaryReactor* reactor) {
::grpc::internal::ClientCallbackUnaryFactory::Create< ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(stub_->channel_.get(), stub_->rpcmethod_GetData1_, context, request, response, reactor);
}
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* TestService::Stub::PrepareAsyncGetData1Raw(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) {
return ::grpc::internal::ClientAsyncResponseReaderHelper::Create< ::market_data::MsgReply, ::market_data::CThostFtdcDepthMarketDataField, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(channel_.get(), cq, rpcmethod_GetData1_, context, request);
}
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* TestService::Stub::AsyncGetData1Raw(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) {
auto* result =
this->PrepareAsyncGetData1Raw(context, request, cq);
result->StartCall();
return result;
}
::grpc::Status TestService::Stub::GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::market_data::MsgReply* response) {
return ::grpc::internal::BlockingUnaryCall< ::market_data::ResMsg, ::market_data::MsgReply, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(channel_.get(), rpcmethod_GetData2_, context, request, response);
}
void TestService::Stub::async::GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response, std::function<void(::grpc::Status)> f) {
::grpc::internal::CallbackUnaryCall< ::market_data::ResMsg, ::market_data::MsgReply, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(stub_->channel_.get(), stub_->rpcmethod_GetData2_, context, request, response, std::move(f));
}
void TestService::Stub::async::GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response, ::grpc::ClientUnaryReactor* reactor) {
::grpc::internal::ClientCallbackUnaryFactory::Create< ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(stub_->channel_.get(), stub_->rpcmethod_GetData2_, context, request, response, reactor);
}
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* TestService::Stub::PrepareAsyncGetData2Raw(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) {
return ::grpc::internal::ClientAsyncResponseReaderHelper::Create< ::market_data::MsgReply, ::market_data::ResMsg, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(channel_.get(), cq, rpcmethod_GetData2_, context, request);
}
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* TestService::Stub::AsyncGetData2Raw(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) {
auto* result =
this->PrepareAsyncGetData2Raw(context, request, cq);
result->StartCall();
return result;
}
TestService::Service::Service() {
AddMethod(new ::grpc::internal::RpcServiceMethod(
TestService_method_names[0],
::grpc::internal::RpcMethod::NORMAL_RPC,
new ::grpc::internal::RpcMethodHandler< TestService::Service, ::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(
[](TestService::Service* service,
::grpc::ServerContext* ctx,
const ::market_data::CThostFtdcDepthMarketDataField* req,
::market_data::MsgReply* resp) {
return service->GetData1(ctx, req, resp);
}, this)));
AddMethod(new ::grpc::internal::RpcServiceMethod(
TestService_method_names[1],
::grpc::internal::RpcMethod::NORMAL_RPC,
new ::grpc::internal::RpcMethodHandler< TestService::Service, ::market_data::ResMsg, ::market_data::MsgReply, ::grpc::protobuf::MessageLite, ::grpc::protobuf::MessageLite>(
[](TestService::Service* service,
::grpc::ServerContext* ctx,
const ::market_data::ResMsg* req,
::market_data::MsgReply* resp) {
return service->GetData2(ctx, req, resp);
}, this)));
}
TestService::Service::~Service() {
}
::grpc::Status TestService::Service::GetData1(::grpc::ServerContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response) {
(void) context;
(void) request;
(void) response;
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
::grpc::Status TestService::Service::GetData2(::grpc::ServerContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response) {
(void) context;
(void) request;
(void) response;
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
} // namespace market_data

View File

@ -0,0 +1,397 @@
// Generated by the gRPC C++ plugin.
// If you make any local change, they will be lost.
// source: market_data.proto
#ifndef GRPC_market_5fdata_2eproto__INCLUDED
#define GRPC_market_5fdata_2eproto__INCLUDED
#include "market_data.pb.h"
#include <functional>
#include <grpcpp/generic/async_generic_service.h>
#include <grpcpp/support/async_stream.h>
#include <grpcpp/support/async_unary_call.h>
#include <grpcpp/support/client_callback.h>
#include <grpcpp/client_context.h>
#include <grpcpp/completion_queue.h>
#include <grpcpp/support/message_allocator.h>
#include <grpcpp/support/method_handler.h>
#include <grpcpp/impl/proto_utils.h>
#include <grpcpp/impl/rpc_method.h>
#include <grpcpp/support/server_callback.h>
#include <grpcpp/impl/server_callback_handlers.h>
#include <grpcpp/server_context.h>
#include <grpcpp/impl/service_type.h>
#include <grpcpp/support/status.h>
#include <grpcpp/support/stub_options.h>
#include <grpcpp/support/sync_stream.h>
namespace market_data {
class TestService final {
public:
static constexpr char const* service_full_name() {
return "market_data.TestService";
}
class StubInterface {
public:
virtual ~StubInterface() {}
virtual ::grpc::Status GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::market_data::MsgReply* response) = 0;
std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>> AsyncGetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>>(AsyncGetData1Raw(context, request, cq));
}
std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>> PrepareAsyncGetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>>(PrepareAsyncGetData1Raw(context, request, cq));
}
virtual ::grpc::Status GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::market_data::MsgReply* response) = 0;
std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>> AsyncGetData2(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>>(AsyncGetData2Raw(context, request, cq));
}
std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>> PrepareAsyncGetData2(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>>(PrepareAsyncGetData2Raw(context, request, cq));
}
class async_interface {
public:
virtual ~async_interface() {}
virtual void GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response, std::function<void(::grpc::Status)>) = 0;
virtual void GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response, ::grpc::ClientUnaryReactor* reactor) = 0;
virtual void GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response, std::function<void(::grpc::Status)>) = 0;
virtual void GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response, ::grpc::ClientUnaryReactor* reactor) = 0;
};
typedef class async_interface experimental_async_interface;
virtual class async_interface* async() { return nullptr; }
class async_interface* experimental_async() { return async(); }
private:
virtual ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>* AsyncGetData1Raw(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) = 0;
virtual ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>* PrepareAsyncGetData1Raw(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) = 0;
virtual ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>* AsyncGetData2Raw(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) = 0;
virtual ::grpc::ClientAsyncResponseReaderInterface< ::market_data::MsgReply>* PrepareAsyncGetData2Raw(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) = 0;
};
class Stub final : public StubInterface {
public:
Stub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options = ::grpc::StubOptions());
::grpc::Status GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::market_data::MsgReply* response) override;
std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>> AsyncGetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>>(AsyncGetData1Raw(context, request, cq));
}
std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>> PrepareAsyncGetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>>(PrepareAsyncGetData1Raw(context, request, cq));
}
::grpc::Status GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::market_data::MsgReply* response) override;
std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>> AsyncGetData2(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>>(AsyncGetData2Raw(context, request, cq));
}
std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>> PrepareAsyncGetData2(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) {
return std::unique_ptr< ::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>>(PrepareAsyncGetData2Raw(context, request, cq));
}
class async final :
public StubInterface::async_interface {
public:
void GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response, std::function<void(::grpc::Status)>) override;
void GetData1(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response, ::grpc::ClientUnaryReactor* reactor) override;
void GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response, std::function<void(::grpc::Status)>) override;
void GetData2(::grpc::ClientContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response, ::grpc::ClientUnaryReactor* reactor) override;
private:
friend class Stub;
explicit async(Stub* stub): stub_(stub) { }
Stub* stub() { return stub_; }
Stub* stub_;
};
class async* async() override { return &async_stub_; }
private:
std::shared_ptr< ::grpc::ChannelInterface> channel_;
class async async_stub_{this};
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* AsyncGetData1Raw(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) override;
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* PrepareAsyncGetData1Raw(::grpc::ClientContext* context, const ::market_data::CThostFtdcDepthMarketDataField& request, ::grpc::CompletionQueue* cq) override;
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* AsyncGetData2Raw(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) override;
::grpc::ClientAsyncResponseReader< ::market_data::MsgReply>* PrepareAsyncGetData2Raw(::grpc::ClientContext* context, const ::market_data::ResMsg& request, ::grpc::CompletionQueue* cq) override;
const ::grpc::internal::RpcMethod rpcmethod_GetData1_;
const ::grpc::internal::RpcMethod rpcmethod_GetData2_;
};
static std::unique_ptr<Stub> NewStub(const std::shared_ptr< ::grpc::ChannelInterface>& channel, const ::grpc::StubOptions& options = ::grpc::StubOptions());
class Service : public ::grpc::Service {
public:
Service();
virtual ~Service();
virtual ::grpc::Status GetData1(::grpc::ServerContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response);
virtual ::grpc::Status GetData2(::grpc::ServerContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response);
};
template <class BaseClass>
class WithAsyncMethod_GetData1 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithAsyncMethod_GetData1() {
::grpc::Service::MarkMethodAsync(0);
}
~WithAsyncMethod_GetData1() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData1(::grpc::ServerContext* /*context*/, const ::market_data::CThostFtdcDepthMarketDataField* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
void RequestGetData1(::grpc::ServerContext* context, ::market_data::CThostFtdcDepthMarketDataField* request, ::grpc::ServerAsyncResponseWriter< ::market_data::MsgReply>* response, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) {
::grpc::Service::RequestAsyncUnary(0, context, request, response, new_call_cq, notification_cq, tag);
}
};
template <class BaseClass>
class WithAsyncMethod_GetData2 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithAsyncMethod_GetData2() {
::grpc::Service::MarkMethodAsync(1);
}
~WithAsyncMethod_GetData2() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData2(::grpc::ServerContext* /*context*/, const ::market_data::ResMsg* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
void RequestGetData2(::grpc::ServerContext* context, ::market_data::ResMsg* request, ::grpc::ServerAsyncResponseWriter< ::market_data::MsgReply>* response, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) {
::grpc::Service::RequestAsyncUnary(1, context, request, response, new_call_cq, notification_cq, tag);
}
};
typedef WithAsyncMethod_GetData1<WithAsyncMethod_GetData2<Service > > AsyncService;
template <class BaseClass>
class WithCallbackMethod_GetData1 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithCallbackMethod_GetData1() {
::grpc::Service::MarkMethodCallback(0,
new ::grpc::internal::CallbackUnaryHandler< ::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply>(
[this](
::grpc::CallbackServerContext* context, const ::market_data::CThostFtdcDepthMarketDataField* request, ::market_data::MsgReply* response) { return this->GetData1(context, request, response); }));}
void SetMessageAllocatorFor_GetData1(
::grpc::MessageAllocator< ::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply>* allocator) {
::grpc::internal::MethodHandler* const handler = ::grpc::Service::GetHandler(0);
static_cast<::grpc::internal::CallbackUnaryHandler< ::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply>*>(handler)
->SetMessageAllocator(allocator);
}
~WithCallbackMethod_GetData1() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData1(::grpc::ServerContext* /*context*/, const ::market_data::CThostFtdcDepthMarketDataField* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
virtual ::grpc::ServerUnaryReactor* GetData1(
::grpc::CallbackServerContext* /*context*/, const ::market_data::CThostFtdcDepthMarketDataField* /*request*/, ::market_data::MsgReply* /*response*/) { return nullptr; }
};
template <class BaseClass>
class WithCallbackMethod_GetData2 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithCallbackMethod_GetData2() {
::grpc::Service::MarkMethodCallback(1,
new ::grpc::internal::CallbackUnaryHandler< ::market_data::ResMsg, ::market_data::MsgReply>(
[this](
::grpc::CallbackServerContext* context, const ::market_data::ResMsg* request, ::market_data::MsgReply* response) { return this->GetData2(context, request, response); }));}
void SetMessageAllocatorFor_GetData2(
::grpc::MessageAllocator< ::market_data::ResMsg, ::market_data::MsgReply>* allocator) {
::grpc::internal::MethodHandler* const handler = ::grpc::Service::GetHandler(1);
static_cast<::grpc::internal::CallbackUnaryHandler< ::market_data::ResMsg, ::market_data::MsgReply>*>(handler)
->SetMessageAllocator(allocator);
}
~WithCallbackMethod_GetData2() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData2(::grpc::ServerContext* /*context*/, const ::market_data::ResMsg* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
virtual ::grpc::ServerUnaryReactor* GetData2(
::grpc::CallbackServerContext* /*context*/, const ::market_data::ResMsg* /*request*/, ::market_data::MsgReply* /*response*/) { return nullptr; }
};
typedef WithCallbackMethod_GetData1<WithCallbackMethod_GetData2<Service > > CallbackService;
typedef CallbackService ExperimentalCallbackService;
template <class BaseClass>
class WithGenericMethod_GetData1 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithGenericMethod_GetData1() {
::grpc::Service::MarkMethodGeneric(0);
}
~WithGenericMethod_GetData1() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData1(::grpc::ServerContext* /*context*/, const ::market_data::CThostFtdcDepthMarketDataField* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
};
template <class BaseClass>
class WithGenericMethod_GetData2 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithGenericMethod_GetData2() {
::grpc::Service::MarkMethodGeneric(1);
}
~WithGenericMethod_GetData2() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData2(::grpc::ServerContext* /*context*/, const ::market_data::ResMsg* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
};
template <class BaseClass>
class WithRawMethod_GetData1 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithRawMethod_GetData1() {
::grpc::Service::MarkMethodRaw(0);
}
~WithRawMethod_GetData1() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData1(::grpc::ServerContext* /*context*/, const ::market_data::CThostFtdcDepthMarketDataField* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
void RequestGetData1(::grpc::ServerContext* context, ::grpc::ByteBuffer* request, ::grpc::ServerAsyncResponseWriter< ::grpc::ByteBuffer>* response, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) {
::grpc::Service::RequestAsyncUnary(0, context, request, response, new_call_cq, notification_cq, tag);
}
};
template <class BaseClass>
class WithRawMethod_GetData2 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithRawMethod_GetData2() {
::grpc::Service::MarkMethodRaw(1);
}
~WithRawMethod_GetData2() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData2(::grpc::ServerContext* /*context*/, const ::market_data::ResMsg* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
void RequestGetData2(::grpc::ServerContext* context, ::grpc::ByteBuffer* request, ::grpc::ServerAsyncResponseWriter< ::grpc::ByteBuffer>* response, ::grpc::CompletionQueue* new_call_cq, ::grpc::ServerCompletionQueue* notification_cq, void *tag) {
::grpc::Service::RequestAsyncUnary(1, context, request, response, new_call_cq, notification_cq, tag);
}
};
template <class BaseClass>
class WithRawCallbackMethod_GetData1 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithRawCallbackMethod_GetData1() {
::grpc::Service::MarkMethodRawCallback(0,
new ::grpc::internal::CallbackUnaryHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>(
[this](
::grpc::CallbackServerContext* context, const ::grpc::ByteBuffer* request, ::grpc::ByteBuffer* response) { return this->GetData1(context, request, response); }));
}
~WithRawCallbackMethod_GetData1() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData1(::grpc::ServerContext* /*context*/, const ::market_data::CThostFtdcDepthMarketDataField* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
virtual ::grpc::ServerUnaryReactor* GetData1(
::grpc::CallbackServerContext* /*context*/, const ::grpc::ByteBuffer* /*request*/, ::grpc::ByteBuffer* /*response*/) { return nullptr; }
};
template <class BaseClass>
class WithRawCallbackMethod_GetData2 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithRawCallbackMethod_GetData2() {
::grpc::Service::MarkMethodRawCallback(1,
new ::grpc::internal::CallbackUnaryHandler< ::grpc::ByteBuffer, ::grpc::ByteBuffer>(
[this](
::grpc::CallbackServerContext* context, const ::grpc::ByteBuffer* request, ::grpc::ByteBuffer* response) { return this->GetData2(context, request, response); }));
}
~WithRawCallbackMethod_GetData2() override {
BaseClassMustBeDerivedFromService(this);
}
// disable synchronous version of this method
::grpc::Status GetData2(::grpc::ServerContext* /*context*/, const ::market_data::ResMsg* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
virtual ::grpc::ServerUnaryReactor* GetData2(
::grpc::CallbackServerContext* /*context*/, const ::grpc::ByteBuffer* /*request*/, ::grpc::ByteBuffer* /*response*/) { return nullptr; }
};
template <class BaseClass>
class WithStreamedUnaryMethod_GetData1 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithStreamedUnaryMethod_GetData1() {
::grpc::Service::MarkMethodStreamed(0,
new ::grpc::internal::StreamedUnaryHandler<
::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply>(
[this](::grpc::ServerContext* context,
::grpc::ServerUnaryStreamer<
::market_data::CThostFtdcDepthMarketDataField, ::market_data::MsgReply>* streamer) {
return this->StreamedGetData1(context,
streamer);
}));
}
~WithStreamedUnaryMethod_GetData1() override {
BaseClassMustBeDerivedFromService(this);
}
// disable regular version of this method
::grpc::Status GetData1(::grpc::ServerContext* /*context*/, const ::market_data::CThostFtdcDepthMarketDataField* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
// replace default version of method with streamed unary
virtual ::grpc::Status StreamedGetData1(::grpc::ServerContext* context, ::grpc::ServerUnaryStreamer< ::market_data::CThostFtdcDepthMarketDataField,::market_data::MsgReply>* server_unary_streamer) = 0;
};
template <class BaseClass>
class WithStreamedUnaryMethod_GetData2 : public BaseClass {
private:
void BaseClassMustBeDerivedFromService(const Service* /*service*/) {}
public:
WithStreamedUnaryMethod_GetData2() {
::grpc::Service::MarkMethodStreamed(1,
new ::grpc::internal::StreamedUnaryHandler<
::market_data::ResMsg, ::market_data::MsgReply>(
[this](::grpc::ServerContext* context,
::grpc::ServerUnaryStreamer<
::market_data::ResMsg, ::market_data::MsgReply>* streamer) {
return this->StreamedGetData2(context,
streamer);
}));
}
~WithStreamedUnaryMethod_GetData2() override {
BaseClassMustBeDerivedFromService(this);
}
// disable regular version of this method
::grpc::Status GetData2(::grpc::ServerContext* /*context*/, const ::market_data::ResMsg* /*request*/, ::market_data::MsgReply* /*response*/) override {
abort();
return ::grpc::Status(::grpc::StatusCode::UNIMPLEMENTED, "");
}
// replace default version of method with streamed unary
virtual ::grpc::Status StreamedGetData2(::grpc::ServerContext* context, ::grpc::ServerUnaryStreamer< ::market_data::ResMsg,::market_data::MsgReply>* server_unary_streamer) = 0;
};
typedef WithStreamedUnaryMethod_GetData1<WithStreamedUnaryMethod_GetData2<Service > > StreamedUnaryService;
typedef Service SplitStreamedService;
typedef WithStreamedUnaryMethod_GetData1<WithStreamedUnaryMethod_GetData2<Service > > StreamedService;
};
} // namespace market_data
#endif // GRPC_market_5fdata_2eproto__INCLUDED

2203
protoc/market_data.pb.cc Normal file

File diff suppressed because it is too large Load Diff

3069
protoc/market_data.pb.h Normal file

File diff suppressed because it is too large Load Diff

8
protoc_generate.sh Normal file
View File

@ -0,0 +1,8 @@
mkdir -p gen_code
generate_code(){
protoc -I ./ --grpc_out=./protoc --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` $1
protoc -I ./ --cpp_out=./protoc $1
}
generate_code $@

90
src/grpc_client_main.cc Normal file
View File

@ -0,0 +1,90 @@
#include <grpc/grpc.h>
#include <grpcpp/channel.h>
#include <grpcpp/client_context.h>
#include <grpcpp/create_channel.h>
#include <grpcpp/security/credentials.h>
#include <iostream>
#include <memory>
#include <string>
#include "common.h"
#include "market_data.pb.h"
#include "protoc.h"
class MyClient {
public:
// create stub
MyClient(std::shared_ptr<grpc::Channel> channel)
: stub_(market_data::TestService::NewStub(channel)) {}
void GetReplyMsg1() {
market_data::CThostFtdcDepthMarketDataField data;
market_data::MsgReply msg_reply;
data.set_turnover(10);
GetOneData1(data, &msg_reply);
}
void GetReplyMsg2() {
market_data::ResMsg data;
market_data::MsgReply msg_reply;
data.add_other_msg("123");
GetOneData2(data, &msg_reply);
}
private:
bool GetOneData1(const market_data::CThostFtdcDepthMarketDataField &data,
market_data::MsgReply *msg_reply) {
grpc::ClientContext context;
// auto start_time = GetTimeUs();
Status status = stub_->GetData1(&context, data, msg_reply);
// auto end_time = GetTimeUs();
// std::cout << "time diff:" << end_time - start_time << "us" << std::endl;
if (!status.ok()) {
std::cout << "GetData rpc failed." << std::endl;
return false;
}
if (msg_reply->message().empty()) {
std::cout << "message empty." << std::endl;
return false;
} else {
std::cout << "MsgReply:" << msg_reply->message() << std::endl;
}
return true;
}
bool GetOneData2(const market_data::ResMsg &data,
market_data::MsgReply *msg_reply) {
grpc::ClientContext context;
// auto start_time = GetTimeUs();
Status status = stub_->GetData2(&context, data, msg_reply);
// auto end_time = GetTimeUs();
// std::cout << "time diff:" << end_time - start_time << "us" << std::endl;
if (!status.ok()) {
std::cout << "GetData rpc failed." << std::endl;
return false;
}
if (msg_reply->message().empty()) {
std::cout << "message empty." << std::endl;
return false;
} else {
std::cout << "MsgReply:" << msg_reply->message() << std::endl;
}
return true;
}
std::unique_ptr<market_data::TestService::Stub> stub_;
};
int main(int argc, char **argv) {
// create a gRPC channel for our stub
// grpc::CreateChannel("locakhost:50051",grpc::InsecureChannelCredentials());
MyClient client(grpc::CreateChannel("localhost:1234",
grpc::InsecureChannelCredentials()));
std::cout << "=====================" << std::endl;
while (true) {
gxt::SleepMs(500);
client.GetReplyMsg1();
gxt::SleepMs(500);
client.GetReplyMsg2();
}
return 0;
}

65
src/grpc_server_main.cc Normal file
View File

@ -0,0 +1,65 @@
#include <forward_list>
#include <future>
#include <iostream>
#include <memory>
#include <mutex>
#include <string>
#include "common.h"
#include "protoc.h"
using market_data::CThostFtdcDepthMarketDataField;
class MyServer final : public market_data::TestService::Service {
public:
Status GetData1(ServerContext *context,
const CThostFtdcDepthMarketDataField *data,
market_data::MsgReply *msg) override {
// cout << "[get data]: " << data->data() << endl;
// string tmp("data received 12345");
// msg->set_message(data->data());
msg->set_message("getdata1"); // 原路返回
// gDebug(data->data().size());
return Status::OK;
}
Status GetData2(ServerContext *context, const market_data::ResMsg *data,
market_data::MsgReply *msg) override {
// cout << "[get data]: " << data->data() << endl;
// string tmp("data received 12345");
// msg->set_message(data->data());
msg->set_message("getdata2"); // 原路返回
// gDebug(data->data().size());
return Status::OK;
}
};
void RunServer() {
std::string server_addr("0.0.0.0:1234");
// std::string server_addr("127.0.0.1:1235");
// create an instance of our service implementation class Test1Impl
MyServer service;
// Create an instance of factory ServerBuilder class
ServerBuilder builder;
// Specify the address and port we want to use to listen for client requests
// using the builders AddListeningPort() method.
builder.AddListeningPort(server_addr, grpc::InsecureServerCredentials());
// Register our service implementation with the builder.
builder.RegisterService(&service);
// Call BuildAndStart() on the builder to create and start an RPC server for
// our service.
std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_addr << std::endl;
// Call Wait() on the server to do a blocking wait until process is killed or
// Shutdown() is called
server->Wait();
}
int main(int argc, char **argv) {
RunServer();
return 0;
}