示例#1
0
bool
TCP_Client_Connection::ssl_shutdown(long ssl_shutdown_timeout)
{
	bool was_ssl = is_ssl_;

	if ( was_ssl )
	{
		if ( ssl_shutdown_timeout < 0 ) ssl_shutdown_timeout = timeout_;
		if ( ssl_shutdown_timeout > 0 )
		{
			timer_.expires_from_now(boost::posix_time::seconds(ssl_shutdown_timeout));
			timer_.async_wait(
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_socket_timeout,
					this,
					placeholders::error,
					(int) SSL_SHUTDOWN,
					ssl_shutdown_timeout
				)));
		}

		socket_->async_shutdown(
			make_custom_alloc_handler(allocator_,
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_ssl_shutdown,
				this,
				placeholders::error,
				false
			))));
	}

	return was_ssl;
}
示例#2
0
void
TCP_Client_Connection::connect(bool ssl_connect, const ip::tcp::endpoint& remote_endpoint, long connect_timeout, long socket_timeout, long ssl_handshake_timeout, size_t buf_size)
{
	this->timeout_ = socket_timeout;

	ACE_GUARD(ACE_Thread_Mutex, guard, lock_);

	if ( connect_timeout > 0 )
	{
		timer_.expires_from_now(boost::posix_time::seconds(connect_timeout));
		timer_.async_wait(
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_socket_timeout,
				this,
				placeholders::error,
				(int) CONNECT,
				connect_timeout
			)));
	}

	socket().async_connect(
		remote_endpoint,
		make_custom_alloc_handler(allocator_,
		strand_.wrap(boost::bind(
			&TCP_Client_Connection::handle_connect,
			this,
			placeholders::error,
			ssl_connect,
			ssl_handshake_timeout,
			buf_size
		))));
}
示例#3
0
void cyclic_read_session::read_until_tail()
{
  boost::asio::async_read_until(serial_port_, read_buffer_, frame_tail_,
      MA_STRAND_WRAP(strand_, make_custom_alloc_handler(read_allocator_,
          boost::bind(&this_type::handle_read_tail, shared_from_this(),
              _1, _2))));

  port_read_in_progress_ = true;
}
示例#4
0
void
TCP_Client_Connection::read(ACE_Message_Block& mb)
{
	//ACE_GUARD(ACE_Thread_Mutex, guard, lock_);

	if ( timeout_ > 0 )
	{
		timer_.expires_from_now(boost::posix_time::seconds(timeout_));
		timer_.async_wait(
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_socket_timeout,
				this,
				placeholders::error,
				(int) READ,
				timeout_
			)));
	}

	if ( is_ssl_ )
	{
		socket_->async_read_some(
			buffer(mb.wr_ptr(), mb.space()),
			make_custom_alloc_handler(allocator_,
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_read,
				this,
				placeholders::error,
				placeholders::bytes_transferred
			))));
	}
	else
	{
		socket().async_read_some(
			buffer(mb.wr_ptr(), mb.space()),
			make_custom_alloc_handler(allocator_,
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_read,
				this,
				placeholders::error,
				placeholders::bytes_transferred
			))));
	}
}
示例#5
0
void portmap_session::remote_write(const char* buffer, int buffer_length)
{
   boost::shared_ptr<std::vector<char> > buf(new 
      std::vector<char>(buffer, buffer + buffer_length));

   // 发送数据.
   m_remote_socket.async_write_some(boost::asio::buffer(*buf),
      make_custom_alloc_handler(m_allocator, 
      boost::bind(&portmap_session::handle_remote_write, 
      shared_from_this(),  buf,
      boost::asio::placeholders::bytes_transferred,
      boost::asio::placeholders::error)));
}
示例#6
0
void
TCP_Client_Connection::handle_connect(const error_code& error, bool ssl_connect, long ssl_handshake_timeout, size_t buf_size)
{
	timer_.expires_at(boost::posix_time::pos_infin); //timer_.cancel();
	if ( error )
	{
		this->on_connect_error(error);
		return this->close();
	}
	
	// ssl async-handshake
	if ( ssl_connect )
	{
		// no matter ssl handshake is done, mark connection in ssl state.
		is_ssl_ = true;

		if ( ssl_handshake_timeout < 0 ) ssl_handshake_timeout = timeout_;
		if ( ssl_handshake_timeout > 0 )
		{
			timer_.expires_from_now(boost::posix_time::seconds(ssl_handshake_timeout));
			timer_.async_wait(
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_socket_timeout,
					this,
					placeholders::error,
					(int) SSL_HANDSHAKE,
					ssl_handshake_timeout
				)));
		}

		socket_->async_handshake(
			ssl::stream_base::client,
			make_custom_alloc_handler(allocator_,
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_ssl_handshake,
				this,
				placeholders::error,
				true,
				buf_size
			))));
	}
	else
	{
		// delay buffer allocation when handshake is done.
		if ( mb_.size((buf_size)?buf_size:BUF_SIZE) != 0 )
			return this->close();

		this->on_open(mb_);
	}
}
示例#7
0
void portmap_session::remote_connect(const boost::system::error_code& err)
{
   if (!err)
   {
      boost::system::error_code ignored_ec;
      m_remote_socket.set_option(tcp::no_delay(true), ignored_ec);
      // 连接成功, 发起一个读请求.
      m_remote_socket.async_read_some(boost::asio::buffer(m_remote_buffer),
         make_custom_alloc_handler(m_allocator,
         boost::bind(&portmap_session::handle_remote_read, shared_from_this(),
         boost::asio::placeholders::error,
         boost::asio::placeholders::bytes_transferred)));
	  // 读取本地链接数据.
	  m_local_socket.async_read_some(boost::asio::buffer(m_local_buffer),
		  make_custom_alloc_handler(m_allocator,
		  boost::bind(&portmap_session::handle_local_read, shared_from_this(),
		  boost::asio::placeholders::error,
		  boost::asio::placeholders::bytes_transferred)));
   }
   else
   {
      close();
   }
}
示例#8
0
void
TCP_Client_Connection::close(long ssl_shutdown_timeout)
{
	this->on_close();

	//ACE_GUARD(ACE_Thread_Mutex, guard, lock_);

	if ( is_ssl_ )
	{
		if ( ssl_shutdown_timeout < 0 ) ssl_shutdown_timeout = timeout_;
		if ( ssl_shutdown_timeout > 0 )
		{
			timer_.expires_from_now(boost::posix_time::seconds(ssl_shutdown_timeout));
			timer_.async_wait(
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_socket_timeout,
					this,
					placeholders::error,
					(int) SSL_SHUTDOWN,
					ssl_shutdown_timeout
				)));
		}

		socket_->async_shutdown(
			make_custom_alloc_handler(allocator_,
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_ssl_shutdown,
				this,
				placeholders::error,
				true
			))));
	}
	else
	{
		asio::error_code err;
		socket().close(err);

		timer_.expires_from_now(boost::posix_time::seconds(0));
		timer_.async_wait(
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_destroy,
				this
			)));
	}
}
示例#9
0
void portmap_session::handle_local_read(const boost::system::error_code& error,
                                        int bytes_transferred)
{
   if (!error)
   {
      // 发送到remote.
      remote_write(m_local_buffer.data(), bytes_transferred);

      // 继续接收local数据.
      m_local_socket.async_read_some(boost::asio::buffer(m_local_buffer),
         make_custom_alloc_handler(m_allocator,
         boost::bind(&portmap_session::handle_local_read, shared_from_this(),
         boost::asio::placeholders::error,
         boost::asio::placeholders::bytes_transferred)));
   }
   else
   {
      close();
   }
}
示例#10
0
void portmap_session::start()
{
   boost::system::error_code ignored_ec;

   {
      boost::mutex::scoped_lock lock(m_user_mtx);
      m_user_cnt++;
   }

   m_local_socket.set_option(tcp::no_delay(true), ignored_ec);

   m_local_host = m_local_socket.remote_endpoint(ignored_ec);
   log_info("Id: " << m_user_cnt << ", IP: "
      << m_local_host.address().to_string(ignored_ec).c_str() 
      << " : " << m_local_host.port() << " 进来.\n");

   // 发起远程连接.
   m_remote_socket.async_connect(m_remote_host, 
      make_custom_alloc_handler(m_allocator,
      boost::bind(&portmap_session::remote_connect, shared_from_this(),
      boost::asio::placeholders::error)));
}
示例#11
0
bool
TCP_Client_Connection::ssl_handshake(long ssl_handshake_timeout)
{
	bool was_ssl = is_ssl_;

	if ( !was_ssl )
	{
		// no matter ssl handshake is done, mark connection in ssl state.
		is_ssl_ = true;

		if ( ssl_handshake_timeout < 0 ) ssl_handshake_timeout = timeout_;
		if ( ssl_handshake_timeout > 0 )
		{
			timer_.expires_from_now(boost::posix_time::seconds(ssl_handshake_timeout));
			timer_.async_wait(
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_socket_timeout,
					this,
					placeholders::error,
					(int) SSL_HANDSHAKE,
					ssl_handshake_timeout
				)));
		}

		socket_->async_handshake(
			ssl::stream_base::client,
			make_custom_alloc_handler(allocator_,
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_ssl_handshake,
				this,
				placeholders::error,
				false,
				mb_.size()
			))));
	}

	return was_ssl;
}
示例#12
0
void
TCP_Client_Connection::write(ACE_Message_Block& mb, bool write_all)
{
	//ACE_GUARD(ACE_Thread_Mutex, guard, lock_);

	if ( timeout_ > 0 )
	{
		timer_.expires_from_now(boost::posix_time::seconds(timeout_));
		timer_.async_wait(
			strand_.wrap(boost::bind(
				&TCP_Client_Connection::handle_socket_timeout,
				this,
				placeholders::error,
				(int) WRITE,
				timeout_
			)));
	}

	if ( is_ssl_ )
	{
		if ( write_all )
			asio::async_write(
				*socket_,
				buffer(mb.rd_ptr(), mb.length()),
				make_custom_alloc_handler(allocator_,
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_write,
					this,
					placeholders::error,
					placeholders::bytes_transferred
				))));
		else
			socket_->async_write_some(
				buffer(mb.rd_ptr(), mb.length()),
				make_custom_alloc_handler(allocator_,
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_write,
					this,
					placeholders::error,
					placeholders::bytes_transferred
				))));
	}
	else
	{
		if ( write_all )
			asio::async_write(
				socket(),
				buffer(mb.rd_ptr(), mb.length()),
				make_custom_alloc_handler(allocator_,
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_write,
					this,
					placeholders::error,
					placeholders::bytes_transferred
				))));
		else
			socket().async_write_some(
				buffer(mb.rd_ptr(), mb.length()),
				make_custom_alloc_handler(allocator_,
				strand_.wrap(boost::bind(
					&TCP_Client_Connection::handle_write,
					this,
					placeholders::error,
					placeholders::bytes_transferred
				))));
	}
}