我知道在一些分布式技术(如RPC)中,使用了术语“封送”,但不理解它与序列化有何不同。它们不是都在把对象转换成一系列的比特吗?
相关:
什么是序列化?
什么是对象编组?
我知道在一些分布式技术(如RPC)中,使用了术语“封送”,但不理解它与序列化有何不同。它们不是都在把对象转换成一系列的比特吗?
相关:
什么是序列化?
什么是对象编组?
当前回答
以下是更具体的例子:
序列化的例子:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
typedef struct {
char value[11];
} SerializedInt32;
SerializedInt32 SerializeInt32(int32_t x)
{
SerializedInt32 result;
itoa(x, result.value, 10);
return result;
}
int32_t DeserializeInt32(SerializedInt32 x)
{
int32_t result;
result = atoi(x.value);
return result;
}
int main(int argc, char **argv)
{
int x;
SerializedInt32 data;
int32_t result;
x = -268435455;
data = SerializeInt32(x);
result = DeserializeInt32(data);
printf("x = %s.\n", data.value);
return result;
}
在序列化中,数据以一种可以在以后存储和取消平坦化的方式被平坦化。
编组演示:
(MarshalDemoLib.cpp)
#include <iostream>
#include <string>
extern "C"
__declspec(dllexport)
void *StdCoutStdString(void *s)
{
std::string *str = (std::string *)s;
std::cout << *str;
}
extern "C"
__declspec(dllexport)
void *MarshalCStringToStdString(char *s)
{
std::string *str(new std::string(s));
std::cout << "string was successfully constructed.\n";
return str;
}
extern "C"
__declspec(dllexport)
void DestroyStdString(void *s)
{
std::string *str((std::string *)s);
delete str;
std::cout << "string was successfully destroyed.\n";
}
(MarshalDemo。c)
#include <Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
int main(int argc, char **argv)
{
void *myStdString;
LoadLibrary("MarshalDemoLib");
myStdString = ((void *(*)(char *))GetProcAddress (
GetModuleHandleA("MarshalDemoLib"),
"MarshalCStringToStdString"
))("Hello, World!\n");
((void (*)(void *))GetProcAddress (
GetModuleHandleA("MarshalDemoLib"),
"StdCoutStdString"
))(myStdString);
((void (*)(void *))GetProcAddress (
GetModuleHandleA("MarshalDemoLib"),
"DestroyStdString"
))(myStdString);
}
在封送处理中,数据不一定需要被平铺,但需要转换为另一种替代表示。所有类型转换都是编组,但并不是所有编组都是类型转换。
封送处理不需要涉及动态分配,它也可以只是结构之间的转换。例如,您可能有一对,但函数期望对的第一个和第二个元素是相反的;你把一对施放/memcpy到另一对是不行的,因为FST和SND会被翻转。
#include <stdio.h>
typedef struct {
int fst;
int snd;
} pair1;
typedef struct {
int snd;
int fst;
} pair2;
void pair2_dump(pair2 p)
{
printf("%d %d\n", p.fst, p.snd);
}
pair2 marshal_pair1_to_pair2(pair1 p)
{
pair2 result;
result.fst = p.fst;
result.snd = p.snd;
return result;
}
pair1 given = {3, 7};
int main(int argc, char **argv)
{
pair2_dump(marshal_pair1_to_pair2(given));
return 0;
}
当您开始处理多种类型的带标签联合时,封送的概念变得尤为重要。例如,您可能会发现很难让JavaScript引擎为您打印一个“c字符串”,但您可以要求它为您打印一个包装好的c字符串。或者如果你想在Lua或Python运行时从JavaScript运行时打印字符串。它们都是字符串,但如果没有编组,通常就无法处理。
An annoyance I had recently was that JScript arrays marshal to C# as "__ComObject", and has no documented way to play with this object. I can find the address of where it is, but I really don't know anything else about it, so the only way to really figure it out is to poke at it in any way possible and hopefully find useful information about it. So it becomes easier to create a new object with a friendlier interface like Scripting.Dictionary, copy the data from the JScript array object into it, and pass that object to C# instead of JScript's default array.
(. js)
var x = new ActiveXObject('Dmitry.YetAnotherTestObject.YetAnotherTestObject');
x.send([1, 2, 3, 4]);
(YetAnotherTestObject.cs)
using System;
using System.Runtime.InteropServices;
namespace Dmitry.YetAnotherTestObject
{
[Guid("C612BD9B-74E0-4176-AAB8-C53EB24C2B29"), ComVisible(true)]
public class YetAnotherTestObject
{
public void send(object x)
{
System.Console.WriteLine(x.GetType().Name);
}
}
}
上面打印了"__ComObject",从c#的角度来看,这有点像一个黑盒。
Another interesting concept is that you might have the understanding how to write code, and a computer that knows how to execute instructions, so as a programmer, you are effectively marshaling the concept of what you want the computer to do from your brain to the program image. If we had good enough marshallers, we could just think of what we want to do/change, and the program would change that way without typing on the keyboard. So, if you could have a way to store all the physical changes in your brain for the few seconds where you really want to write a semicolon, you could marshal that data into a signal to print a semicolon, but that's an extreme.
其他回答
来自编组(计算机科学)维基百科的文章:
The term "marshal" is considered to be synonymous with "serialize" in the Python standard library1, but the terms are not synonymous in the Java-related RFC 2713: To "marshal" an object means to record its state and codebase(s) in such a way that when the marshalled object is "unmarshalled", a copy of the original object is obtained, possibly by automatically loading the class definitions of the object. You can marshal any object that is serializable or remote. Marshalling is like serialization, except marshalling also records codebases. Marshalling is different from serialization in that marshalling treats remote objects specially. (RFC 2713) To "serialize" an object means to convert its state into a byte stream in such a way that the byte stream can be converted back into a copy of the object.
因此,编组除了保存对象的状态外,还在字节流中保存对象的代码库。
基础知识第一
Byte Stream - Stream is a sequence of data. Input stream - reads data from source. Output stream - writes data to destination. Java Byte Streams are used to perform input/output byte by byte (8 bits at a time). A byte stream is suitable for processing raw data like binary files. Java Character Streams are used to perform input/output 2 bytes at a time, because Characters are stored using Unicode conventions in Java with 2 bytes for each character. Character stream is useful when we process (read/write) text files.
RMI(远程方法调用)-一个API,提供了一种机制来创建java分布式应用程序。RMI允许一个对象调用另一个JVM中运行的对象的方法。
序列化和编组都被松散地用作同义词。这里有一些区别。
序列化——对象的数据成员被写入二进制形式或字节流(然后可以写入文件/内存/数据库等)。一旦将对象数据成员写入二进制形式,就不能保留任何关于数据类型的信息。
编组-对象被序列化(以二进制格式的字节流),附加数据类型+代码库,然后传递远程对象(RMI)。编组将数据类型转换为预先确定的命名约定,以便可以根据初始数据类型进行重构。
因此序列化是编组的一部分。
CodeBase是告诉Object的接收者该对象的实现可以在哪里找到的信息。任何认为自己可能会将一个对象传递给另一个之前可能没有见过它的程序的程序,都必须设置代码库,以便如果接收方在本地没有可用的代码,可以知道从哪里下载代码。在反序列化对象时,接收方将从中获取代码库并从该位置加载代码。(摘自@Nasir的回答)
序列化几乎就像对象使用的内存的一个愚蠢的内存转储,而编组存储关于自定义数据类型的信息。
在某种程度上,Serialization通过值传递的实现来执行封送,因为没有传递数据类型的信息,只是将原始形式传递到字节流。
如果流从一个操作系统到另一个操作系统,如果不同的操作系统有不同的表示相同数据的方法,序列化可能会有一些与大端序和小端序相关的问题。另一方面,编组非常适合在操作系统之间迁移,因为结果是更高级别的表示。
两者都做一件共同的事情——序列化一个对象。序列化用于传输对象或存储对象。但是:
序列化:当你序列化一个对象时,只有该对象中的成员数据被写入字节流;不是那个代码 实际上实现了对象。 编组:当我们谈论将对象传递给远程对象(RMI)时,会使用编组这个术语。在编组中,对象被序列化(成员数据被序列化)+代码库附加。
因此序列化是编组的一部分。
CodeBase是告诉Object的接收者该对象的实现可以在哪里找到的信息。任何认为自己可能会将一个对象传递给另一个之前可能没有见过它的程序的程序,都必须设置代码库,以便如果接收方在本地没有可用的代码,可以知道从哪里下载代码。在反序列化对象时,接收方将从中获取代码库并从该位置加载代码。
封送处理是指将函数的签名和参数转换为单字节数组。 专门用于RPC的目的。
序列化通常是指将整个对象/对象树转换为字节数组 封送处理将序列化对象参数,以便将它们添加到消息中并通过网络传递。 序列化也可以用于存储到磁盘
可以把它们看作同义词,它们都有一个生产者,把东西发送给消费者……在实例的结束字段被写入一个字节流,而另一端则将相同的实例反向和向上写入。
注意:java RMI还支持从接收端传输缺少的类…