简体   繁体   English

进程间通信包创建

[英]Inter-process communication packet creation

I'm writing an inter-process communication protocol where ac# client is calling APIs in a server process that will be written in different languages (Java, C, C++, etc.) Both processes run on the same machine and the communication layer itself is not the issue (Currently I'm using sockets since it's supported by most languages but may use shared memory later) There're about 180 APIs in total, each of course with its own set of parameters of various types. 我正在写一个进程间通信协议,其中ac#客户端在将用不同语言(Java,C,C ++等)编写的服务器进程中调用API。这两个进程都在同一台机器上运行,并且通信层本身并不是问题(当前我正在使用套接字,因为大多数语言都支持套接字,但是以后可能会使用共享内存)总共大约有180个API,每个API都有自己的各种类型的参数集。 Since the server can be written in any language, I'm converting all parameters to an array of bytes, put it in a packet with the API identifier and sending it over to the other process where it will be decoded based on the API identifier. 由于服务器可以用任何语言编写,因此我将所有参数转换为字节数组,将其放入具有API标识符的数据包中,然后将其发送到其他进程,在此将根据API标识符对其进行解码。 The return data from the server process will be formatted the same way. 来自服务器进程的返回数据将以相同的方式格式化。

What I'm looking for is the fastest way (and preferably a single function) that takes variable number of different-type parameters and convert them into a byte array. 我正在寻找的是最快的方法(最好是单个函数),该方法将可变数量的不同类型的参数转换为字节数组。 Currently, I'm using BitConverter class to convert each parameter to an array of bytes and concatenate all arrays into one. 当前,我正在使用BitConverter类将每个参数转换为字节数组,并将所有数组连接为一个。 There are no reference-type parameters except strings, which will be converted to an array of bytes with the first 2-bytes containing string length. 除字符串外,没有引用类型的参数,这些参数将转换为字节数组,其中前2个字节包含字符串长度。 I have control over both the client and server so each knows what order of parameters to expect in the packet (Based on API ID) and the size of each parameter. 我对客户端和服务器都有控制权,因此每个人都知道期望数据包中参数的顺序(基于API ID)以及每个参数的大小。

If there's another way to do this or how to do it using a single function like: 如果还有另一种方法或如何使用单个函数来执行此操作,例如:

byte[] toByteArray (variable-number of different-type parameters…)

Please let me know. 请告诉我。 Performance is an issue since the frequency of API calls is quite high. 由于API调用的频率很高,因此性能是一个问题。

I think you should try Google's Protocol Buffers: https://developers.google.com/protocol-buffers/ 我认为您应该尝试使用Google的协议缓冲区: https//developers.google.com/protocol-buffers/

Protobuf is language-neutral. Protobuf与语言无关。 So you define your own structures and then generate specific code for Java, C#, C++, etc. 因此,您可以定义自己的结构,然后为Java,C#,C ++等生成特定的代码。

I opted for using the following code. 我选择使用以下代码。 Was initially concerned about the performance hit of using params and boxing/unboxing, but after some profiling it turns out to be negligible. 最初担心使用参数和装箱/拆箱会对性能造成影响,但经过一些分析后,结果却可以忽略不计。 Here it is for anyone who's interested. 这里适合任何有兴趣的人。

    public static int ToByteArray (byte[] bytes, int Offs, params object[] args)
    {
        int CurrIdx = Offs;

        for (int i=0; i<args.Length; i++)
        {
            object obj = args[i];
            if (obj is int)
            {
                int x = (int) obj;
                bytes[CurrIdx++] = (byte)(x & 0xff);
                bytes[CurrIdx++] = (byte)((x >> 8) & 0xff);
                bytes[CurrIdx++] = (byte)((x >> 16) & 0xff);
                bytes[CurrIdx++] = (byte)((x >> 24) & 0xff);
            }
            else if (obj is double)
            {
                double x = (double)obj;
                System.Buffer.BlockCopy(BitConverter.GetBytes(x), 0, bytes, CurrIdx, sizeof(double));
                CurrIdx += sizeof(double);
            }
            else if (obj is string)
            {
                string x = (string)obj;
                int ByteLen = x.Length * sizeof(char);
                bytes[CurrIdx++] = (byte)(x.Length & 0xff);
                bytes[CurrIdx++] = (byte)((x.Length >> 8) & 0xff);
                System.Buffer.BlockCopy(x.ToCharArray(), 0, bytes, CurrIdx, ByteLen);
                CurrIdx += ByteLen;
            }
        }
        return (CurrIdx - Offs);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM