
#ifndef _MEDIACLIENTPROTOCOL_H_
#define _MEDIACLIENTPROTOCOL_H_

#include "MFSPublic.h"
// #include "3rd/uvw.hpp"
#include "BaseService.h"
#include "ZWRedisTool.h"
#include "MediaClientConnection.h"

namespace mfs
{
    
const uint8_t zwkj_Client_Head[4] = { 0x7a, 0x77, 0x6b, 0x6a};  // 'zwkj'
const uint8_t zwkj_Client_Tail[4] = { 0x61, 0x70, 0x70, 0x31};  // 'app1'
class MediaClientProtocol: public MediaClientConnection
{
    public:
    virtual int writeData(char* data, int dataLen) override;
    virtual void sendMessage(const char* data, int dataLen) override;
    virtual void generatePackageHead(int dataLen, char* head, int& headLen) override;
    virtual void generatePackageTail(int dataLen, char* tail, int& tailLen) override;
     
    public:
    MediaClientProtocol(std::shared_ptr<uvw::Loop> loop, std::shared_ptr<uvw::TCPHandle> client, const std::string& name)
    :MediaClientConnection(loop, client, name)
    {
        _streamInfo.targetType = "2";
        _status = SEND_HANDSHAKE;
        logd("TestFileClientWriteEnable="+std::to_string(AppSetting::Instance()->TestFileClientWriteEnable));
        if (AppSetting::Instance()->TestFileClientWriteEnable)
        {
            _cacheTestFile = "/home/MediaClientProtocol_"+ _clientIp +"_"+std::to_string(_clientPort)+".bin1";
            _cacheTestStream.open(_cacheTestFile, std::ios::binary | std::ios::trunc);
            _cacheTestFile = "/home/MediaClientProtocol_"+ _clientIp +"_"+std::to_string(_clientPort)+".bin2";
            _cacheTestData.open(_cacheTestFile, std::ios::binary | std::ios::trunc);
            logd("_cacheTestFile="+_cacheTestFile+" , isopen="+std::to_string(_cacheTestData.is_open()));
        }
        
    }


    private:
#if CONVERT_AAC
        std::shared_ptr<adpcm_state> _adpcmState;
        std::shared_ptr<Pcm2AAC> _pcm2aac;
    std::ofstream  testfilestream;
#endif
    int _testPackageIndex = 0;

    int _packageNumber = 0;
    int _wirtedData=0;

    int _logReadFlag = 0;
    int _logWriteFlag =0;

    std::string _cacheTestFile;

    int parseWebSocketHead();
    int parseWebSocketBody();
    virtual int parse(int length) override;
    virtual int recvHandShakeData() override;
    virtual int sendHandShakeData() override;

    
    virtual void alogd(const std::string& message, const std::string& fileName, const int line, const std::string& functionName) override {
        BaseProtocol::alogd(std::string()+"["+_url+"]"+message, fileName, line, functionName);
    }
    virtual void aloge(const std::string& message, const std::string& fileName, const int line, const std::string& functionName)  override {
        BaseProtocol::aloge(std::string()+"["+_url+"]"+message, fileName, line, functionName);
    }
    virtual void alogv(const std::string& message, const std::string& fileName, const int line, const std::string& functionName)  override {
        BaseProtocol::alogv(std::string()+"["+_url+"]"+message, fileName, line, functionName);
    }
};

}




#endif // _MEDIACLIENTPROTOCOL_H_