Syncing Objects for a Socket/Network Connection -- Best Practice Not Using 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 a开发者_开发知识库ny advice SO users! :)
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.
精彩评论