Skip to content
Extraits de code Groupes Projets
sender_utils.h 5,6 ko
Newer Older
  • Learn to ignore specific revisions
  • #ifndef __SENDER_UTILS_H_
    #define __SENDER_UTILS_H_
    
    
    #include <fcntl.h>
    
    #include <poll.h>
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <stdbool.h>
    #include <stdint.h>
    
    #include <sys/types.h>
    #include <sys/socket.h>
    
    #include <sys/time.h>
    
    #include <unistd.h>
    
    #include "utils.h"
    
    #include "packet_interface.h"
    
    
    
    #define SEQNUM_RANGE            256
    
    #define TIMER_LIMIT             2000   // It's in milli seconds, in this network, the latency to send is = [0, 2000ms]
    
    #define SENDER_INACTIVE_TIMEOUT 30000  // Only waits 30sec (it's in milliseconds) for the ACK of the CLOSING_PKT (the very last sended pkt)
    
    #define WINDOW_SIZE             31
    #define OUT_OFF_WINDOW          255
    
    // It is used to identify what kind of PTYPE_DATA pkt was sent lately
    // /!\ It doesn't trace the sended FEC !
    
    Samuel de Meester de Ravestein's avatar
    Samuel de Meester de Ravestein a validé
    typedef enum {
    	RANDOM_PKT    = 0,
    	LAST_DATA_PKT = 1,
    
    	CLOSING_PKT   = 2
    
    Samuel de Meester de Ravestein's avatar
    Samuel de Meester de Ravestein a validé
    } last_sended_pkt_t;
    
    
    /** 
     *  This structure represents the state of the sender at each moment
     *  It corresponds to some state of a FSM                            */
    
    typedef struct state {
    
        uint8_t r_window_size; // receiver buffer space
    
        uint8_t s_window_size; // sender (our) buffer space
    
        struct timeval timers[WINDOW_SIZE];            // timeval struct corresponding to the last time the pkt was sended
        struct timeval timers_first_send[WINDOW_SIZE]; // timeval struct corresponding to the first time the pkt was sended (only used for the stats)
    
        pkt_t *buffer[WINDOW_SIZE];  // When the buffer fields are not used, they MUST be se to NULL
    
        uint8_t head;  // place last element insert +1 in the buffer (free place)  | Head and tail are used to know
        uint8_t tail;  // place oldest element insert in the buffer                | the start and end of the sender window (of the sended pkt)
    
        uint8_t next_seqnum;
    
        uint8_t map_seqnum_to_buffer_place[SEQNUM_RANGE]; // Default value is: OUT_OFF_WINDOW
    
        last_sended_pkt_t last_pkt_sent;  // Can either be: RANDOM_PKT, LAST_DATA_PKT or CLOSING_PKT
    
        bool fec_enabled;
    
        pkt_t *FEC;      // The pkt FEC in construction
    
    Samuel de Meester de Ravestein's avatar
    Samuel de Meester de Ravestein a validé
        uint8_t FEC_nbr; // The number of PTYPE_DATA stacked on FEC
    
        transfer_stats_t *stats;
    
     * @brief Creation of the UNIQUE structure representing the sender state.
    
     * @param fec_enabled : bool: variable telling whether the FEC are enabled or not
     * @return sender_state_t* : The structure representing the state of the sender.
    
    sender_state_t *state_new(bool fec_enabled);
    
    /**
     * @brief Deletion if the structure representing the sender state.
     * 
     * @param state : The variable representing the sender (state).
     */
    
    void state_del(sender_state_t *state);
    
     * @brief Checking if the sender is allowed to send a NEW pkt
    
     * 
     * @param state : The variable representing the sender (state).
    
     * @return bool : 
     *          - true : if there are enough room in the buffers and some pkt still need to be sent
     *          - false: if there are no room in the buffer 
     *                   or if we're waiting for the last ACK of the DATATYPE
     *                   or if all pkt have been sent 
    
    bool can_send(sender_state_t *state);
    
     * @brief Cautious this function is used for sending and sending back PTYPE_DATA pkt !
    
     * 
     * @param state     : The variable representing the sender (state).
     * @param pkt       : The pkt to be sent.
     * @param position  : The place in the buffer of the pkt.
     * @param socket_fd : The socket on which the pkt need to be sent.
     * @return int      : 0 if no error, -1 otherwise.
     */
    
    int send_pkt(sender_state_t *state, pkt_t *pkt, uint8_t position, int socket_fd);
    
    
     * @brief The function handle ACK and NACK pkt.
    
     * @param state     : The variable representing the sender (state).
     * @param socket_fd : The socket on which pkt are sent.
     * @return int      : 0 if no error, -1 otherwise
    
    int handle_returning_ack_nack(sender_state_t *state, int socket_fd);
    
    
     * @brief The function checks if the oldest not acked pkt has exceeded the timer (TIMER_LIMIT).
     *        If it has exceeded the timer then it is sent back.
    
     * @param state     : The variable representing the sender (state).
     * @param socket_fd : The socket on which pkt are sent.
     * @return int      : 0 if no error, -1 otherwise
    
    int checking_timer(sender_state_t *state, int socket_fd);
    
     * @brief Determines wheter the read file offset is at the end of the file
    
     * @param sending_fd : The file descriptor of the file you want to know if you're at the end.
     * @return true  : The offset of sending_fd is at the end of the file
     * @return false : The offset of sending_file is not at the end of the file
    
     */
    bool is_it_EOF(int sending_fd);
    
    /**
    
     * @brief It performs the XOR operation between state->FEC and pkt and stores the result in state->FEC
    
     * @param state : The variable representing the sender (state).
    
     * @param pkt 
     */
    void construct_FEC(sender_state_t *state, pkt_t *pkt);
    
    /**
    
     * @brief It sends the FEC pkt contains in state->FEC.
    
     * @param state     : The variable representing the sender (state).
     * @param socket_fd : The socket on which pkt are sent.
     * @return int      : 0 if no error, -1 otherwise
    
     */
    int send_FEC(sender_state_t *state, int socket_fd);
    
    
     * @brief When this function is called, the sender MUST be allowed to send a pkt.
     *        It sends the next pkt and update (if necessary) the variable 'last_pkt_sent' of state.
    
     * @param state      : The variable representing the sender (state).
     * @param sending_fd : The file descriptor of the file to be send
     * @param socket_fd  : The socket on which pkt are sent.
     * @return int       : 0 if no error, -1 otherwise
    
    int read_and_send(sender_state_t *state, int sending_fd, int socket_fd);