#include "client_tcp_connect.hpp"
#include "abstract_tcp_connect.hpp"
#include "logger.hpp"
#include "tcp_client.hpp"
#include "tinypb_data.hpp"
#include <memory>



namespace tinyrpc {
    ClientTcpConnection::ClientTcpConnection(int fd, Reactor& reactor, TcpClient& cli) :
    AbstractTcpConnection(fd, reactor),
    m_client(cli)
    {

    }
    
    ClientTcpConnection::~ClientTcpConnection() {

    }

    void ClientTcpConnection::process()  {
        while(m_readBuffer.getReadable() > 0) {
            std::shared_ptr<AbstractData> data(new TinypbData);

            bool ret = m_client.getCoder().decoder(m_readBuffer, *data);

            if(ret == false) {
                logger() << "decode error";
                break;
            }
            
            std::shared_ptr<TinypbData> tmp = std::dynamic_pointer_cast<TinypbData>(data);
            m_respond_datas[tmp->msg_req] = data;
             
        }
    }
}