pans
2017-08-18 0607eba0ba964b3178507eabcd234fe7d8a0d860
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//
// 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