GrpcPrint/PrintS/DataManage/StreamServer.h

68 lines
1.6 KiB
C
Raw Normal View History

2024-03-15 12:31:34 +08:00
#pragma once
#include <thread>
#include <vector>
#include <string>
#include <grpcpp/grpcpp.h>
#include <grpc/support/log.h>
#include "../protobuf/stream.grpc.pb.h"
2024-03-19 17:45:12 +08:00
#include "ClientInfo.h"
2024-03-22 11:28:06 +08:00
#include "../Machine/Machine.h"
2024-03-15 12:31:34 +08:00
using grpc::Server;
using grpc::ServerBuilder;
using grpc::ServerContext;
using grpc::Status;
using stream::Stream;
using stream::RequestInfo;
using stream::ResponseInfo;
class StreamServer final : public Stream::Service {
typedef void (*DataCallBack)(void* pthis,const ReadData& msg);
2024-03-22 11:28:06 +08:00
typedef void (*ClientStateCallBack)(void* pthis,const ReadData& msg);
2024-03-15 12:31:34 +08:00
public:
2024-03-22 11:28:06 +08:00
StreamServer(Machine* pConfig);
2024-03-15 12:31:34 +08:00
~StreamServer();
2024-03-22 11:28:06 +08:00
void Init();
2024-03-15 12:31:34 +08:00
void Run();
2024-03-22 11:28:06 +08:00
void Stop();
2024-03-15 12:31:34 +08:00
void SetCallBackFunc(void* pdata,DataCallBack dataCallBack) {
m_dataCallBack = dataCallBack;
m_handlePtr = pdata;
}
2024-04-18 11:59:51 +08:00
//ClientInfo* GetClient() {
// std::lock_guard<std::mutex> lck(m_clientMutex);
// return m_clientList.empty() ? nullptr : m_clientList.front();
//}
2024-03-15 12:31:34 +08:00
2024-04-18 11:59:51 +08:00
//ClientInfo* GetAllClient() {
// std::lock_guard<std::mutex> lck(m_clientMutex);
// return m_clientList.front();
//}
2024-03-19 17:45:12 +08:00
2024-03-15 12:31:34 +08:00
private:
Status AllStream(ServerContext* context, grpc::ServerReaderWriter<ResponseInfo, RequestInfo>* stream) override;
private:
2024-04-18 11:59:51 +08:00
//std::mutex m_clientMutex;
//std::list<ClientInfo*> m_clientList;
2024-03-15 12:31:34 +08:00
std::thread m_checkCloseTd; //检测客户端关闭线程
bool m_checkQuitFlag;
DataCallBack m_dataCallBack;
int m_port; //监听端口
void* m_handlePtr;
2024-03-19 17:45:12 +08:00
2024-03-22 11:28:06 +08:00
Machine* m_machine;
std::thread m_listenTd;
std::unique_ptr<Server> m_server;
2024-03-15 12:31:34 +08:00
};