2024-03-15 12:31:34 +08:00
|
|
|
|
#include "StreamClient.h"
|
2024-04-01 18:26:14 +08:00
|
|
|
|
#include "../utils/LocalAddr.h"
|
2024-03-15 12:31:34 +08:00
|
|
|
|
|
|
|
|
|
StreamClient::StreamClient()
|
|
|
|
|
: m_localIp("0.0.0.0")
|
|
|
|
|
, m_port(50010)
|
|
|
|
|
, m_readQuitFlag(false)
|
|
|
|
|
, m_writeQuitFlag(false)
|
|
|
|
|
, m_dataCallBack(nullptr)
|
|
|
|
|
, m_handlePtr(nullptr){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StreamClient::~StreamClient() {
|
|
|
|
|
m_readQuitFlag = true;
|
|
|
|
|
m_writeQuitFlag = true;
|
|
|
|
|
|
2024-04-02 17:45:03 +08:00
|
|
|
|
if (m_connectTd.joinable()) {
|
|
|
|
|
m_connectTd.join();
|
|
|
|
|
}
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void StreamClient::Init() {
|
|
|
|
|
m_localIp = LocalAddr().GetSystemIpAddress();
|
|
|
|
|
std::string target_str = m_localIp +":"+ std::to_string(m_port);
|
2024-04-24 18:12:41 +08:00
|
|
|
|
m_channelTwo = grpc::CreateChannel(target_str, grpc::InsecureChannelCredentials());
|
|
|
|
|
m_stubTwo = Stream::NewStub(m_channelTwo);
|
2024-03-15 12:31:34 +08:00
|
|
|
|
|
2024-04-01 18:26:14 +08:00
|
|
|
|
m_connectTd = std::thread([this]() {
|
|
|
|
|
this->AllStream();
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool StreamClient::GetPushMsg(WriteData& msg) {
|
|
|
|
|
std::lock_guard<std::mutex> lock(m_msgLock);
|
|
|
|
|
if (m_msgList.empty()) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
else {
|
2024-04-24 10:28:50 +08:00
|
|
|
|
msg = m_msgList.front();
|
2024-03-15 12:31:34 +08:00
|
|
|
|
m_msgList.pop_front();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-04-24 10:28:50 +08:00
|
|
|
|
void StreamClient::SetPushMsg(const WriteData& msg) {
|
2024-03-15 12:31:34 +08:00
|
|
|
|
std::lock_guard<std::mutex> lock(m_msgLock);
|
2024-04-24 10:28:50 +08:00
|
|
|
|
m_msgList.emplace_back(msg);
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void StreamClient::AllStream() {
|
|
|
|
|
|
|
|
|
|
ClientContext context;
|
|
|
|
|
std::unique_ptr<grpc::ClientReaderWriter<RequestInfo, ResponseInfo>> stream(m_stubTwo->AllStream(&context));
|
|
|
|
|
|
|
|
|
|
std::thread reader([this, &stream] {
|
|
|
|
|
ResponseInfo readInfo;
|
|
|
|
|
while (!m_readQuitFlag && stream->Read(&readInfo)) {
|
|
|
|
|
ReadData readData;
|
|
|
|
|
readData.dataType = (READTYPE)readInfo.datatype();
|
2024-04-11 10:15:32 +08:00
|
|
|
|
std::list<Item> its;
|
|
|
|
|
for (const ::stream::ParamInfo& it : readInfo.item()) {
|
|
|
|
|
readData.its.emplace_back(Item{ it.namekey(),it.strvalue(),(DATATYPE)it.valuetype()});
|
|
|
|
|
printf("接收到服务端消息:dataType:%d,nameKey:%s, strvalue:%s,valueType:%d\n",
|
|
|
|
|
readData.dataType, it.namekey().data(), it.strvalue().c_str(), it.valuetype());
|
|
|
|
|
}
|
2024-03-15 12:31:34 +08:00
|
|
|
|
readData.result = readInfo.result();
|
|
|
|
|
|
2024-04-11 10:15:32 +08:00
|
|
|
|
|
2024-03-15 12:31:34 +08:00
|
|
|
|
|
|
|
|
|
if (m_dataCallBack) {
|
|
|
|
|
m_dataCallBack(m_handlePtr, readData);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::thread writer([this, &stream] {
|
|
|
|
|
while(!m_writeQuitFlag){
|
|
|
|
|
WriteData writeData;
|
|
|
|
|
if (GetPushMsg(writeData)) {
|
|
|
|
|
RequestInfo request;
|
2024-04-09 16:53:02 +08:00
|
|
|
|
request.set_namekey(writeData.nameKey);
|
2024-04-08 13:43:56 +08:00
|
|
|
|
request.set_datatype(writeData.dataType);
|
2024-03-15 12:31:34 +08:00
|
|
|
|
request.set_strvalue(writeData.strValue);
|
2024-04-11 10:15:32 +08:00
|
|
|
|
request.set_valuetype((::stream::TYPE)writeData.valueType);
|
2024-03-15 12:31:34 +08:00
|
|
|
|
stream->Write(request);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
stream->WritesDone();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
reader.join();
|
|
|
|
|
writer.join();
|
|
|
|
|
|
|
|
|
|
Status status = stream->Finish();
|
|
|
|
|
if (!status.ok()) {
|
|
|
|
|
std::cout << "RPC failed: " << status.error_message() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-04-24 18:12:41 +08:00
|
|
|
|
bool StreamClient::GetLayerByIndex(int index, ::stream::ResponseInfo* response){
|
|
|
|
|
// 初始化 gRPC
|
|
|
|
|
std::string targetStr = m_localIp + ":" + std::to_string(m_port);
|
|
|
|
|
std::unique_ptr<Stream::Stub> _stub = Stream::NewStub(grpc::CreateChannel(targetStr, grpc::InsecureChannelCredentials()));
|
|
|
|
|
|
|
|
|
|
ClientContext context;
|
|
|
|
|
::stream::RequestInfo request;
|
|
|
|
|
request.set_datatype(LAYERDATAREQ);
|
|
|
|
|
request.set_strvalue(std::to_string(index));
|
|
|
|
|
request.set_valuetype((stream::TYPE)iINT);
|
|
|
|
|
Status status = _stub->Simple(&context, request, response);
|
|
|
|
|
return status.ok();
|
|
|
|
|
}
|