Exemplo n.º 1
0
/*
 * sss_handle_receive()
 * 
 * This routine is called whenever there is a sss connection established and
 * the socket assocaited with that connection has incoming data. We will first
 * look for a newline "\n" character to see if the user has entered something 
 * and pressed 'return'. If there is no newline in the buffer, we'll attempt
 * to receive data from the listening socket until there is.
 * 
 * The connection will remain open until the user enters "Q\n" or "q\n", as
 * deterimined by repeatedly calling recv(), and once a newline is found, 
 * calling sss_exec_command(), which will determine whether the quit 
 * command was received.
 * 
 * Finally, each time we receive data we must manage our receive-side buffer.
 * New data is received from the sss socket onto the head of the buffer,
 * and popped off from the beginning of the buffer with the 
 * sss_exec_command() routine. Aside from these, we must move incoming
 * (un-processed) data to buffer start as appropriate and keep track of 
 * associated pointers.
 */
void sss_handle_receive(SSSConn* conn)
{
  int data_used = 0, rx_code = 0;
  char *lf_addr; 
  
  conn->rx_rd_pos = conn->rx_buffer;
  conn->rx_wr_pos = conn->rx_buffer;
  
  printf("[sss_handle_receive] processing RX data\n");
  
  while(conn->state != CLOSE)
  {
    /* Find the Carriage return which marks the end of the header */
    lf_addr = strchr((const char*)conn->rx_buffer, '\n');
      
    if(lf_addr)
    {
      /* go off and do whatever the user wanted us to do */
      sss_exec_command(conn);
    }
    /* No newline received? Then ask the socket for data */
    else
    {
      rx_code = recv(conn->fd, (char*)conn->rx_wr_pos, 
        SSS_RX_BUF_SIZE - (conn->rx_wr_pos - conn->rx_buffer) -1, 0);
          
     if(rx_code > 0)
      {
        conn->rx_wr_pos += rx_code;
        
        /* Zero terminate so we can use string functions */
        *(conn->rx_wr_pos+1) = 0;
      }
    }

    /* 
     * When the quit command is received, update our connection state so that
     * we can exit the while() loop and close the connection
     */
    conn->state = conn->close ? CLOSE : READY;

    /* Manage buffer */
    data_used = conn->rx_rd_pos - conn->rx_buffer;
    memmove(conn->rx_buffer, conn->rx_rd_pos, 
       conn->rx_wr_pos - conn->rx_rd_pos);
    conn->rx_rd_pos = conn->rx_buffer;
    conn->rx_wr_pos -= data_used;
    memset(conn->rx_wr_pos, 0, data_used);
  }

  printf("[sss_handle_receive] closing connection\n");
  close(conn->fd);
  sss_reset_connection(conn);
  
  return;
}
Exemplo n.º 2
0
/*
 * sss_handle_receive()
 * 
 * This routine is called whenever there is a sss connection established and
 * the socket assocaited with that connection has incoming data. We will first
 * look for a newline "\n" character to see if the user has entered something 
 * and pressed 'return'. If there is no newline in the buffer, we'll attempt
 * to receive data from the listening socket until there is.
 * 
 * The connection will remain open until the user enters "Q\n" or "q\n", as
 * deterimined by repeatedly calling recv(), and once a newline is found, 
 * calling sss_exec_command(), which will determine whether the quit 
 * command was received.
 * 
 * Finally, each time we receive data we must manage our receive-side buffer.
 * New data is received from the sss socket onto the head of the buffer,
 * and popped off from the beginning of the buffer with the 
 * sss_exec_command() routine. Aside from these, we must move incoming
 * (un-processed) data to buffer start as appropriate and keep track of 
 * associated pointers.
 */
void sss_handle_receive(SSSConn* conn)
{
    // Maxime's version
    int data_used = 0, n_bytes_received = 0;
    //INT8U *lf_addr; 
    conn->rx_rd_pos = conn->rx_buffer;
    conn->rx_wr_pos = conn->rx_buffer;
      
    printf("[sss_handle_receive] processing RX data\n");
        
    while(conn->state != CLOSE)
    {
        //Receiving bytes
        #if DEBUG_CODE
            printf("\nNumber of bytes we are looking for: %li\n",
            SSS_RX_BUF_SIZE - (conn->rx_wr_pos - conn->rx_buffer) -1);
        #endif
        n_bytes_received = recv(conn->fd, conn->rx_wr_pos, 
        SSS_RX_BUF_SIZE - (conn->rx_wr_pos - conn->rx_buffer) -1, 0);
          
        if(n_bytes_received > 0)
        {
            conn->rx_wr_pos += n_bytes_received;
            // Zero terminate so we can use string functions
            *(conn->rx_wr_pos+1) = 0; 
        }
        #if DEBUG_CODE
            printf("Data received addr: %p \n",conn->rx_wr_pos-n_bytes_received);  
            printf("rX buffer addr: %p \n",conn->rx_buffer);
        #endif    
        /* go off and do whatever the user wanted us to do */
        sss_exec_command(conn);
        #if DEBUG_CODE
            printf("Command executed\n");
        #endif
         
        /* 
        * When the quit command is received, update our connection state so that
        * we can exit the while() loop and close the connection
        */
        conn->state = conn->close ? CLOSE : READY;
        #if DEBUG_CODE
            printf("Command executed1\n");
        #endif
        /* Manage buffer */
        data_used = conn->rx_rd_pos - conn->rx_buffer;
        #if DEBUG_CODE
            printf("Command executed2: wr_pos %p rd_pos %p\n",conn->rx_wr_pos, conn->rx_rd_pos);
            printf("Data used %i\n",data_used);
        #endif
        memmove(conn->rx_buffer, conn->rx_rd_pos, 
           conn->rx_wr_pos - conn->rx_rd_pos);
        #if DEBUG_CODE
            printf("Command executed3\n");
        #endif
        conn->rx_rd_pos = conn->rx_buffer;
        #if DEBUG_CODE
            printf("Command executed4\n");
        #endif
        conn->rx_wr_pos -= data_used;
        #if DEBUG_CODE
            printf("Command executed5\n");
        #endif
        //memset(conn->rx_wr_pos, 0, data_used);
        #if DEBUG_CODE
            printf("Command executed6\n");
        #endif
    }
    printf("[sss_handle_receive] closing connection\n");
    close(conn->fd);
    sss_reset_connection(conn);
    return;
}