同步套接字/网络连接的对象 - 不使用 WCF 的最佳实践?
通过套接字连接同步对象的好方法是什么?
这是我到目前为止的想法。
对象在其 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用标准 .NET 序列化方法将对象与字符串(或某些二进制 blob)相互转换,请参阅 这里有一篇文章。
我不会在每个属性设置器上调用 Update,因为如果对象有很多属性并且用户会更改其中几个属性,则会出现不必要的网络传输并且速度会很慢。相反,用户应该调用像 BeginUpdate 和 FinishUpdate 这样的方法,这些方法可以嵌套(所以我会创建一个嵌套计数器)。当最外层的 FinishUpdate 被调用时,您可以在网络上同步它们。 Setters 可以检查是否调用了 BeginUpdate(嵌套计数器大于零),如果没有,则在这种情况下抛出异常。您甚至可以创建一次性对象,该对象将在创建时调用 BeginUpdate 并在 Dispose 上调用 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:
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.