Is there a good way, to generate code in C++ for sending function parameters over the network?

StackOverflow https://stackoverflow.com/questions/13633926

  •  03-12-2021
  •  | 
  •  

I need something (like annotations, reflections, defines, or something completely different) that I can add to a arbitrary function or function call.

For every function f, which has this something added, a specific function t should be invoked with the parameters of f.

The function t should now send its parameters over the network to a other program.

Is there a way to do something like this?

有帮助吗?

解决方案

The general principle behind this is called marshalling, where the parameteres of a function are wrapped up in a way that you can send them to some other computer. The sender marshalls the parameters, the receiver unmarshalls. Both need to have an understanding on the types, and must do it in a way consistent with the different computer architectures.

Remote procedure calls (RPC) are one way of sending function calls from one computer to another, and need some marshalling behind them.

XMLRPC, as pointed out by Victor, is one way to do it, where the marshalling happens via XML. There are lots of other packages, and googling for RPCs and marshalling should give more possibilities, but XMLRPC might be just what you want.

CORBA and DCOM are related mechanism. The idea of sending function calls between computers is one major concept in distributed systems, so maybe look a round a little more, probably there are very simple solutions to your specific problem.

其他提示

XMLRPC library might be the one you're looking for, if you just want to call the function on the remote machine. It generates the code for marshalling the parameters using the XML RPC specs.

You can use Google Protocol Buffers for this: https://developers.google.com/protocol-buffers/docs/overview

It is an easy to use and portable serialisation library. In addition you will be able to write programs able to 'talk' to your C++ code (deserialize objects serialised by your C++ code) in different languages, like Python, Java, and others, which can be very useful, for example for easier testing.

If you are in a Linux/BSD environment, do 'man rpcgen'. I found it easier to build distributed apps with than DCE/CORBA.

Let's give what you are trying to do a name, 'inter-process function call' or IFC.

I have been working on a IFC compiler (ifcc) for generating scalable cloud apps with realtime browser based visibility built into the apps.

The prototype compiler is working. The visibility portion of the framework is also working but not yet integrated into the compiler for generating instrumented code.

Ifcc takes as its input a C include file and based on the function prototype declarations in the .h file, it generates C source code for marshalling, and, client and server proxy/stubs suitable for use in a heterogenous (takes care of byte ordering/big endian/little endian) environment.

And, Oh, by the way, ifcc is a smart compiler. It not only handles arrays, typedefs, nested structures, etc. but it also does arbitrary pointer chasing. E.g. if you call a function with a pointer to doubly link list, it will package it up, send it and recreate the doubly link list at the receiving end.

If people are interested in playing with it, add a comment with output of 'uname -a' of your system as well as which compiler you are using. If there is lots of interest I may make it available for developers to learn/play/experiment with.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top