示例#1
0
int Peer::Init(network::Socket & sock, const TorrentInterfaceInternalPtr & torrent, PEER_ADD peer_add)
{
    m_nm->Socket_set_assoc(sock, shared_from_this());
    memset(&m_buf, 0, sizeof(network::buffer));
    m_torrent = torrent;
    m_nm = torrent->get_nm();
    m_g_cfg = torrent->get_cfg();
    m_bitfield = new unsigned char[m_torrent->get_bitfield_length()];
    memset(m_bitfield, 0, m_torrent->get_bitfield_length());
    m_sleep_time = 0;
    m_peer_choking= true;
    m_peer_interested = false;
    m_am_choking = false;//
    m_am_interested = false;//
    m_downloaded = 0;
    m_uploaded = 0;
    m_sock = sock;
    memcpy(&m_addr, &m_sock->m_peer, sizeof(sockaddr_in));
    get_peer_key(m_sock->m_peer, m_ip);//inet_ntoa(m_sock->m_peer.sin_addr);
    switch(peer_add)
    {
    case PEER_ADD_TRACKER:
        m_state = PEER_STATE_SEND_HANDSHAKE;
        break;
    case PEER_ADD_INCOMING:
        if (send_handshake() != ERR_NO_ERROR)
            goto_sleep();
        if (send_bitfield() != ERR_NO_ERROR)
            goto_sleep();
        m_state = PEER_STATE_GENERAL_READY;
        break;
    }
    return ERR_NO_ERROR;
}
示例#2
0
void Peer::event_sock_error(network::Socket sock, int errno_)
{
#ifdef PEER_DEBUG
    logger::LOGGER() << "Peer " << m_ip.c_str() << ": close connection";
#endif
    goto_sleep();

}
示例#3
0
void Peer::event_sock_ready2read(network::Socket sock)
{
    try
    {
        bool closed;
        size_t ret;
        ret = m_nm->Socket_recv(sock, m_buf.data + m_buf.length, BUFFER_SIZE - m_buf.length, closed);
        m_buf.length += ret;
        if (process_messages() != ERR_NO_ERROR)
        {
            goto_sleep();
            return;
        }
        if (closed)
            goto_sleep();
    }
    catch (Exception & e) {
        goto_sleep();
    }
}
示例#4
0
void Peer::event_sock_timeout(network::Socket sock)
{
#ifdef PEER_DEBUG
    logger::LOGGER() << "Peer " << m_ip.c_str() << ": timeout";
#endif
    try
    {
        goto_sleep();
        return;
    }
    catch (Exception & e) {
        return;
    }
}
示例#5
0
int
rf212_sleep(void)
{
	int status;
	/* Check whether we're already sleeping */
	if (!sleep_on) {
		//printf("\r\n goto sleep %d",rf233_get_channel());
		//delay_ms(1);
		sleep_on = 1;
		/* Turn off the Radio */
		status = rf212_off();
		/* Set the SLP_PIN to high */
		if(status == 0) {
			goto_sleep();
		}
	}
	
	return 0;
	
}
示例#6
0
/* switch the radio off */
int
off(void)
{ 
  #if NULLRDC_CONF_802154_AUTOACK_HW
  if(rf233_status() != STATE_RX_AACK_ON ) {
  #else
  if(rf233_status() != STATE_RX_ON) {
  #endif
    /* fail, we need the radio transceiver to be in this state */
    return -1;
  }

  /* turn off the radio transceiver */
  ENERGEST_OFF(ENERGEST_TYPE_LISTEN);
  RF233_COMMAND(TRXCMD_TRX_OFF);
  radio_is_on = 0;
  return 0;
}
/*---------------------------------------------------------------------------*/
/* Put the Radio in sleep mode */

int 
rf233_sleep(void)
{
	int status;
	/* Check whether we're already sleeping */
	if (!sleep_on) {
	//printf("\r\n goto sleep %d",rf233_get_channel());
	//delay_ms(1);
	sleep_on = 1;
	/* Turn off the Radio */
	status = rf233_off();
	/* Set the SLP_PIN to high */
	  if(status == 0) {
	    goto_sleep();
	  }
	}
	
	return 0;
	
}
示例#7
0
int Peer::clock()
{
    if (m_state == PEER_STATE_SEND_HANDSHAKE)
    {
        if (m_sock == NULL)
        {
            try
            {
                m_nm->Socket_add(m_addr, shared_from_this(), m_sock);
            }
            catch (...)
            {
                goto_sleep();
                return ERR_INTERNAL;
            }
        }
        if (send_handshake() != ERR_NO_ERROR)
            goto_sleep();
        if (send_bitfield() != ERR_NO_ERROR)
            goto_sleep();
        m_state = PEER_STATE_WAIT_HANDSHAKE;
    }
    if (!m_peer_choking)
    {
        if (m_state == PEER_STATE_GENERAL_READY )
            m_state = PEER_STATE_REQUEST_READY;
    }
    else if (m_state == PEER_STATE_GENERAL_READY || m_state == PEER_STATE_REQUEST_READY)
    {
        send_interested();
        m_state = PEER_STATE_WAIT_UNCHOKE;
    }
    //если находимся в нужном состоянии и очередь не пуста
    if ((m_state == PEER_STATE_GENERAL_READY || m_state == PEER_STATE_REQUEST_READY || m_state == PEER_STATE_WAIT_UNCHOKE) && !m_requests_queue.empty())
    {
        char block[BLOCK_LENGTH];
        uint32_t block_length;
        PIECE_INDEX piece_index;
        BLOCK_INDEX block_index;
        std::set<BLOCK_ID>::iterator iter = m_requests_queue.begin();
        get_piece_block_from_block_id(*iter, piece_index, block_index);
        //если удалось прочитать блок и отправить, удаляем индекс блока из очереди
        if (m_torrent->read_block(piece_index, block_index, block, block_length) == ERR_NO_ERROR &&
                send_piece(piece_index, BLOCK_LENGTH * block_index, block_length, block) == ERR_NO_ERROR)
        {
            m_requests_queue.erase(iter);
            m_torrent->inc_uploaded(block_length);
            m_uploaded += block_length;
            //logger::LOGGER() << "rx=%f tx=%f\n", get_rx_speed(), get_tx_speed());
        }
    }
    if (m_state == PEER_STATE_REQUEST_READY && m_requested_blocks.size() <= PEER_MAX_REQUEST_NUMBER && !m_blocks2request.empty())
    {
        std::set<BLOCK_ID>::iterator iter = m_blocks2request.begin();
        PIECE_INDEX piece;
        BLOCK_INDEX block;
        uint32_t length;
        get_piece_block_from_block_id(*iter, piece, block);
        m_torrent->get_block_length_by_index(piece, block, length);
        if (send_request(piece, block, length) == ERR_NO_ERROR)
        {
            m_requested_blocks.insert(*iter);
            m_blocks2request.erase(iter);
        }
    }
    return ERR_NO_ERROR;
}
示例#8
0
文件: dice.c 项目: rblack42/RRB.AVR
void loop(void)
{
     unsigned char face, j, led_bits;

     if (sleepy_counter > (uint16_t)SLEEPY_COUNT)
     {
	  goto_sleep();
	  finish_roll = 0;
     }

     if (!button_state || !finish_roll)
	  // Button is being pressed; return
	  return;

     // Our randomness is the current value of a 16bit counter tied to
     // the CPU clock / 256 + an 8bit counter tied to the independent
     // watchdog timer oscillator all modulo 6
     face = (result_counter + wdt_counter) % N_FACES;

     // Run the animation for a tad longer
     sleepy_counter = 0;
     delay_by_tenths(3);

     sleepy_counter = 0;
     pattern_fade   = 0;
     old_leds_on    = 0;
     older_leds_on  = 0;
     finish_roll    = 0;

#if defined(SHUFFLE_STUFF)
     // Show some die faces, slowing down as we approach the selected result, i
     for (j = 0; j < 3; j++)
     {
	  START_PWM(2);
	  leds_on = pgm_read_byte(&(sequences[0][shuffle[j]+PATTERN_OFFSET]));
	  delay_by_tenths(2+2*j);
     }
#endif

     // Flash the result on and off very quickly with quick fading
     led_bits = pgm_read_byte(&(sequences[0][face+PATTERN_OFFSET]));
     for (j = 0; j < 7; j++)
     {
	  START_PWM(3);
	  leds_on = (j & 1) ? led_bits : 0;
	  delay_by_tenths(2);
     }
     STOP_PWM();

     // And now display the result (TIMER1_COMPA_vect actually does the display)
     leds_on = led_bits;

#if defined(SHUFFLE_STUFF)
     // Shuffle the list of die faces
     shuffle_faces();
#endif

     // Select a new LED sequence to show wen the button is next pressed
     sequence_index = (result_counter + wdt_counter + face) % N_SEQUENCES;
     pattern_hold   = pgm_read_byte(&(sequences[sequence_index][HOLD_OFFSET]));
     pattern_fade   = pgm_read_byte(&(sequences[sequence_index][FADE_OFFSET]));
     pattern_index  = PATTERN_OFFSET;

     // And now start the idle timer afresh
     sleepy_counter = 0;
}