Program Listing for File TcpDaemon.h

Return to documentation for file (src/desert_classes/TcpDaemon.h)

/****************************************************************************
 * Copyright (C) 2024 Davide Costa                                          *
 *                                                                          *
 * This file is part of RMW desert.                                         *
 *                                                                          *
 *   RMW desert is free software: you can redistribute it and/or modify it  *
 *   under the terms of the GNU General Public License as published by the  *
 *   Free Software Foundation, either version 3 of the License, or any      *
 *   later version.                                                         *
 *                                                                          *
 *   RMW desert is distributed in the hope that it will be useful,          *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for more details.                           *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with RMW desert.  If not, see <http://www.gnu.org/licenses/>.    *
 ****************************************************************************/

#ifndef TCP_DAEMON_H_
#define TCP_DAEMON_H_

#include <queue>
#include <vector>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <thread>
#include <chrono>

#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <unistd.h>

#include "rmw/error_handling.h"

#define MAX_PACKET_LENGTH 512

#define ADDRESS "127.0.0.1"

#define START_MARKER 0b10011001
#define END_MARKER   0b01010101
#define BYTE_MASK    0b11111111

class TcpDaemon
{
  public:
    TcpDaemon();

    bool init(int port);
    static std::vector<uint8_t> read_packet();
    static void enqueue_packet(std::vector<uint8_t> packet);


  private:
    static int _client_fd;
    static std::queue<std::vector<uint8_t>> _rx_packets;
    static std::queue<std::vector<uint8_t>> _tx_packets;

    void socket_rx_communication();
    void socket_tx_communication();

};

#endif