houxiao
2017-08-17 2b43077d967c28fe99e1ff2b99f19e1433c710d9
VisitFace/RtspNativeCodec/app/src/main/cpp/RemoteFunc.h
@@ -1,35 +1,78 @@
//
// Created by pansen on 2017/8/4.
//
#ifndef RTSPNATIVECODEC_REMOTEFUNC_H
#define RTSPNATIVECODEC_REMOTEFUNC_H
#include <iostream>
#include <capnp/ez-rpc.h>
#include "RemoteMethod.proto.h"
//::capnp::Capability::Server
template <class T> class RemoteServer{
public:
//    RemoteServer(std::string host, int port, capnp::EzRpcServer rpcServer) : rpcServer(rpcServer) {
//        this->rpcServer(kj::heap<T>(),host, port);
//                //.rpcServer(kj::heap<T>(),host, port);
//    };
    RemoteServer(std::string host,int port):host(host),port(port){};
    ~RemoteServer(){};
    bool start();
    void stop();
    bool running;
private:
    std::string host;
    int port;
    static void *rServer_thd(void *arg);
    pthread_t server_thd;
    pthread_mutex_t mutexSerevr;
};
#endif //RTSPNATIVECODEC_REMOTEFUNC_H
//
// Created by pansen on 2017/8/4.
//
#ifndef RTSPNATIVECODEC_REMOTEFUNC_H
#define RTSPNATIVECODEC_REMOTEFUNC_H
#include <iostream>
#include <capnp/ez-rpc.h>
#include <logger.h>
template<class T>
class RemoteServer
{
public:
   RemoteServer(std::string host, int port) : host(host), port(port) {};
   ~RemoteServer() {};
   bool start()
   {
      LOG_INFO << "server_thd::start" << LOG_ENDL;
      if (running == true)
      {
         LOG_INFO << "The server_thd is already running" << LOG_ENDL;
         return false;
      }
      running = true;
      pthread_mutex_init(&mutexSerevr, NULL); // 初始化互斥对象,动态加锁,用于server
      int retServer = pthread_create(&server_thid, NULL, RemoteServer::rServer_thd, this);
      if (retServer != 0)
      {
         LOGP(ERROR, "server_thd_create: %s/n", strerror(retServer));
         running = false;
         return false;
      }
      return true;
   }
   void stop()
   {
//    LOG_INFO << "server_thd::stop" << LOG_ENDL;
//    if (!running)
//        return;
//    running = false;
//    pthread_mutex_destroy(&mutexSerevr);
      //#todo
   }
   bool running;
private:
   std::string host;
   int port;
   static void *rServer_thd(void *arg)
   {
      try {
         RemoteServer &rs = *(RemoteServer *) arg;
         capnp::EzRpcServer trpcServer(kj::heap<T>(), rs.host, rs.port);
         auto &serverLoop = trpcServer.getWaitScope();
         kj::NEVER_DONE.wait(serverLoop);
      }
      catch (kj::Exception e){
         LOG_ERROR<<e.getLine()<<LOG_ENDL;
      }
   }
   pthread_t server_thid;
   pthread_mutex_t mutexSerevr;
};
#endif //RTSPNATIVECODEC_REMOTEFUNC_H