Esempio n. 1
0
void instance::write(execution_unit* ctx, buffer_type& buf,
                     header& hdr, payload_writer* pw) {
  CAF_LOG_TRACE(CAF_ARG(hdr));
  try {
    if (pw) {
      auto pos = buf.size();
      // write payload first (skip first 72 bytes and write header later)
      char placeholder[basp::header_size];
      buf.insert(buf.end(), std::begin(placeholder), std::end(placeholder));
      binary_serializer bs{ctx, buf};
      (*pw)(bs);
      auto plen = buf.size() - pos - basp::header_size;
      CAF_ASSERT(plen <= std::numeric_limits<uint32_t>::max());
      hdr.payload_len = static_cast<uint32_t>(plen);
      stream_serializer<charbuf> out{ctx, buf.data() + pos, basp::header_size};
      out << hdr;
    } else {
      binary_serializer bs{ctx, buf};
      bs << hdr;
    }
  }
  catch (std::exception& e) {
    CAF_LOG_ERROR(CAF_ARG(e.what()));
  }
}
Esempio n. 2
0
 void receive_1p( FUNC func_ )
 {
     if( connection_1p )
     {
         try
         {
             while( true )
             {
                 std::memset( buffer_.data(), '\0', buffer_.size() );
                 auto state_ = connection_1p->receive( buffer_.data(), buffer_.size(), 0 );
                 if( state_ != bluetooth::connection::socket_state::success )
                 {
                     break;
                 }
                 func_( this, buffer_ );
             }
         }
         catch( bluetooth_disconnect_exception const& )
         {
             connection_1p.reset();
             if( !listener_ )
             {
                 listener_ = std::make_shared<bluetooth::listener>();
             }
             subject_1p.notify( connection_state::disconnect );
         }
     }
 }
Esempio n. 3
0
std::string Base64Helper::Encode(const buffer_type& data) {
    auto encodedSize = data.size() * 4;
    encodedSize = (encodedSize + 3) / 3;
    encodedSize += 4 - (encodedSize % 4);   
    
    std::string text;
    text.resize(encodedSize);
    
    base64Encode(&text[0], data.data(), data.size());    
    
    return text;
}
Esempio n. 4
0
void async_udp::udp_listener::listen_handler_( buffer_type bt, const boost::system::error_code& error, const size_t bytes_received )
{
	if ( error )
	{
		static const int NO_ENOUGHT_BUFFER = 234;
		if ( error.value() == NO_ENOUGHT_BUFFER )
		{
			enlarge_buffer_( bt );
			register_listen_( bt, bytes_received );
		}
		return;
	}
	if ( bytes_received == bt->size() && (*bt)[ bytes_received - 1 ] != '\0' )
	{
		enlarge_buffer_( bt );
		register_listen_( bt, bytes_received );
	}
	else
	{
		{
			boost::mutex::scoped_lock lock( protect_messages_ );
			messages_.push_back( std::string( bt->c_str(), bytes_received ) );
		}
		register_listen_();
	}
}
Esempio n. 5
0
void multicast_communication::udp_listener::listen_handler_( buffer_type bt, const boost::system::error_code& error, const size_t bytes_received )
{
	if ( error )
	{
		static const int NO_ENOUGHT_BUFFER = 234;
		if ( error.value() == NO_ENOUGHT_BUFFER )
		{
			enlarge_buffer_( bt );
			register_listen_( bt, bytes_received );
		}
		return;
	}
	if ( bytes_received == bt->size() && (*bt)[ bytes_received - 1 ] != '\0' )
	{
		enlarge_buffer_( bt );
		register_listen_( bt, bytes_received );
	}
	else
	{
		{
			boost::mutex::scoped_lock lock( protect_messages_ );
			switch(data_type_)
			{
			case 'Q' :
				push_vector<messages::quote_message>(messages_quote, quote_parser.parse(std::string( bt->c_str(), bytes_received )));
				break;
			case 'T' :
				push_vector<messages::trade_message>(messages_trade, trade_parser.parse(std::string( bt->c_str(), bytes_received )));
				break;
			}
			
		}
		register_listen_();
	}
}
Esempio n. 6
0
	static http::request_data assemble(http::request_data const& data, buffer_type& header)
	{
		namespace spirit = boost::spirit;

		char const sp[] = " ";
		char const ver[] = "HTTP/";
		char const crlf[] = "\r\n";
		//! собираем start-line
		header.insert(header.end(), data.method.begin(), data.method.end());
		header.insert(header.end(), sp, sp + sizeof(sp) - 1);
		header.insert(header.end(), data.uri.begin(), data.uri.end());
		header.insert(header.end(), sp, sp + sizeof(sp) - 1);
		header.insert(header.end(), ver, ver + sizeof(ver) - 1);
		header.insert(header.end(), data.ver.begin(), data.ver.end());
		header.insert(header.end(), crlf, crlf + sizeof(crlf) - 1);
		//! собираем message-header в плоский буфер
		detail::assemble(data.hdrs, header);
		//! разбираем сформированнй буфер и создаем структуру пакета
		http::request_data req_data;
		http::request req(&req_data);
		char const* begin = &header[0];
		char const* end = &header[0] + header.size();
		spirit::parse_info<> info = spirit::parse(begin, end, req);
		return req_data;
	}
 void udp_listener::listen_handler_( buffer_type bt, const boost::system::error_code& error, const size_t bytes_received )
 {
     if ( error )
     {
         static const int NO_ENOUGHT_BUFFER = 234;
         if ( error.value() == NO_ENOUGHT_BUFFER )
         {
             enlarge_buffer_( bt );
             register_listen_( bt, bytes_received );
         }
         return;
     }
     if ( bytes_received == bt->size() && (*bt)[ bytes_received - 1 ] != '\0' )
     {
         enlarge_buffer_( bt );
         register_listen_( bt, bytes_received );
     }
     else
     {
         message_type data( bt->begin(), bt->begin() + bytes_received );
         message m;
         m.data = data;
         m.type_ = type_;
         func_(m);
         this->register_listen_( );
     }
 }
 /// @pre `n <= buf_.size()`
 static chunk_type get_chunk(buffer_type& buf, size_t n) {
   CAF_LOG_TRACE(CAF_ARG(buf) << CAF_ARG(n));
   chunk_type xs;
   if (!buf.empty() && n > 0) {
     xs.reserve(std::min(n, buf.size()));
     if (n < buf.size()) {
       auto first = buf.begin();
       auto last = first + static_cast<ptrdiff_t>(n);
       std::move(first, last, std::back_inserter(xs));
       buf.erase(first, last);
     } else {
       std::move(buf.begin(), buf.end(), std::back_inserter(xs));
       buf.clear();
     }
   }
   return xs;
 }
Esempio n. 9
0
std::string to_string(buffer_type buf){
    std::size_t length = 0; 
    while(length < buf.size())
    {
        if(buf[length] == '\0') break;
        length++;
    }
    return std::string(buf.data(), buf.data() + length);
}
Esempio n. 10
0
void Tourist::read(tcp::socket &socket, buffer_type &buffer, size_t n, rw_handler_method method) {
  if (n > buffer.size()) {
    LOG_ERROR << "Read " << n << " bytes into " << buffer.size() << "-byte buffer";
    return;
  }
  std::shared_ptr<Tourist> p_this = shared_from_this();
  boost::asio::async_read(socket, boost::asio::buffer(buffer, n),
      [p_this, method, &socket, &buffer](const boost::system::error_code &error, size_t n) {
          LOG_VERBOSE << "[" << p_this->id_ << "] " << "Read " << n << " bytes";
          IF_LOG(plog::verbose) {
            boost::algorithm::hex(buffer.begin(), buffer.begin() + n,
                std::ostream_iterator<char>(std::cout));
            std::cout << std::endl;
          }
          if (method) {
            ((*p_this).*method)(error, n);
          }
      });
Esempio n. 11
0
void instance::write(buffer_type& buf,
                     message_type operation,
                     uint32_t* payload_len,
                     uint64_t operation_data,
                     const node_id& source_node,
                     const node_id& dest_node,
                     actor_id source_actor,
                     actor_id dest_actor,
                     payload_writer* pw) {
  if (! pw) {
    binary_serializer bs{std::back_inserter(buf), &get_namespace()};
    source_node.serialize(bs);
    dest_node.serialize(bs);
    bs.write(source_actor)
      .write(dest_actor)
      .write(uint32_t{0})
      .write(static_cast<uint32_t>(operation))
      .write(operation_data);
  } else {
    // reserve space in the buffer to write the payload later on
    auto wr_pos = static_cast<ptrdiff_t>(buf.size());
    char placeholder[basp::header_size];
    buf.insert(buf.end(), std::begin(placeholder), std::end(placeholder));
    auto pl_pos = buf.size();
    { // lifetime scope of first serializer (write payload)
      binary_serializer bs1{std::back_inserter(buf), &get_namespace()};
      (*pw)(bs1);
    }
    // write broker message to the reserved space
    binary_serializer bs2{buf.begin() + wr_pos, &get_namespace()};
    auto plen = static_cast<uint32_t>(buf.size() - pl_pos);
    source_node.serialize(bs2);
    dest_node.serialize(bs2);
    bs2.write(source_actor)
       .write(dest_actor)
       .write(plen)
       .write(static_cast<uint32_t>(operation))
       .write(operation_data);
    if (payload_len)
      *payload_len = plen;
  }
}
Esempio n. 12
0
static std::string readFromFile(buffer_type &buffer, HANDLE hFile) {
	DWORD dwRead = 0;
	std::string str;
	DWORD chunk_size = buffer.size() - 10;
	do {
		DWORD retval = ReadFile(hFile, buffer, chunk_size, &dwRead, NULL);
		if (retval == 0 || dwRead <= 0 || dwRead > chunk_size)
			return str;
		buffer[dwRead] = 0;
		str += buffer;
	} while (dwRead == chunk_size);
	return str;
}
Esempio n. 13
0
				bool is_out_of_range(buffer_type const& buffer, size_type i) const {
					return i >= buffer.size();
				}
Esempio n. 14
0
void async_udp::udp_listener::enlarge_buffer_( buffer_type& bt )
{
	bt->resize( bt->size() + default_buffer_size );
}
Esempio n. 15
0
void multicast_communication::udp_listener::enlarge_buffer_( buffer_type& bt )
{
	bt->resize( bt->size() + default_buffer_size );
}