
#include <string>
#include <iostream>
#include "../3rd/uvw.hpp"

using namespace std;

int main(int argc, char** argv)
{
    if (argc!=2)
    {
        cout <<"7972:实时视频；7975:对讲；7978:视频回放"<<endl;
        return 0;
    }
    int port = atoi(argv[1]);
    if (port != 7972 && port != 7978 && port != 7978)
    {
        cout <<"7972:实时视频；7975:对讲；7978:视频回放"<<endl;
        cout <<argv[1]<<endl;
        cout <<port<<endl;
        return 0;
    }
    
    
    const std::string address = std::string{"127.0.0.1"};

    auto loop = uvw::Loop::getDefault();
    auto client = loop->resource<uvw::TCPHandle>();
    client->on<uvw::CloseEvent>([loop](const uvw::CloseEvent &, uvw::TCPHandle &client) 
    {
        cout << "连接关闭" << endl;
    });
    client->on<uvw::WriteEvent>([](const uvw::WriteEvent &event, uvw::TCPHandle &client) 
    {
    });
    client->on<uvw::EndEvent>([](const uvw::EndEvent &, uvw::TCPHandle &client) { 
        client.close(); 
    });
    client->on<uvw::ErrorEvent>([](const uvw::ErrorEvent &event, uvw::TCPHandle &client) {
        cout << event.name() << "("<< event.code() <<"):"<<event.what()<<endl;
        client.close();
    });
    client->on<uvw::DataEvent>([](uvw::DataEvent &event, uvw::TCPHandle &client) 
    {
        cout << event.data.get()<<endl;
    });
    client->on<uvw::AllocEvent>([](const uvw::AllocEvent &event, uvw::TCPHandle &client) 
    {
    });
    client->once<uvw::ConnectEvent>([](const uvw::ConnectEvent &, uvw::TCPHandle &client) {
        cout << "建立连接" <<endl;
        client.read();
    });
    client->connect(address, port);
    loop->run();
    cout << "退出程序" << endl;
    return 0;
}