src/mrt.c
// mrt_send: send a chunk of data over a given connection (may temporarily block execution if the receiver is busy/full)
// size - bytes to send
// buf - data pointer
send a chunk of data over a given connection (may temporarily block execution if the receiver is busy/full)
size - bytes to send
buf - data pointer
send a chunk of data over a given connection ( may temporarily block execution if the receiver is busy / full ) size - bytes to send buf - data pointer
int mrt_send(mrt_connection * conn, char * buf, int size) {
  int i;
  char * entr;
  int tot_pkts = size/MSS + (size%MSS?1:0);
  timer t_send;
  int timeout_send = 0;
  if (!conn->active) return 1;
  entr = buf;
reset:
  while(conn->active && conn->base - global_offset + 1 != tot_pkts) {
      timer_set(&t_send);
      if (MRT_TEST) printf("I am waiting...\n");
      for (i = conn->base + 1 - global_offset; i < tot_pkts; i++) {
        while(conn->active && i - conn->base  + global_offset > conn->rcvwnd) {
          sleep(1);
          if (timer_diff(&t_send) >= SEND_TIMEOUT) goto reset;
        }
        send_data_pkt(conn->fd, &conn->addr, i + global_offset + conn->offset, 0, buf + i * MSS, i<tot_pkts - 1?MSS:(size%MSS?size%MSS:MSS));
      }
      if (timer_diff(&t_send) >= SEND_TIMEOUT) continue;
      nanosleep((const struct timespec[]){0, 1e8L}, NULL);
  }
  global_offset += tot_pkts;
  return 0;
}
int mrt_send ( mrt_connection * conn , char * buf , int size ) { int i ; char * entr ; int tot_pkts = size / MSS + ( size % MSS ? 1 : 0 ) ; timer t_send ; int timeout_send = 0 ; if ( ! conn -> active ) return 1 ; entr = buf ; reset : while ( conn -> active && conn -> base - global_offset + 1 != tot_pkts ) { timer_set ( & t_send ) ; if ( MRT_TEST ) printf ( " \n " ) ; for ( i = conn -> base + 1 - global_offset ; i < tot_pkts ; i ++ ) { while ( conn -> active && i - conn -> base + global_offset > conn -> rcvwnd ) { sleep ( 1 ) ; if ( timer_diff ( & t_send ) >= SEND_TIMEOUT ) goto reset ; } send_data_pkt ( conn -> fd , & conn -> addr , i + global_offset + conn -> offset , 0 , buf + i * MSS , i < tot_pkts - 1 ? MSS : ( size % MSS ? size % MSS : MSS ) ) ; } if ( timer_diff ( & t_send ) >= SEND_TIMEOUT ) continue ; nanosleep ( ( const struct timespec [ ] ) { 0 , 1e8L } , NULL ) ; } global_offset += tot_pkts ; return 0 ; }
mrt_send: send a chunk of data over a given connection (may temporarily block execution if the receiver is busy/full)
size - bytes to send
buf - data pointer
mrt_send : send a chunk of data over a given connection ( may temporarily block execution if the receiver is busy / full ) size - bytes to send buf - data pointer
// test cases