2024-03-15 12:31:34 +08:00
|
|
|
|
#include "StreamServer.h"
|
|
|
|
|
|
2024-03-22 11:28:06 +08:00
|
|
|
|
StreamServer::StreamServer(Machine* p)
|
2024-03-15 12:31:34 +08:00
|
|
|
|
: m_port(50010)
|
|
|
|
|
, m_checkQuitFlag(false)
|
2024-03-22 11:28:06 +08:00
|
|
|
|
, m_dataCallBack(nullptr)
|
2024-04-08 13:43:56 +08:00
|
|
|
|
, m_machine(p)
|
|
|
|
|
, m_handlePtr(nullptr){
|
2024-03-15 12:31:34 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
StreamServer::~StreamServer() {
|
2024-03-22 11:28:06 +08:00
|
|
|
|
Stop();
|
2024-04-07 17:09:01 +08:00
|
|
|
|
|
|
|
|
|
m_checkQuitFlag = true;
|
|
|
|
|
if (m_checkCloseTd.joinable()) m_checkCloseTd.join();
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Status StreamServer::AllStream(ServerContext* context, grpc::ServerReaderWriter<ResponseInfo, RequestInfo>* stream) {
|
|
|
|
|
ClientInfo* cinfo = new ClientInfo();
|
|
|
|
|
|
2024-04-03 13:55:07 +08:00
|
|
|
|
string addr = context->peer();
|
|
|
|
|
size_t pos = addr.find_first_of(':');
|
|
|
|
|
printf("%s client login...\n", addr.substr(pos+1).c_str());
|
2024-03-15 12:31:34 +08:00
|
|
|
|
cinfo->m_clientAddr = context->peer();
|
|
|
|
|
cinfo->m_context = context;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lck(m_clientMutex);
|
|
|
|
|
m_clientList.emplace_back(cinfo);
|
2024-03-22 11:28:06 +08:00
|
|
|
|
m_machine->UpdateClients(m_clientList);
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::thread read([this, &stream, cinfo] {
|
|
|
|
|
RequestInfo request;
|
|
|
|
|
while (!cinfo->m_readQuitFlag && stream->Read(&request)) {
|
|
|
|
|
|
|
|
|
|
ReadData readData;
|
|
|
|
|
readData.dataType = (READTYPE)request.datatype();
|
|
|
|
|
readData.nameKey = request.namekey();
|
|
|
|
|
readData.strValue = request.strvalue();
|
|
|
|
|
readData.valueType = (DATATYPE)request.valuetype();
|
|
|
|
|
readData.clientPtr = cinfo;
|
|
|
|
|
|
|
|
|
|
printf("客户端消息:dataType:%d,nameKey:%s, strValue:%s,valueType:%d",
|
|
|
|
|
readData.dataType, readData.nameKey.c_str(), readData.strValue.c_str(), readData.valueType);
|
|
|
|
|
if (m_dataCallBack) {
|
|
|
|
|
m_dataCallBack(m_handlePtr,readData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
std::thread write([this, &stream, cinfo] {
|
|
|
|
|
while(!cinfo->m_writeQuitFlag) {
|
|
|
|
|
WriteData writeData;
|
|
|
|
|
if (cinfo->GetPushMsg(writeData)) {
|
|
|
|
|
ResponseInfo response;
|
|
|
|
|
response.set_result(true);
|
2024-04-08 13:43:56 +08:00
|
|
|
|
response.set_datatype(writeData.dataType);
|
2024-03-15 12:31:34 +08:00
|
|
|
|
response.set_strvalue(writeData.strValue);
|
|
|
|
|
response.set_namekey(writeData.nameKey);
|
|
|
|
|
response.set_valuetype((::stream::ResponseInfo_TYPE)writeData.valueType);
|
|
|
|
|
stream->Write(response);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
read.join();
|
|
|
|
|
write.join();
|
|
|
|
|
|
|
|
|
|
return Status::OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-22 11:28:06 +08:00
|
|
|
|
|
|
|
|
|
void StreamServer::Init() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void StreamServer::Run() {
|
2024-04-08 13:43:56 +08:00
|
|
|
|
//检测下线线程
|
2024-03-22 11:28:06 +08:00
|
|
|
|
if (!m_checkCloseTd.joinable()) {
|
|
|
|
|
m_checkCloseTd = std::thread([this] {
|
|
|
|
|
while (!m_checkQuitFlag) {
|
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lck(m_clientMutex);
|
|
|
|
|
bool ischange = false;
|
|
|
|
|
for (auto client = m_clientList.begin(); client != m_clientList.end();) {
|
|
|
|
|
if (!(*client)->IsConnect()) {
|
|
|
|
|
printf("%s 下线了...\n", (*client)->m_clientAddr.c_str());
|
|
|
|
|
delete (*client);
|
|
|
|
|
client = m_clientList.erase(client);
|
|
|
|
|
ischange = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
++client;
|
2024-03-19 17:45:12 +08:00
|
|
|
|
}
|
2024-03-22 11:28:06 +08:00
|
|
|
|
if (ischange) m_machine->UpdateClients(m_clientList);
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
2024-03-22 11:28:06 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2024-03-22 11:28:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//监听线程
|
|
|
|
|
if (!m_listenTd.joinable()) {
|
|
|
|
|
m_listenTd = std::thread([this] {
|
|
|
|
|
std::string server_address("0.0.0.0:" + std::to_string(m_port));
|
2024-04-07 17:09:01 +08:00
|
|
|
|
//std::cout << "Server listening on " << server_address << std::endl;
|
2024-03-22 11:28:06 +08:00
|
|
|
|
ServerBuilder builder;
|
|
|
|
|
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
|
|
|
|
builder.RegisterService(this);
|
|
|
|
|
m_server = builder.BuildAndStart();
|
|
|
|
|
m_server->Wait();
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-03-22 11:28:06 +08:00
|
|
|
|
void StreamServer::Stop() {
|
|
|
|
|
m_server->Shutdown();
|
|
|
|
|
if (m_listenTd.joinable()) {
|
|
|
|
|
m_listenTd.join();
|
|
|
|
|
}
|
2024-03-15 12:31:34 +08:00
|
|
|
|
|
2024-04-08 13:43:56 +08:00
|
|
|
|
{
|
|
|
|
|
std::lock_guard<std::mutex> lck(m_clientMutex);
|
|
|
|
|
for (auto client = m_clientList.begin(); client != m_clientList.end(); ++client) {
|
|
|
|
|
delete (*client);
|
|
|
|
|
}
|
|
|
|
|
m_clientList.clear();
|
|
|
|
|
}
|
2024-03-15 12:31:34 +08:00
|
|
|
|
}
|