当前位置: 首页>>代码示例>>C++>>正文


C++ TCPConnection::disconnect_abortive方法代码示例

本文整理汇总了C++中TCPConnection::disconnect_abortive方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPConnection::disconnect_abortive方法的具体用法?C++ TCPConnection::disconnect_abortive怎么用?C++ TCPConnection::disconnect_abortive使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TCPConnection的用法示例。


在下文中一共展示了TCPConnection::disconnect_abortive方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: connection_thread_main

void HTTPServer_Impl::connection_thread_main(TCPConnection connection)
{
	try
	{
		std::string request;
		bool bool_result = read_line(connection, request);
		if (bool_result == false)
		{
			connection.disconnect_abortive();
			return;
		}

		std::string headers;
		bool_result = read_lines(connection, headers);
		if (bool_result == false)
		{
			connection.disconnect_abortive();
			return;
		}

		// Extract request command, url and version:

		std::string command, url, version;
		std::string::size_type pos1 = request.find(' ');
		if (pos1 == std::string::npos)
			throw Exception("Bad request");
		command = request.substr(0, pos1);
		if (command != "POST" && command != "GET")
			throw Exception("Unsupported");
		std::string::size_type pos2 = request.find(' ', pos1 + 1);
		if (pos2 == std::string::npos)
			throw Exception("Bad request");
		url = request.substr(pos1+1, pos2-pos1-1);
		std::string::size_type pos3 = request.find(' ', pos2 + 1);
		if (pos3 != std::string::npos)
			throw Exception("Bad request");
		version = request.substr(pos2 + 1);

		DataBuffer request_data;

		// Handle request:

		// Look for a request handler that will deal with the HTTP request:
		MutexSection mutex_lock(&mutex);
		std::vector<HTTPRequestHandler>::size_type index, size;
		size = handlers.size();
		bool handled = false;
		for (index = 0; index < size; index++)
		{
			if (handlers[index].is_handling_request(command, url, headers))
			{
				HTTPRequestHandler handler = handlers[index];
				mutex_lock.unlock();

				if (command == "POST")
				{
					write_line(connection, "HTTP/1.1 100 Continue");
					// write_line(connection, "Content-Length: 0");
					write_line(connection, "");
				}

				std::shared_ptr<HTTPServerConnection_Impl> connection_impl(std::make_shared<HTTPServerConnection_Impl>());
				connection_impl->connection = connection;
				connection_impl->request_type = command;
				connection_impl->request_url = url;
				connection_impl->request_headers = headers;
				HTTPServerConnection http_connection(connection_impl);
				handler.handle_request(http_connection);
				handled = true;
				break;
			}
		}
		mutex_lock.unlock();

		if (!handled)
		{
			// No handler wants it.  Reply with 404 Not Found:
			std::string error_msg("404 Not Found");
			write_line(connection, "HTTP/1.1 404 Not Found");
			write_line(connection, "Server: ClanLib HTTP Server");
			write_line(connection, "Connection: close");
			write_line(connection, "Vary: *");
			write_line(connection, "Content-Type: text/plain");
			write_line(connection, "Content-Length: " + StringHelp::int_to_local8(error_msg.length()+2));
			write_line(connection, "");
			write_line(connection, error_msg);
		}

		connection.disconnect_graceful();

/*
		if (url == "/")
		{
			File file("Sources/test.html", File::open_existing);
			DataBuffer response(file.get_size());
			file.read(response.get_data(), response.get_size(), true);
			file.close();

			// Send back response.

//.........这里部分代码省略.........
开发者ID:wbyang1985,项目名称:ClanLib,代码行数:101,代码来源:http_server_impl.cpp


注:本文中的TCPConnection::disconnect_abortive方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。