SipServer.h 2.92 KB
#ifndef __SIPSERVER_H__
#define __SIPSERVER_H__

extern "C" {
#include <osip2/osip_mt.h>
#include <eXosip2/eXosip.h>
}

#include <map>
#include <string>
#include <thread>
#include <chrono>
#include <mutex>

#include "./Message/CatalogParser.h"
#include "sip_header.h"

using namespace std;

class Client {
public:
    Client(string ip, int port, string device) :
            mIp(ip),
            mPort(port),
            mRtpPort(0),
            mDevice(device),
            mIsReg(false){
    }
    ~Client() = default;
public:

    void setRtpPort(int rtpPort) {
        mRtpPort = rtpPort;
    }

    void setReg(bool isReg) {
        mIsReg = isReg;
    }
    string  getDevice() const{
        return mDevice;
    }
    string  getIp() const{
        return mIp;
    }
    int getPort() const{
        return mPort;
    }

    unsigned long getHeartBeat() {
        return mHeartBeatTime;
    }

    void updateHeartBeat(unsigned long ts) {
        mHeartBeatTime = ts;
    }

private:
    string mIp; // client ip
    int mPort; // client port
    string mDevice;// 340200000013200000024
    bool mIsReg;
    int mRtpPort;
    unsigned long mHeartBeatTime{0};

};


class SipServer {
public:
    static SipServer* getInstance(){
		static SipServer* singleton = nullptr;
		if (singleton == nullptr){
			singleton = new SipServer();
		}
		return singleton;
	}
    
public:
    SipServer();
    ~SipServer();

    bool Init(ServerInfo* pInfo);

    int RequestInvite_UDP(const char* dst_channel, int rtpPort);

    int RequestInvite_TCP_a(const char* dst_channel, int rtpPort);

    void close();

public:
    void event_loop();
    void timing_getcatlog();

private:
    int init_sip_server();
    int sip_event_handle(eXosip_event_t *evtp);

    void RequestCatalog(Client* client);
    void cacheCatalog();

    void response_message_answer(eXosip_event_t *evtp,int code);
    void response_register(eXosip_event_t *evtp);
    void response_register_401unauthorized(eXosip_event_t *evt);
    void response_message(eXosip_event_t *evtp);
    void response_invite_ack(eXosip_event_t *evtp);
    int request_bye(eXosip_event_t* evtp);// 通知相机停止推流
    int parse_xml(const char* data, const char* s_mark, bool with_s_make, const char* e_mark, bool with_e_make, char* dest);
    void dump_request(eXosip_event_t *evtp);
    void dump_response(eXosip_event_t *evtp);

    int clearClientMap();
    void deleteClientByDevice(string device);

    Client* get_parent_by_id(string id);
    bool check_device_status(string id);

private:
    bool mQuit{ false };
    eXosip_t *mSipCtx;
    ServerInfo mInfo;

    std::map<std::string, Client *> mClientMap;// <DeviceID,SipClient>
    mutex m_client_map_mtx;
    std::map<std::string, DeviceInfo> m_device_map;
    mutex m_device_map_mtx;

    thread* m_event_loop_thread;
};


#endif //__SIPSERVER_H__