Beispiel #1
0
void master_threads::run_once(ACL_VSTREAM* client)
{
	if (service_on_accept(client) != 0)
		return;

	socket_stream* stream = (socket_stream*) client->context;
	acl_assert(stream);
	ACL_SOCKET fd = stream->sock_handle();
	int   timeout = stream->get_rw_timeout();

	while (true)
	{
		if (ACL_VSTREAM_BFRD_CNT(client) > 0)
		{
			// 当函数返回 1 时表示 client 已经被关闭了
			if (service_main(client, NULL) == 1)
				break;
			continue;
		}

		// acl_read_wait 当 timeout 为 -1 时才是完全阻塞
		// 等待连接有数据可读,当为 0 时则会立即返回,当
		// > 0 时则等待最多指定超时时间
		if(acl_read_wait(fd, timeout > 0 ? timeout : -1) == 0)
			client->sys_read_ready = 1;
		else if (service_on_timeout(client, NULL) == 0)
			continue;
		else
		{
			service_on_close(client, NULL);
			// 删除流对象时会同时关闭套接字
			delete stream;
			break;
		}

		// 当函数返回 1 时表示 client 已经被关闭了
		if (service_main(client, NULL) == 1)
			break;
	}

	if (__count_limit > 0 && __count >= __count_limit)
		__stop = true;
}
Beispiel #2
0
void master_threads2::run_once(ACL_VSTREAM* client)
{
	if (service_on_accept(client) != 0)
	{
		service_on_close(client, NULL);
		acl_vstream_close(client);
		return;
	}

	socket_stream* stream = (socket_stream*) client->context;
	acl_assert(stream);
	ACL_SOCKET fd = stream->sock_handle();
	int   timeout = stream->get_rw_timeout();
	int   ret;

	while (true)
	{
		if (ACL_VSTREAM_BFRD_CNT(client) > 0)
		{
			// 当函数返回 1 时表示 client 已经被关闭了
			if (service_main(client, NULL) == 1)
				break;
			continue;
		}

		// acl_read_wait 当 timeout 为 -1 时才是完全阻塞
		// 等待连接有数据可读,当为 0 时则会立即返回,当
		// > 0 时则等待最多指定超时时间
		if(acl_read_wait(fd, timeout > 0 ? timeout : -1) == 0)
			client->read_ready = 1;
		else if (service_on_timeout(client, NULL) == 0)
			continue;
		else
		{
			service_on_close(client, NULL);

			// stream 对象会在 service_on_close 中被删除,
			// 但在删除时并不会真正关闭套接流,所以需要在
			// 此处关闭套接字流
			acl_vstream_close(client);
			break;
		}

		// 返回 -1 表示需要关闭该客户端连接
		if ((ret = service_main(client, NULL)) == -1)
		{
			service_on_close(client, NULL);

			// stream 对象会在 service_on_close 中被删除,
			// 但在删除时并不会真正关闭套接流,所以需要在
			// 此处关闭套接字流
			acl_vstream_close(client);
			break;
		}

		// service_main 只能返回 0 或 -1
		acl_assert(ret == 0);
	}

	if (__count_limit > 0 && __count >= __count_limit)
		__stop = true;
}