同步套接字/网络连接的对象 - 不使用 WCF 的最佳实践?

发布于 2024-08-11 04:34:24 字数 440 浏览 2 评论 0原文

通过套接字连接同步对象的好方法是什么?

这是我到目前为止的想法。

对象在其 getter 和 setter 中调用 Update 方法,该方法通过套接字连接将类的所有信息传输到其他连接的客户端。为了在客户端上更新这些更改,需要调用一个重载方法,该方法基本上解析字符串以获取其成员的值并相应地设置它们。

这是一种合理的处理方式吗?根据我所读到的 Raknet 如何做它的事情,它让你实现特定的方法,每当对象需要更新时它就会调用这些方法。

如果您处理的所有数据都是原始类型,我可以看到这种工作方式可能效率低下。但是其他班级呢?期望 API 上的用户在自己的类中调用上述方法是否合理?如果他们使用没有重写方法的标准数据结构怎么办?

我知道像 WCF 这样的东西可以为您做到这一点,但我真的很想实现我自己的解决方案,即使它不像 WCF 那样是最佳的。

感谢用户的任何建议! :)

What's a good way to sync objects over a socket connection?

Here's what I've thought of so far.

An object, in its getters and setters, calls an Update method that transfers all of the class's information over the socket connection to other connected clients. To update these changes on the clients, a overloaded method is called that basically parses a string for the values of its members and sets them accordingly.

Is this a reasonable way to go about things? For what I've read on how Raknet does its stuff, it has you implement specific methods that it expects to call whenever an object needs to be updated.

I can see this working, maybe inefficiently, if all the data you're deal with is primitive types. But what about other classes? Is it reasonable to expect the user on an API to call this method described above in their own classes? What if they're using a standard data structure that doesn't have the method overrided.

I know there's things like the WCF that do this for you, but I really want to implement my own solution, even if it's not optimal like WCF may be.

Thanks for any advice SO users! :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-08-18 04:34:24

尝试使用标准 .NET 序列化方法将对象与字符串(或某些二进制 blob)相互转换,请参阅 这里有一篇文章

我不会在每个属性设置器上调用 Update,因为如果对象有很多属性并且用户会更改其中几个属性,则会出现不必要的网络传输并且速度会很慢。相反,用户应该调用像 BeginUpdate 和 FinishUpdate 这样的方法,这些方法可以嵌套(所以我会创建一个嵌套计数器)。当最外层的 FinishUpdate 被调用时,您可以在网络上同步它们。 Setters 可以检查是否调用了 BeginUpdate(嵌套计数器大于零),如果没有,则在这种情况下抛出异常。您甚至可以创建一次性对象,该对象将在创建时调用 BeginUpdate 并在 Dispose 上调用 FinishUpdate,因此您可以编写:

using (var ut = new UpdateTransaction(myObject))
// UpdateTransaction constructor calls myObject.BeginUpdate
{
    myObject.MyProp = 5;
    // ...
}
// at the end of using clause Dispose is called,
// which in turn calls myObject.FinishUpdate  

如果您有一组复杂的对象,那么您需要创建一个类来表示该组对象并将它们全部存储在列表。那么应该为此类实现 BeginUpdate/FinishUpdate,而不是为单个对象实现。

Try to use standart .NET serialization methods to convert objects to/from string (or some binary blob), see an article here.

I would not call Update on each property setter, because if the object has many properties and user would change several of them, there would be unnecessary network transfers and it would be slow. Instead, user should call method like BeginUpdate and FinishUpdate, which could be nested (so I'd make a nest counter). When most outermost FinishUpdate is called, you sync them on network. Setters could check if BeginUpdate is called (nest counter more than zero) and if not, throw exception in that case. You can even make disposable object that would call BeginUpdate on create and FinishUpdate on Dispose, so you can write:

using (var ut = new UpdateTransaction(myObject))
// UpdateTransaction constructor calls myObject.BeginUpdate
{
    myObject.MyProp = 5;
    // ...
}
// at the end of using clause Dispose is called,
// which in turn calls myObject.FinishUpdate  

If you have complex set of objects, then you need to make a class that would represent that set of objects and store them all in the list. BeginUpdate/FinishUpdate then should be implemented for this class, not for individual objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文