Example #1
0
void Connection::handle_read(const boost::system::error_code& e, size_t bytes_transferred,
                                    boost::tuple<ErrorHandler,Handler> handler, boost::shared_ptr<Connection> conn)

{
    try
    {
        if (e)
        {
            boost::get<0>(handler)(e);
        }
        else
        {
            int originalSize = inbound_data_.size();
            inbound_data_.resize(originalSize + bytes_transferred);
            int buffSize = originalSize + bytes_transferred;
            std::cout << originalSize << "+" << bytes_transferred << "=" << buffSize << std::endl;
            std::memcpy(&inbound_data_[originalSize],&inbound_buffer_[0],bytes_transferred);
            while(buffSize>=HEADER_SIZE) { // this means we have at least a header in our buffer
                int sz = inbound_data_[3];
                sz = (sz << 8) | inbound_data_[2];
                sz = (sz << 8) | inbound_data_[1];
                sz = (sz << 8) | inbound_data_[0];
                if(buffSize>=HEADER_SIZE+sz) { // we have a full message to parse!
                    std::string archive_data(&inbound_data_[HEADER_SIZE], sz);
                    Json::Value value;
                    std::cout << archive_data << std::endl;
                    if(reader.parse(archive_data,value))
                    {
                        boost::get<1>(handler)(value);
                    }
                    if(HEADER_SIZE+sz<buffSize) {
                        std::copy(&inbound_data_[HEADER_SIZE+sz],&inbound_data_[buffSize],&inbound_data_[0]);
                        buffSize-=HEADER_SIZE+sz;
                    } else {
                        inbound_data_.clear();
                        buffSize = 0;
                    }
                    inbound_buffer_.resize(4);
                } else {
                    inbound_buffer_.resize(sz);
                }
            }
            std::cout << "async read again: " << inbound_buffer_.size() << std::endl;
            boost::asio::async_read(socket_, boost::asio::buffer(inbound_buffer_),
                            boost::bind(&Connection::handle_read,
                                        this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred,
                                        handler,conn));
        }
    }
    catch(std::exception& e)
    {
        std::cout << "Connection::handle_read_header: " << e.what() << std::endl;
    }
}
void Udp_graph_communicator::handle_receive_from(const boost::system::error_code& error, size_t bytes_recvd)
{
    if (!error)
    {
        try
        {
            if (printDebug) 
				std::cout<<"ricezione: "<<std::endl;

            std::string archive_data(&inbound_data_[header_length], MAX_PACKET_LENGTH-header_length);
            std::istringstream archive_stream(archive_data);
            boost::archive::text_iarchive archive(archive_stream);
            archive >> input_map_tp;
        }
        catch (std::exception& e)
        {
            // Unable to decode data.
            boost::system::error_code error(boost::asio::error::invalid_argument);
            std::cout.write(inbound_data_, bytes_recvd);
            std::cout << std::endl;
// 			ERR("pacchetto sbagliato: %s",inbound_data_);
            throw "Problema nella ricezione di un pacchetto";
        }
        if (printDebug) 
			std::cout<<input_map_tp<<std::endl;
//         if (!mutex_is_mine)
// 		{
        mutex.lock();
// 			mutex_is_mine=true;
// 		}

        //Copiamo tutto, sarĂ  compito del sender assicurarsi di non mandare robaccia
        for (graph_packet::iterator it=input_map_tp.begin();it!=input_map_tp.end();++it)
        {
			//se non ho l'informazione oppure ce l'ho ma e' vecchia la cambio
            if (!tp->count(it->first) || tp->at(it->first).timestamp<it->second.timestamp) 
            {
                (*tp)[it->first]=it->second;
            }
        }
// 		if (!socket_.available())
// 		{
// 			mutex_is_mine=false;
        mutex.unlock();
// 		}

        socket_.async_receive_from(
            boost::asio::buffer(inbound_data_, MAX_PACKET_LENGTH), listen_endpoint_,
            boost::bind(&Udp_graph_communicator::handle_receive_from, this,
                        boost::asio::placeholders::error,
                        boost::asio::placeholders::bytes_transferred));
    }
}