Exemple #1
0
bool DEProtoProxy::get_Image(void* image, unsigned int length)
{
	boost::lock_guard<boost::mutex>(*this->server->getTransactionMutex());

	DEPacket pkt, dataHeader;
	pkt.set_type(DEPacket::P_COMMAND);
	SingleCommand* cmd = pkt.add_command();
	cmd->set_command_id(k_GetImage);

	this->sendCommand(pkt);
	this->receiveFromServer(dataHeader);
	if (dataHeader.type() != DEPacket::P_DATA_HEADER ||
		!dataHeader.has_data_header()) 
		BOOST_THROW_EXCEPTION(ProtocolException() << errorMessage("Did not receive expected data header for image."));
	
	this->imgBuffer.resizeIfNeeded(static_cast<std::size_t>(dataHeader.data_header().bytesize()));

	if (!this->server->receive(this->imgBuffer.getBufferPtr(), static_cast<std::size_t>(dataHeader.data_header().bytesize()), this->imageTimeout))
	{
		BOOST_THROW_EXCEPTION(CommunicationException() << errorMessage("Unable to receive image from server."));
	}

	if (dataHeader.data_header().bytesize() != length)
	{
		BOOST_THROW_EXCEPTION(CommandException() << errorMessage ("Image received did not have the expected size"));
	}

	memcpy(image, this->imgBuffer.getBufferPtr(), length);

	return true;
}
Exemple #2
0
void PacketCreator::addSingleCommand(DEPacket& message, type cmdVal)
{
	message.set_type(DEPacket::P_COMMAND);
	SingleCommand* cmd = message.add_command();
	cmd->set_command_id(cmdVal);

	vector<Param>::iterator  it;
	for (it = this->_params.begin(); it != this->_params.end(); it++)
	{
		AnyParameter* param = cmd->add_parameter();
		this->setAnyParameter(param, *it);
	}

	if (!this->_persistent) this->clear();
}
bool PacketUtility::getValues(const SingleCommand& singleCommand)
{
	// Check return types.
	bool formatIsCorrect = true;
	if (singleCommand.parameter_size() != this->_params.size())
		formatIsCorrect = false;

	if (formatIsCorrect)
	{
		for (unsigned int i = 0; i < this->_params.size(); i++)
		{
			if (singleCommand.parameter(i).type() != this->_params[i].get<0>())
			{
				formatIsCorrect = false;
				break;
			}
		}
	}

	// Everything checks out, get values.
	if (formatIsCorrect)
	{
		for (int i = 0; i < singleCommand.parameter_size(); i++)
		{
			switch (singleCommand.parameter(i).type())
			{
				case AnyParameter::P_INT:
				 (*(int*)(this->_params[i].get<1>())) = singleCommand.parameter(i).p_int();
				 break;
				case AnyParameter::P_BOOL:
				 (*(bool*)(this->_params[i].get<1>())) = singleCommand.parameter(i).p_bool();
				 break;
				case AnyParameter::P_FLOAT:
				 (*(float*)(this->_params[i].get<1>())) = singleCommand.parameter(i).p_float();
				 break;
				default: // not implemented
				 formatIsCorrect =  false;
			}
		}
	}

	if (!this->persistent) this->clear();
	return formatIsCorrect;
}