Newer
Older
casic_unitree_dog / src / tcp_client.hpp
//
// Created by casic on 25-2-25.
//

#ifndef TCP_CLIENT_HPP
#define TCP_CLIENT_HPP

#include <atomic>
#include <mutex>
#include <string>
#include <unistd.h>
#include <vector>
#include "slam_wrapper.hpp"

class TcpClient {
public:
    TcpClient();

    ~TcpClient() {
        if (_client_socket != -1) {
            close(_client_socket);
        }
    }

    void connect(const std::string &server_ip, int server_port);

    void stop() const { _should_stop = true; }

private:
    mutable std::atomic<bool> _should_stop{false};
    mutable std::mutex _mutex;
    int _client_socket{-1};
    int _max_retries = 12;
    int _retry_interval = 5000; //重连间隔
    SlamWrapper _slam;

    void receive_data();

    void handle_data(const std::vector<char> &buffer);
};

#endif //TCP_CLIENT_HPP