예제 #1
0
 void start_receive() {
     socket_.async_receive_from(
             asio::buffer(recv_buffer_), remote_endpoint_,
             boost::bind(&Network::handle_receive, this,
                 asio::placeholders::error, 
                 asio::placeholders::bytes_transferred));
             
 }
예제 #2
0
void receiving()
{

	mi_socket_server.async_receive_from(
	        boost::asio::buffer(recv_buffer), remote_endpoint,
	        boost::bind(&mi_udp_server::handler, this,
	        boost::asio::placeholders::error,
	        boost::asio::placeholders::bytes_transferred));

}
예제 #3
0
	void thread_fun()
	{
		char buffer[2000];

		for (;;)
		{
			error_code ec;
			udp::endpoint from;
			size_t bytes_transferred;
			bool done = false;
			m_socket.async_receive_from(
				boost::asio::buffer(buffer, sizeof(buffer)), from, 0
				, boost::bind(&incoming_packet, _1, _2, &bytes_transferred, &ec, &done));
			while (!done)
			{
				m_ios.poll_one();
				m_ios.reset();
			}

			if (ec == boost::asio::error::operation_aborted
				|| ec == boost::asio::error::bad_descriptor) return;

			if (ec)
			{
				fprintf(stderr, "Error receiving on DHT socket: %s\n", ec.message().c_str());
				return;
			}

			try
			{
				entry msg = bdecode(buffer, buffer + bytes_transferred);

#if defined TORRENT_DEBUG && TORRENT_USE_IOSTREAM
				std::cerr << msg << std::endl;
#endif
				++m_dht_requests;
			}
			catch (std::exception& e)
			{
				fprintf(stderr, "failed to decode DHT message: %s\n", e.what());
			}
		}
	}
예제 #4
0
	void thread_fun()
	{
		char buffer[2000];

		error_code ec;
		udp::endpoint from;
		m_socket.async_receive_from(
			boost::asio::buffer(buffer, int(sizeof(buffer))), from, 0
			, std::bind(&udp_tracker::on_udp_receive, this, _1, _2, &from, &buffer[0], int(sizeof(buffer))));

		m_ios.run(ec);

		if (ec)
		{
			std::printf("UDP Error running UDP tracker service: %s\n", ec.message().c_str());
			return;
		}

		std::printf("UDP exiting UDP tracker thread\n");
	}
예제 #5
0
	void on_udp_receive(error_code const& ec, size_t bytes_transferred, udp::endpoint* from, char* buffer, int size)
	{
		if (ec)
		{
			std::printf("%s: UDP tracker, read failed: %s\n", time_now_string(), ec.message().c_str());
			return;
		}

		if (bytes_transferred < 16)
		{
			std::printf("%s: UDP message too short (from: %s)\n", time_now_string(), print_endpoint(*from).c_str());
			return;
		}

		if (m_abort)
		{
			return;
		}

		std::printf("%s: UDP message %d bytes\n", time_now_string(), int(bytes_transferred));

		char* ptr = buffer;
		detail::read_uint64(ptr);
		std::uint32_t const action = detail::read_uint32(ptr);
		std::uint32_t const transaction_id = detail::read_uint32(ptr);

		error_code e;

		switch (action)
		{
			case 0: // connect

				std::printf("%s: UDP connect from %s\n", time_now_string()
					, print_endpoint(*from).c_str());
				ptr = buffer;
				detail::write_uint32(0, ptr); // action = connect
				detail::write_uint32(transaction_id, ptr); // transaction_id
				detail::write_uint64(10, ptr); // connection_id
				m_socket.send_to(boost::asio::buffer(buffer, 16), *from, 0, e);
				if (e) std::printf("%s: UDP send_to failed. ERROR: %s\n"
					, time_now_string(), e.message().c_str());
				else std::printf("%s: UDP sent response to: %s\n"
					, time_now_string(), print_endpoint(*from).c_str());
				break;

			case 1: // announce

				++m_udp_announces;
				std::printf("%s: UDP announce [%d]\n", time_now_string()
					, int(m_udp_announces));
				ptr = buffer;
				detail::write_uint32(1, ptr); // action = announce
				detail::write_uint32(transaction_id, ptr); // transaction_id
				detail::write_uint32(1800, ptr); // interval
				detail::write_uint32(1, ptr); // incomplete
				detail::write_uint32(1, ptr); // complete
				// 0 peers
				m_socket.send_to(boost::asio::buffer(buffer, 20), *from, 0, e);
				if (e) std::printf("%s: UDP send_to failed. ERROR: %s\n"
					, time_now_string(), e.message().c_str());
				else std::printf("%s: UDP sent response to: %s\n"
					, time_now_string(), print_endpoint(*from).c_str());
				break;
			case 2:
				// ignore scrapes
				std::printf("%s: UDP scrape (ignored)\n", time_now_string());
				break;
			default:
				std::printf("%s: UDP unknown message: %d\n", time_now_string()
					, action);
				break;
		}

		m_socket.async_receive_from(
			boost::asio::buffer(buffer, size), *from, 0
			, std::bind(&udp_tracker::on_udp_receive, this, _1, _2, from, buffer, size));
	}
예제 #6
0
파일: main.cpp 프로젝트: Lalaland/Bullet
void handler(const boost::system::error_code& error, std::size_t /*bytes_transferred*/, udp::socket &sock, udp::endpoint *end)
{
   switch(reinterpret_cast<t_Packet *>(ReceiveBuffer)->type)
   {
      case 0:
      {
         cout<<"I got a default packet"<<endl;
      }
         break;

      case 1: 
      {
         cout<<"Got a ping packet"<<endl;
         cout<<"Its from "<<end->address().to_string()<<endl; 
         
         t_pingPacket pingPacket(*reinterpret_cast<t_pingPacket *>(ReceiveBuffer));
         
         cout<<"The time difference was an amazing "<<(boost::posix_time::microsec_clock::universal_time().time_of_day().total_microseconds() - pingPacket.time)<<endl;
         cout<<"Sending it back...\n\n"<<endl;
         sock.send_to(boost::asio::buffer(ReceiveBuffer),*end);
      }
         break;
      
      case 2:
      {
         cout<<"Someone from "<<end->address().to_string()<<" is trying to join"<<endl;
         
         t_connectPacket connectPacket;
         
         bm::right_map::const_iterator lol = table.right.find(*end);
         if (table.right.end() == lol)
         {
            cout<<"They are not already connected"<<endl;

            connectPacket.id = CurId;
            table.insert(bm::value_type(CurId++,*end));
         }

         else
         {
            cout<<"They connected before"<<endl;

            connectPacket.id=lol->second;
         }

         sock.send_to(boost::asio::buffer(&connectPacket,sizeof(connectPacket)),*end);
         cout<<"We gave them an id of "<<(connectPacket.id)<<endl<<endl;
      }
         break;
      
      case 3:
      {
         cout<<"Someone from "<<end->address().to_string()<<" with id "<<table.right.find(*end)->second<<" wants the world"<<endl;

         t_worldPacket worldPacket(*reinterpret_cast<t_worldPacket *>(ReceiveBuffer));
         worldPacket.numOfObjects = 6;

         cout<<"Telling them the world has five objects"<<endl<<endl;
         sock.send_to(boost::asio::buffer(&worldPacket,sizeof(worldPacket)),*end);
      }
         break;

      case 4:
      {
         cout<<"Someone from "<<end->address().to_string()<<" with id "<<table.right.find(*end)->second<<" wants some objects"<<endl;

         t_objectPacket objectPacket(*reinterpret_cast<t_objectPacket *>(ReceiveBuffer));
         
         for (int i = 0;i<objectPacket.numOfObjects;i++)
         {
            cout<<"He wants the object "<<objectPacket.numbers[i]<<endl;
         }
         
         cout<<"\nMaking new packet"<<endl;

         t_objectPacket *newObjectPacket = reinterpret_cast<t_objectPacket *>(malloc(sizeof(t_objectPacket) + objectPacket.numOfObjects * sizeof(t_objectData)));
         memcpy(newObjectPacket,&objectPacket,sizeof(objectPacket));
      
         cout<<"Created new packet of right size(hopefully)"<<endl;
         cout<<"Setting them all to zero, so I can be lazy"<<endl; 

         //memset(newObjectPacket->objectData,0,newObjectPacket->numOfObjects * sizeof(t_objectData));

         cout<<"Setting first to name ";
         strcpy(newObjectPacket->objectData[0].name,"wow");
         cout<<newObjectPacket->objectData[0].name<<endl;

         cout<<"Finially sending the stupid thing over, it has a size of "<<(sizeof(t_objectPacket) + objectPacket.numOfObjects * sizeof(t_objectData))<<endl<<endl;
         sock.send_to(boost::asio::buffer(newObjectPacket,sizeof(t_objectPacket) + objectPacket.numOfObjects * sizeof(t_objectData)),*end);


      }

      default:
         cout<<"Who knows what packet I got!!"<<endl;
   }  
   
   sock.async_receive_from(boost::asio::buffer(ReceiveBuffer),*end,boost::bind(handler,
      boost::asio::placeholders::error,
      boost::asio::placeholders::bytes_transferred,
      boost::ref(sock),
      end));
}