예제 #1
0
파일: ni_msg.c 프로젝트: NhaTrang/lon4linux
NI_Code ni_reset(void)
{
    LDVCode ldv_error = LDV_OK;
    NI_Code ni_error  = NI_OK;
    time_t  deadline;

    init_pending_list( );       /* throw away any unprocessed messages */

    if (verbose_flag)
        if (verbose_flag) fprintf(LogFilePtr, "\nReset network interface.\n" );
	
//	sleep(1);  // Karl's test 17/07/00
//	fprintf(LogFilePtr,"TEST:  in ni_reset()  sending_immediate niRESET \n");
    ni_error = ni_send_immediate( niRESET );
    if( ni_error != NI_OK )
        return ni_error;

//	sleep(1); // Karl's test 17/07/00
    deadline = time( NULL ) + niWAIT_TIME;

//	fprintf(LogFilePtr,"TEST:  in ni_reset()  waiting for response from niRESET \n");

    do {
        /* Loop until an input is received from the network interface. */
        do {
            /* Check for timeout. */
            if ( time( NULL ) > deadline )
                /* No response from network interface. */
                return(NI_TIMEOUT);

            /* Check for network interface input. */
            ldv_error = ldv_read( ni_handle, &msg_in, sizeof( msg_in ) );
	      //  sleep(1);
        } while (ldv_error == LDV_NO_MSG_AVAIL
                 || ldv_error == LDV_DEVICE_BUSY);

        if (verbose_flag) {
            fprintf(LogFilePtr,"\nReceived a LONTALK message, error = %d\n", ldv_error);
            if (ldv_error == LDV_OK)
                ni_msg_display(&msg_in);
        }

        if (ldv_error != LDV_OK) {
            /* Network driver returned error, display it, and return error. */
            ni_ldv_error_display("IN ni_reset(). Network driver error on reset", ldv_error);
            return(NI_DRIVER_ERROR);
        }

        if (msg_in.msg_hdr.exp.cmpl_code == MSG_FAILS)
            /* Failure completion code. */
            return(NI_RESET_FAILS);
    } while( msg_in.ni_hdr.noq.cmd != niRESET );

    return ni_send_immediate( niFLUSH_CANCEL );  /* response */
}
예제 #2
0
void send_receiver(int index, int packet_size, struct timeval deadline)
{
  if (rcvdb[index].pending.is_pending)
  {
    pending_write next;
    next.size = packet_size;
    next.delta = update_write_time(index, deadline);
    push_pending_write(index, next);
  }
  else
  {
    int error = send_with_reconnect(index, packet_size);
    if (error > 0)
    {
      // This means that there was a successful write, but
      // incomplete. So we need to set up a pending write.
      init_pending_list(index, error, deadline);
    }
  }

/*
  int sockfd;
  int error = 1, retry=0;
  struct in_addr addr;

  sockfd = rcvdb[index].sockfd;

  if (packet_size <= 0) {
    packet_size = 1;
  }
  if (rcvdb[index].pending > 0) {
    add_pending(index, packet_size);
    return;
  }
  if (packet_size > MAX_PAYLOAD_SIZE){
    add_pending(index, packet_size - MAX_PAYLOAD_SIZE);
    packet_size = MAX_PAYLOAD_SIZE;
  }

  error = send(sockfd, random_buffer, packet_size, MSG_DONTWAIT);
  logWrite(PEER_WRITE, NULL, "Wrote %d bytes", error);
  // Handle failed connection
  while (error == -1 && errno == ECONNRESET && retry < 3) {
    reconnect_receiver(index);
    sockfd= rcvdb[index].sockfd;
    error = send(sockfd, random_buffer, packet_size, MSG_DONTWAIT);
    logWrite(PEER_WRITE, NULL, "Wrote %d reconnected bytes", error);
    retry++;
  }
  //if still disconnected, reset
  if (error == -1 && errno == ECONNRESET) {
    remove_index(index, &write_fds);
    addr.s_addr = rcvdb[index].ip;
    printf("Error: send_receiver() - failed send to %s three times. \n", inet_ntoa(addr));
  }
  else if (error == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
    add_pending(index, packet_size);
  }
  else if (error == -1) {
    perror("send_receiver: send");
    clean_exit(1);
  }
  else {
    total_size += error;
//    printf("Total: %d, Pending: %d\n", total_size, rcvdb[index].pending);
    add_pending(index, packet_size - error);
  }
*/
}