//
//  MediaForwardService.h
//
//  Created by 杨净 on 2018/3/20.
//  Copyright © 2018年 杨净. All rights reserved.
//
//  email: 1026397500@qq.com
//

#ifndef _MEDIAFORWARDSERVICE_H_
#define _MEDIAFORWARDSERVICE_H_

#include "MFSPublic.h"
#include "3rd/uvw.hpp"
#include "AppSetting.h"

namespace mfs
{

class NetTool;
class WebSocket;
class Rtp808;
class MediaInfo;

class MediaForwardService: public NoCopy
{
public:
    MediaForwardService(int webSocketPort, int rtpPort, int rtpybPort, int videoInfoPort, int appPort,int rtmpPort, int rtpPortSend, int rtpPortSendyb, const std::string& type) : _websocketPort(webSocketPort), 
    _rtpPort(rtpPort), _rtpybPort(rtpybPort), _videoInfoPort(videoInfoPort),_appPort(appPort), _type(type),_rtmpPort(rtmpPort),_rtpPortSend(rtpPortSend),_rtpPortSendyb(rtpPortSendyb)
    {
        AppSetting::Instance()->rtpPortSend = _rtpPortSend;
        AppSetting::Instance()->rtpPortSendyb = _rtpPortSendyb;
        
        _epollFd = epoll_create(100);
        FORWORT_ERRNO_ASSERT(_epollFd);
        _epollEvent.events = EPOLLIN;
        // _typeData[0] = std::make_pair<SocketType, void*>(WebsocketServiceType, &_webSocketFd);
        _typeData[1] = std::make_pair<SocketType, void*>(RtpServiceType, &_rtpSocketFd);
        _typeData[2] = std::make_pair<SocketType, void*>(VideoInfoServiceType, &_videoInfoSocketFd);
    }
    int start();

private:
    int _webSocketFd = -1;
    int _rtpSocketFd = -1;
    int _videoInfoSocketFd = -1;

    const int _eventTimeOut = 10000;

    int _epollFd;
    int _websocketPort;
    int _rtpPort;
    int _rtpybPort;
    int _videoInfoPort;
    int _appPort;
    int _rtmpPort;
    int _rtpPortSend;
    int _rtpPortSendyb;
    
    std::string _type;

    epoll_event _epollEvent = {0};

    struct timespec _currentTime = {0};

    std::set<MediaInfo*> _videoInfoClientSet;                                   // 所有MediaInfo连接

    int initSocket(int& socketFd, int port, void* userData);

    void uninitSocket(int& socketFd);

    std::pair<SocketType, void*> _typeData[3];

    int init();

    int doServiceEvent(int event, std::pair<SocketType, void*>* evenData);

    
    int InitializeAutomationTask(uvw::Loop &loop);
};

}

#endif
