Exemplo n.º 1
0
int main(int argc, char* argv[]) {

	// Let's register the CTRL + c signal handler.
	if(signal(SIGINT, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGINT handler." << std::endl;
		return -1;
	}

	// Register termination handler.
	if(signal(SIGTERM, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGTERM handler." << std::endl;
		return -1;
	}

	// Create the core system.
	if(xdl::createCore(&core, argc, argv, xdl::XdevLFileName("udp_client_demo.xml")) != xdl::ERR_OK) {
		return xdl::ERR_ERROR;
	}

	// Get the instance to the socket module.
	udp_socket = xdl::getModule<xdl::XdevLUDPSocket2*>(core, xdl::XdevLID("MyUDPSocket"));
	if(!udp_socket) {
		return xdl::ERR_ERROR;
	}

	if(udp_socket->connect(xdl::XdevLHostName("localhost"), xdl::XdevLPort(4321)) != xdl::ERR_OK)
		return xdl::ERR_ERROR;

	myNetworkData 					data;
	while(run) {
		data.request = REQ_GETTIME;

		if(udp_socket->write((xdl::xdl_uint8*)&data, sizeof(myNetworkData)) != sizeof(myNetworkData)) {
			std::cerr << "Couldn't send request: " << udp_socket->getError() << std::endl;
		}

		if(udp_socket->read((xdl::xdl_uint8*)&data, sizeof(myNetworkData)) == sizeof(myNetworkData)) {
			std::cout << "XdevLCore server time: " << data.time << "\r" << std::flush;
		}

	}

	// Close socket.
	udp_socket->close();

	// Destroy the Core.
	xdl::destroyCore(core);

	return 0;
}
Exemplo n.º 2
0
int main(int argc, char* argv[]) {

	// Let's register the CTRL + c signal handler.
	if(signal(SIGINT, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGINT handler." << std::endl;
		return -1;
	}

	// Register termination handler.
	if(signal(SIGTERM, exitHandle) == SIG_ERR) {
		std::cerr <<  "Failed to set SIGTERM handler." << std::endl;
		return -1;
	}

	// Create the core system.
	auto result = xdl::createCore(&core, argc, argv);
	if(xdl::RET_SUCCESS != result) {
		return result;
	}

	// Plug the XdevLNetwork plugins into the core.
	result = xdl::plug(core, xdl::XdevLPluginName("XdevLNetwork"), xdl::XdevLVersion(0, 6, 0));
	if(xdl::RET_SUCCESS != result) {
		return result;
	}

	// Create the UDP socket object.
	udp_socket = xdl::createModule<xdl::XdevLUDPSocket2*>(core, xdl::XdevLModuleName("XdevLUDPSocket2"), xdl::XdevLID("MyUDPSocket"));
	if(xdl::isModuleNotValid(udp_socket)) {
		return xdl::RET_FAILED;
	}

	result = udp_socket->connect(xdl::XdevLHostName("localhost"), xdl::XdevLPort(8000));
	if(xdl::RET_SUCCESS != result) {
		return result;
	}

	// Create an array declaration. It describes the structure of the array.
	xdl::XdevLArrayDeclaration arrayDecl;
	arrayDecl.add(xdl::ARRAY_FLOAT_ELEMENT);
	arrayDecl.add(xdl::ARRAY_UINT16_ELEMENT);
	arrayDecl.add(xdl::ARRAY_DOUBLE_ELEMENT);
	arrayDecl.add(xdl::ARRAY_UINT8_ELEMENT);
	arrayDecl.add(xdl::ARRAY_UINT16_ELEMENT);

	// Create an array to store the data.
	std::array<xdl::xdl_uint8, 64> dataToTransfer {{0}};

	while(true) {

		// This class helps us to add values into the array.
		xdl::XdevLArrayModifier arrayModifier(dataToTransfer.data(), 64);

		// Put some values into the array.
		arrayModifier.write<xdl::xdl_float>(10.123f);
		arrayModifier.write<xdl::xdl_uint16>(20);
		arrayModifier.write<xdl::xdl_double>(30.9394);
		arrayModifier.write<xdl::xdl_uint8>(1);
		arrayModifier.write<xdl::xdl_uint16>(40);

		// Send the stuff.
		udp_socket->sendArray((xdl::xdl_uint8*)dataToTransfer.data(), arrayDecl);

		// Wait a bit
		//	xdl::sleep(0.01);

	}

	udp_socket->close();

	// Destroy Core.
	xdl::destroyCore(core);

	return 0;
}