Ejemplo n.º 1
0
void file_socket::handle_msg(out_msg_ctype& msg)
{
	if (msg.size() <= ORDER_LEN)
	{
		printf("wrong order length: " size_t_format ".\n", msg.size());
		return;
	}

	switch (*msg.data())
	{
	case 0:
		if (TRANS_IDLE == state)
		{
			trans_end();

			char buffer[ORDER_LEN + DATA_LEN];
			*buffer = 0; //head

			file = fopen(boost::next(msg.data(), ORDER_LEN), "rb");
			if (NULL != file)
			{
				fseeko64(file, 0, SEEK_END);
				__off64_t length = ftello64(file);
				memcpy(boost::next(buffer, ORDER_LEN), &length, DATA_LEN);
				state = TRANS_PREPARE;
			}
			else
			{
				memset(boost::next(buffer, ORDER_LEN), -1, DATA_LEN);
				printf("can't not open file %s!\n", boost::next(msg.data(), ORDER_LEN));
			}

			send_msg(buffer, sizeof(buffer), true);
		}
		break;
	case 1:
		if (TRANS_PREPARE == state && NULL != file && ORDER_LEN + OFFSET_LEN + DATA_LEN == msg.size())
		{
			__off64_t offset;
			memcpy(&offset, boost::next(msg.data(), ORDER_LEN), OFFSET_LEN);
			__off64_t length;
			memcpy(&length, boost::next(msg.data(), ORDER_LEN + OFFSET_LEN), DATA_LEN);
			if (offset >= 0 && length > 0 && offset + length <= ftello64(file))
			{
				state = TRANS_BUSY;
				fseeko64(file, offset, SEEK_SET);
				direct_send_msg(replaceable_buffer(boost::make_shared<file_buffer>(file, length)), true);
			}
		}
		break;
	case 2:
		printf("client says: %s\n", boost::next(msg.data(), ORDER_LEN));
		break;
	default:
		break;
	}
}
Ejemplo n.º 2
0
void file_socket::handle_msg(const file_buffer& str)
{
	if (str.size() <= ORDER_LEN)
	{
		printf("wrong order length: " size_t_format ".\n", str.size());
		return;
	}

	switch (*str.data())
	{
	case 0:
		if (TRANS_IDLE == state)
		{
			trans_end();

			char buffer[ORDER_LEN + DATA_LEN];
			*buffer = 0; //head

			file = fopen(std::next(str.data(), ORDER_LEN), "rb");
			if (nullptr != file)
			{
				fseeko64(file, 0, SEEK_END);
				auto length = ftello64(file);
				memcpy(std::next(buffer, ORDER_LEN), &length, DATA_LEN);
				state = TRANS_PREPARE;
			}
			else
			{
				*(__off64_t*) std::next(buffer, ORDER_LEN) = -1;
				printf("can't not open file %s!\n", std::next(str.data(), ORDER_LEN));
			}

			send_msg(buffer, sizeof(buffer), true);
		}
		break;
	case 1:
		if (TRANS_PREPARE == state && nullptr != file && ORDER_LEN + OFFSET_LEN + DATA_LEN == str.size())
		{
			auto offset = *(__off64_t*) std::next(str.data(), ORDER_LEN);
			auto length = *(__off64_t*) std::next(str.data(), ORDER_LEN + OFFSET_LEN);
			if (offset >= 0 && length > 0 && offset + length <= ftello64(file))
			{
				state = TRANS_BUSY;
				direct_send_msg(file_buffer(boost::make_shared<data_buffer>(file, offset, length)), true);
			}
		}
		break;
	case 2:
		printf("client says: %s\n", std::next(str.data(), ORDER_LEN));
		break;
	default:
		break;
	}
}
Ejemplo n.º 3
0
void file_socket::on_msg_send(in_msg_type& msg)
{
	BOOST_AUTO(buffer, boost::dynamic_pointer_cast<file_buffer>(msg.raw_buffer()));
	if (NULL != buffer)
	{
		buffer->read();
		if (buffer->empty())
			trans_end();
		else
			direct_send_msg(msg, true);
	}
}
Ejemplo n.º 4
0
void file_socket::on_msg_send(file_buffer& msg)
{
	auto buffer = boost::dynamic_pointer_cast<data_buffer>(msg.raw_buffer());
	if (nullptr != buffer)
	{
		buffer->read();
		if (buffer->empty())
			trans_end();
		else
			direct_send_msg(msg, true);
		}
}