| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- #include <QCoreApplication>
- #include <QDateTime>
- #include <iostream>
- #include <vector>
- #include "cumsgbuffer.h"
- #include "pcmsgbuffer.h"
- #include <iostream>
- #include <memory>
- #include <string>
- #include <grpcpp/grpcpp.h>
- #include <grpcpp/health_check_service_interface.h>
- #include <grpcpp/ext/proto_server_reflection_plugin.h>
- #include "uploadmsg.grpc.pb.h"
- using grpc::Server;
- using grpc::ServerBuilder;
- using grpc::ServerContext;
- using grpc::Status;
- #include <QDateTime>
- static cumsgbuffer gcumsgbuf;
- static pcmsgbuffer gpcmsgbuf;
- // Logic and data behind the server's behavior.
- class UploadServiceImpl final : public iv::Upload::Service {
- Status upload(ServerContext* context, const iv::UploadRequest* request,
- iv::UploadReply* reply) override {
- std::vector<char> xvectordata;
- qDebug("size is %d",request->xdata().size());
- if(request->xdata().size()>0)
- {
- xvectordata.resize(request->xdata().size());
- memcpy(xvectordata.data(),request->xdata().data(),request->xdata().size());
- }
- gcumsgbuf.addmsg(request->id(),request->ntime(),request->strvin(),request->strquerymd5(),request->strctrlmd5(),
- &xvectordata,request->bimportant(),request->kepptime());
- // std::string strVIN,strctrlMD5;
- int id;
- qint64 ntime;
- std::vector<char > xvectorctrldata;
- int nres = gpcmsgbuf.getmsg(request->strvin(),request->strctrlmd5(),id,ntime,&xvectorctrldata);
- reply->set_nres(nres);
- if(nres == 1)
- {
- reply->set_xdata(xvectorctrldata.data(),xvectorctrldata.size());
- }
- // char * strdata = new char[10000000];
- // qint64 time = QDateTime::currentMSecsSinceEpoch();
- // memcpy(strdata,&time,8);
- // reply->set_data(strdata,10000000);
- // delete strdata;
- return Status::OK;
- }
- Status query(ServerContext* context, const iv::queryreq* request,
- iv::queryReply* reply) override {
- int id;
- qint64 ntime;
- std::vector<char > xvectorquerydata;
- int nres = gcumsgbuf.getmsg(request->strvin(),request->strquerymd5(),request->nlasttime(),id,ntime,&xvectorquerydata);
- reply->set_nres(nres);
- if(nres > 0)
- {
- reply->set_data(xvectorquerydata.data(),xvectorquerydata.size());
- reply->set_id(id);
- reply->set_ntime(ntime);
- }
- return Status::OK;
- }
- Status ctrl(ServerContext* context, const iv::ctrlreq* request,
- iv::ctrlReply * reply) override {
- std::vector<char> xvectordata;
- if(request->data().size()>0)
- {
- xvectordata.resize(request->data().size());
- memcpy(xvectordata.data(),request->data().data(),request->data().size());
- }
- int nid = gpcmsgbuf.addmsg(request->id(),request->ntime(),request->strvin(),request->strctrlmd5(),&xvectordata,
- request->bimportant(),request->kepptime());
- reply->set_nsendid(nid);
- return Status::OK;
- }
- };
- void RunServer() {
- std::string server_address("0.0.0.0:50051");
- UploadServiceImpl service;
- grpc::EnableDefaultHealthCheckService(true);
- // grpc::reflection::InitProtoReflectionServerBuilderPlugin();
- ServerBuilder builder;
- // Listen on the given address without any authentication mechanism.
- builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
- builder.SetMaxReceiveMessageSize(300000000);
- // builder.SetMaxMessageSize(100000000);
- // builder.SetMaxSendMessageSize(100000000);
- // Register "service" as the instance through which we'll communicate with
- // clients. In this case it corresponds to an *synchronous* service.
- builder.RegisterService(&service);
- // Finally assemble the server.
- std::unique_ptr<Server> server(builder.BuildAndStart());
- std::cout << "Server listening on " << server_address << std::endl;
- // Wait for the server to shutdown. Note that some other thread must be
- // responsible for shutting down the server for this call to ever return.
- server->Wait();
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- gpcmsgbuf.start();
- RunServer();
- return a.exec();
- }
|