コード例 #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;
}
コード例 #2
0
void exitHandle(int signal) {

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

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

	exit(0);
}
コード例 #3
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;
}
コード例 #4
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) != xdl::ERR_OK) {
		return xdl::ERR_ERROR;
	}

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

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

	if(udp_socket->bind(xdl::XdevLPort(4321)) != xdl::ERR_OK) {
		std::cout << "Can't bind socket.\n";
		return xdl::ERR_ERROR;
	}

	// We need an array declaration because this will be send with the data. Using
	// this we will know how the send structure will look like.
	xdl::XdevLArrayDeclaration arrayDecl;

	std::array<xdl::xdl_uint8, 64> dataReceived {{0}};

	xdl::XdevLTimer timer;

	while(true) {
		timer.setT1();

		auto ret = 0;
		// Read incomming data.
		if((ret = udp_socket->receiveArray((xdl::xdl_uint8*)dataReceived.data(), arrayDecl)) != -1) {

			// This class helps us to read values from and array.
			xdl::XdevLArrayModifier buffer(dataReceived.data(), ret);

			// Iterate through all elements.
			for(auto& declaration : arrayDecl) {

				switch(declaration.element) {
					case xdl::ARRAY_DOUBLE_ELEMENT: {
						std::cout << "xdl_double: " << buffer.read<xdl::xdl_double>() << std::endl;
					}
					break;
					case xdl::ARRAY_FLOAT_ELEMENT: {
						std::cout << "xdl_float: " << buffer.read<xdl::xdl_float>() << std::endl;
					}
					break;
					case xdl::ARRAY_INT32_ELEMENT: {
						std::cout << "xdl_int32: " << buffer.read<xdl::xdl_int32>() << std::endl;
					}
					break;
					case xdl::ARRAY_UINT32_ELEMENT: {
						std::cout << "xdl_uint32: " << buffer.read<xdl::xdl_uint32>() << std::endl;
					}
					break;
					case xdl::ARRAY_INT16_ELEMENT: {
						std::cout << "xdl_int16: " << buffer.read<xdl::xdl_int16>() << std::endl;
					}
					break;
					case xdl::ARRAY_UINT16_ELEMENT: {
						std::cout << "xdl_uint16: " << buffer.read<xdl::xdl_uint16>() << std::endl;
					}
					break;
					case xdl::ARRAY_INT8_ELEMENT: {
						std::cout << "xdl_int8: " << (xdl::xdl_int)buffer.read<xdl::xdl_int8>() << std::endl;
					}
					break;
					case xdl::ARRAY_UINT8_ELEMENT: {
						std::cout << "xdl_uint8: " << (xdl::xdl_int)buffer.read<xdl::xdl_uint8>() << std::endl;
					}
					break;
				}
				std::cout << std::endl;
			}

		}
		timer.setT2();

		std::cout << "Time: " << (timer.getT2T164()) << " us" << std::endl;
	}

	udp_socket->close();

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

	return 0;
}