Exemple #1
0
 void node::on_received_message( const shared_ptr<peer_connection>& con, 
                                 const message& msg )
 {
    try {
       ilog( "type ${t}", ("t",msg.type) );
       switch ( (message_type)msg.type )
       {
          case message_type::signin:
             on_signin_request( con, msg.as<signin_request>() );
             break;
          case goodbye:
             on_goodbye( con, msg.as<goodbye_message>() );
             break;
          case peer_request:
             break;
          case peer_response:
             break;
          case message_type::block:
             ilog( "on block message  ${block}", ("block",msg.as<block_message>().block) );
             on_block_message( con, msg.as<block_message>() );
             break;
          case keep_alive:
             ilog( "keep alive ${peer}", ("peer",con->remote_endpoint()) );
             break;
          default:
             FC_ASSERT( !"unknown message type", "",("type",msg.type) );
       }
    } 
    catch ( const fc::exception& e )
    {
       ilog( "exception processing message ${m}", ("m",e.to_detail_string() ) );
       con->send_message( goodbye_message(e) ); 
       con->_socket.close();
    }
 } // on_received_message
   void peer_connection::read_loop()
   {
      ilog( "read loop" );
      try 
      {
         auto one_time_key = fc::ecc::private_key::generate();
         fc::ecc::public_key pub = one_time_key.get_public_key();
         auto s = pub.serialize();

         _socket.write( (char*)&s, sizeof(s) );

         fc::ecc::public_key_data remote_one_time_key;
         _socket.read( (char*)&remote_one_time_key, sizeof(remote_one_time_key) );

         _shared_secret = one_time_key.get_shared_secret( remote_one_time_key );

         elog( "${ss}", ("ss",_shared_secret) );

         if( _send_queue.size() && !_send_queue_complete.valid() )
            _send_queue_complete = fc::async( [=](){ process_send_queue(); } );

         message next_message;
         next_message.data.resize( BTS_NETWORK_MAX_MESSAGE_SIZE );

         while( !_read_loop.canceled() )
         {
            // read a message
            _socket.read( (char*)&next_message.size, sizeof(next_message.size) );
            wlog( "                                                      read message of size ${s} ", ("s", next_message.size) );

            if( next_message.size > BTS_NETWORK_MAX_MESSAGE_SIZE )
            {
               send_message( goodbye_message( message_too_large() ) ); 
               _socket.close();
               FC_CAPTURE_AND_THROW( message_too_large, (next_message.size) );
            }

            _socket.read( (char*)&next_message.type, sizeof(next_message.type) );
            wlog( "                     read message of size ${s}   type ${t}", ("s", next_message.size)("t",int(next_message.type)) );
            _socket.read( next_message.data.data(), next_message.size );
            wlog( "read body of message" );

            received_message( shared_from_this(), next_message );
         }
      } 
      catch ( const fc::exception& e )
      {
         ilog( "closed: ${e}", ("e", e.to_detail_string()) );
         connection_closed( shared_from_this(), e );
         return;
      }
      ilog( "closed!" );
      connection_closed( shared_from_this(), optional<fc::exception>() );
   }
int main(void){
	//store the computed value of 2 to the power of 3
	int answer = 0;

	//call a function by using its name followed by ()
	//if the function expects any information (arguments), provide the arguments between () 
	welcome();	
	/*
	use the power function to compute 2 raised to the power of 3 by calling the power 
	function and passing the arguments in the correct order, that is: base, exponent
	the power function will return a result that use can assign to a variable using =
	*/
	answer = power(2,3);
	printf("Two raised to the power of three is: %d\n", answer);
	//call the goodbye message function and pass the value 15 as an argument
	goodbye_message(15);

	return 0;
}
int main(void){
	//a variable to store the result of the exponentiation
	int answer;

	//call a function by writing its name followed by ()
        //provide arguments between the () if the function expects them
        welcome_message();
        //eventually our program will do some stuff

	//call the power function with the correct number of arguments
	//assign (store the value) of the result in the "answer" variable
	//ex. 2 raised to the power of 3
	answer = power(2,3);
	printf("Two raised to the power of three is: %d\n", answer);

	//print our goodbye message 
	goodbye_message(5);
	
}