68 lines
1.6 KiB
C++
68 lines
1.6 KiB
C++
#pragma once
|
|
#include <thread>
|
|
#include <vector>
|
|
#include <string>
|
|
#include <grpcpp/grpcpp.h>
|
|
#include <grpc/support/log.h>
|
|
#include "../protobuf/stream.grpc.pb.h"
|
|
#include "ClientInfo.h"
|
|
#include "../Machine/Machine.h"
|
|
|
|
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);
|
|
typedef void (*ClientStateCallBack)(void* pthis,const ReadData& msg);
|
|
|
|
|
|
public:
|
|
StreamServer(Machine* pConfig);
|
|
~StreamServer();
|
|
|
|
void Init();
|
|
void Run();
|
|
void Stop();
|
|
|
|
void SetCallBackFunc(void* pdata,DataCallBack dataCallBack) {
|
|
m_dataCallBack = dataCallBack;
|
|
m_handlePtr = pdata;
|
|
}
|
|
|
|
ClientInfo* GetClient() {
|
|
std::lock_guard<std::mutex> lck(m_clientMutex);
|
|
return m_clientList.empty() ? nullptr : m_clientList.front();
|
|
}
|
|
|
|
ClientInfo* GetAllClient() {
|
|
std::lock_guard<std::mutex> lck(m_clientMutex);
|
|
return m_clientList.front();
|
|
}
|
|
|
|
private:
|
|
Status AllStream(ServerContext* context, grpc::ServerReaderWriter<ResponseInfo, RequestInfo>* stream) override;
|
|
|
|
private:
|
|
std::mutex m_clientMutex;
|
|
std::list<ClientInfo*> m_clientList;
|
|
|
|
std::thread m_checkCloseTd; //检测客户端关闭线程
|
|
bool m_checkQuitFlag;
|
|
DataCallBack m_dataCallBack;
|
|
|
|
int m_port; //监听端口
|
|
void* m_handlePtr;
|
|
|
|
Machine* m_machine;
|
|
|
|
std::thread m_listenTd;
|
|
std::unique_ptr<Server> m_server;
|
|
|
|
};
|