grpc流式傳輸示例(c++)-創(chuàng)新互聯(lián)

目錄

十年的啟東網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。營銷型網(wǎng)站的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調整啟東建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)從事“啟東網(wǎng)站設計”,“啟東網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。

grpc理解

流式傳輸方式

Attention


grpc理解

grpc是結合protobuf的遠程調用框架,服務端和客戶端均支持同步和異步模式。同步模式下,服務器的service函數(shù)會阻塞,且當前線程不能再服務其它的client,類似于多線程模式,一個線程服務一個client,可通過ResourceQuota設置大線程數(shù);異步模式下,grpc提供類似poll方式管理事件,用戶注冊事件,并接收通知,一個線程管理一個CompletionQueue,通過輪詢管理clients,線程模型可以用戶自定義,在追求性能的情況下官方推薦異步模式。

grpc::ResourceQuota resource_quata;
resource_quata.SetMaxThreads(2);
builder.SetResourceQuota(resource_quata);
流式傳輸方式

grpc自定義的service函數(shù)可以定義流式的參數(shù)和返回值,對應的函數(shù)實現(xiàn)通過Reader,Writer,ReaderWriter實現(xiàn)用戶自定義的邏輯,從而減少內存的使用,同時在部分數(shù)據(jù)到來時就可以進行數(shù)據(jù)處理。

proto文件

syntax="proto3";
package support;

service SupportGenerator{
    rpc GenSupport(stream STL) returns( stream STL){}
}

message Point{
    float x=1;
    float y=2;
    float z=3;
}

message STL {    
    repeated Point points=1;
}

服務端的代碼如下

#pragma once
#include#include#include"support.grpc.pb.h"
#include"support.pb.h"

//server impl
class SupportGeneratorService final :public support::SupportGenerator::Service
{
public://important
	grpc::Status GenSupport(grpc::ServerContext* context, grpc::ServerReaderWriter* stl_reader_writer) override
	{
		vectorstls_; //recv
		vectorvertics_;//recv
		vectorcoordinates_;//recv
		support::STL stl;//send
		while (stl_reader_writer->Read(&stl))
		{
			google::protobuf::RepeatedPtrFieldpoints = stl.points();
			CoutInLine("recieved from client ", points.size());
			stls_.emplace_back(stl);	
			for (auto& point : points)
			{
				vertics_.push_back(point);	
				coordinates_.push_back(point.x());
				coordinates_.push_back(point.y());
				coordinates_.push_back(point.z());
			}
		}		
        //return the request stl
		//for (auto stl_recieved : stls_)
		//{
		//	stl_reader_writer->Write(stl_recieved);
		//}		
		//return grpc::Status::OK;

		//handle	
#pragma region handle request
		
#pragma endregion

		auto support_vertics = what you return ;
		int once_count = 100;
		int loop_count = support_vertics.size() / once_count + 1;
		int index = 0;
		for (int loop = 0; loop< loop_count; loop++)
		{
			support::STL stl;
			for (int j = 0; j< once_count && index< support_vertics.size(); j++)
			{
				auto vertic = support_vertics[index++];
				auto point = stl.add_points();
				point->set_x(vertic.x);
				point->set_y(vertic.y);
				point->set_z(vertic.z);
			}
			stl_reader_writer->Write(stl);
		}
		return grpc::Status::OK;
	}
};

class HServer
{
public:

	HServer()
	{		
		grpc::ServerBuilder builder;
		builder.AddListeningPort("0.0.0.0:5000", grpc::InsecureServerCredentials());
		builder.RegisterService(&support_generator_service_);
		//對于同步server 的資源管理
		grpc::ResourceQuota resource_quata;
		resource_quata.SetMaxThreads(2);
		builder.SetResourceQuota(resource_quata);
		server_ = builder.BuildAndStart();
	}

	~HServer()
	{
		Stop();
	}

	void RunBackground();
	
	void Stop();

private:
	unique_ptrserver_;	
	SupportGeneratorService support_generator_service_;
};

客戶端代碼

std::shared_ptrchannel = grpc::CreateChannel("127.0.0.1:5000", grpc::InsecureChannelCredentials());
	std::unique_ptrstub = support::SupportGenerator::NewStub(channel);
		
	while (true)
	{
		getchar();
		grpc::ClientContext client_context;
		std::shared_ptr>client_reader_writer(stub->GenSupport(&client_context));


        //recieved
        std::vectorpoints;
		//read stl
        

        int n_vertics = vertics.size();
        int once_count = 100;
        int loop_count = n_vertics / once_count + 1;
        int index = 0;
		for (int loop = 0; loop< loop_count; loop++)
		{
            auto vertics = ReadStl("");//vertics should be read once_count every time in loop
			support::STL stl;
			for (int i = 0; i< once_count && indexset_x(vertics[index].x());
				point->set_y(vertics[index].y());
				point->set_z(vertics[index].z());

                index++;
			}
			client_reader_writer->Write(stl);
			std::cout<< "client write stl "<< loop<<"   "<WritesDone();
		std::cout<< "write done "<Read(&stl))
		{
            auto points_once = stl.points();
            for (auto& point : points_once)
            {
                points.emplace_back(point);//get all data in memory
            }
            Save("", points_once);//save by many times can reduce memory usage
			std::cout<< "recieved from server  "<
Attention

protobuf 二進制序列化和反序列化需要protoc可執(zhí)行文件產(chǎn)生序列化對象的相關代碼;與grpc結合需要grpc自帶的對應編程語言的插件產(chǎn)生服務基類的相關代碼,二者版本需要相容。直接通過grpc安裝protobuf。

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

分享名稱:grpc流式傳輸示例(c++)-創(chuàng)新互聯(lián)
路徑分享:http://www.muchs.cn/article36/dsjspg.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、標簽優(yōu)化電子商務、搜索引擎優(yōu)化、網(wǎng)站改版網(wǎng)站維護

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)