• Content count

  • Joined

  • Last visited

Community Reputation

223 Neutral

About ms291052

  • Rank
    Advanced Member
  1. Quote:Original post by ChaosEngine TBH, I've only used DataContractSerializer in the context of wcf, but AFAIK every is taken care of for you. I think this code shows the deserialisation. public T GetTypedObject<T>(string xml) { DataContractSerializer dataContractSerialiser = new DataContractSerializer(typeof(T)); byte[] buffer = GetBuffer(xml); XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(buffer, XmlDictionaryReaderQuotas.Max) ; T typedObject = (T)dataContractSerialiser.ReadObject(reader, true); return typedObject; } Still, in this code, you have to magically know that the XML string contains a certain type of object. For example, if you think the XML string contains a LoginRequest, you have to call GetTypedObject<LoginRequest>( XML ). The problem now lies in finding out the TYPE of the object before you actually deserialize, so that you know which type to pass into the method you outlined.
  2. Quote:Original post by ChaosEngine Ok, you're using sockets for comms, but there's nothing stopping you using WCF's serialisation mechanism. Just use the DataContractSerializer and write to a MemoryStream. Rolling your own will be more hassle than it's worth. Your suggestion is good. I'm actually using the System.Xml.Serialization library instead, but it's roughly the same idea. By no means am I rolling my own. The most difficult part of this process, however, is deciding which type to deserialize into. This must be known before deserialization to create a XML deserializer / DataContractSerializer with the proper template. My code right now looks like this: public object Deserialize( string Data ) { Type T = GetDataType( Data ); // See footnote for Serializer definition. MethodInfo DeserializeMethod = typeof( Serializer ).GetMethod( "Deserialize" ); DeserializeMethod = DeserializeMethod.MakeGenericMethod( T ); object DataObject = DeserializeMethod.Invoke( null, new object[] { Data } ); return DataObject; } This is all well in good, but the hard part is in that black box function, "GetDataType", which takes a string of XML code and magically returns back the type of the object contained therein (or null, if it's invalid). Does the DataContractSerializer do this for you? If so, that'd be wonderful, but I'm unfortunately not seeing that feature. Footnote (Serializer class): public class Serializer { public static string Serialize<T>( T Input ) { XmlSerializer Serializer = new XmlSerializer( typeof( T ) ); StringWriter StringWriter = new StringWriter(); Serializer.Serialize( StringWriter, Input ); return StringWriter.ToString(); } public static T Deserialize<T>( string Input ) { XmlSerializer Serializer = new XmlSerializer( typeof( T ) ); StringReader Reader = new StringReader( Input ); return (T)Serializer.Deserialize( Reader ); } }
  3. Quote: Naively, I'd wonder why you weren't using any of the built-in serialization libraries. I am. I use the System.Xml.Serialization stuff. That still doesn't help me dispatch the objects to appropriate functions according to type.
  4. Quote: Though the entire concept makes me wonder if this isn't the end of a dark, treacherous path filled with dubious design choices. I sure hope not! The situation is I have a stream of objects coming over a socket connection. I then deserialize them, and I wish to dispatch different objects to different functions. For example, the StateUpdate needs to be passed to UpdateGame( StateUpdate ), while the LoginRequest needs to be passed to HandleLogin( LoginRequest), etc. Any better suggestions on how to do this elegantly?
  5. void SomeFunctionWithAForLoop() { int i; foo(); bar(); for( i = 0; i < 10; i++ ) printf( "hello, world!" ); myfunction(); myotherfunct(); } Just declare the variable at the top...
  6. You're still declaring key in the middle of the code. In C, all variables must be declared at the top of the _function_, not just above the loop that they're used in.
  7. Hello, everyone. I have a bit of a C# problem. I would like to have a map that takes a type T as a key, and a delegate of the form "void function( T Parameter )" as the value. This would ideally allow me to invoke a function with the syntax Map[ typeof( Item ) ]( Item ), or similar. Thank you for your advice! ms291052
  8. Quote:Original post by cyansoft Quote: 1) Loop through the potential types, and attempt to serialize the incoming string in a try/catch block until we find one that doesn't throw an error. Not a good idea. There's a great chance that you may run in a scenario where a message as the same data signature and the wrong server method is called. This is very true, and could cause a lot of confusion. Thanks for pointing this out! Quote:Original post by cyansoft Quote: 3) Prepend the request with a byte representing the type. Server-side we peel this byte off, and then parse the object as the corresponding type. Too cumbersome. Remember that you'll have to maintain both the client and server side programs, tracking the magic byte used to identify each message. True. For each addition, I'd have to modify the shared code, the server's dispatching routine, and the client's sending routine. Nevertheless, that's only marginally more effort than the other two solutions. Quote:Original post by Codeka use something like a HTTP header which is a little more extensible I like this idea a lot, as it could just be blended into the current message header. Nevertheless, parsing becomes more difficult. Simply parsing out the root tag would be the easiest solution to implement, but as Codeka points out, some sort of header is necessary anyway, and malformed XML could cause significant issues with the '>' solution. Thanks for your advice guys. I'm always glad to hear multiple opinions before I try to reach a verdict.
  9. Quote:Original post by cyansoft Why are you not using the WFC (Windows Communication Foundation) for your web services? I have looked into WCF as well, and even developed a preliminary version of the project using WCF. Unfortunately, due to the pushing of data from the server to the client occasionally, management decided to use pure sockets throughout the project.
  10. Hello, everyone. In my most recent application I have a client-server setup where a client issues requests to the server in the form of XML serialized objects. For example, a client might issue a login request by sending the server an object like this: class LoginInformation { public string UserID { get; set; } public string PasswordHash {get; set; } public LoginInformation(); } Naturally there are many types of requests, logins, information updates, status queries, et cetera. The client can send all these objects fine, but on the server side, how does one most effectively handle these requests? I'm trying to find a way where when the server receives a string reprenting an object, and say for a LoginInformation object, it calls HandleLogin( LoginInformation info), for example, and similar functions for each other object type. So far I have several ideas on how to do this, none of which are very elegant: 1) Loop through the potential types, and attempt to serialize the incoming string in a try/catch block until we find one that doesn't throw an error. 2) Find the first index of the character '>' and switch based on the tag of the first element in the XML string. 3) Prepend the request with a byte representing the type. Server-side we peel this byte off, and then parse the object as the corresponding type. None of these methods are very appealing, but this must be a relatively common scenario. Might anyone offer some advice? Thank you, ms291052
  11. Event Handlers and Decoupling

    Actually, one of the goals of this project is to implement a scripting language into all of this. The question then is how do the player and world expose themselves to the script? Assuming they're not global, we run into many of the same problems. I'm still quite interested into what the 'ideal' software engineering solution is. Similar problems come up from time to time, and I would like to have a solid foundation on which to continue.
  12. I have a simple design question related to event handlers in my small bird's eye 2-D game. So far I have the following classes: Map - Manages the grid of tiles that represents a map. Object - Contains a position and string ID (e.g. "TreasureChest", "Goblin", or "Door"). Player - Derived from Object. Contains miscellaneous player-related data, such as inventory. World - Contains a map, a player, and a list of objects. I would like to allow the player to interact with objects by specifying an Object.Use() function. In the case of a treasure chest, the player would receive the treasure. In the case of the goblin, combat is initiated. In the case of a door, the world is changed to whatever is beyond the door. My question is where to define the Use() function. The obvious thing to do is to make Object and interface and implement it in classes for TreasureChest, Goblin, and Door, thus overloading the Use() function to do the appropriate action. The downside to this approach, however, is that a large number of small classes would be used, and this would force a lot of coupling. For example, the TreasureChest would have to know about the Player to modify his inventory and the Door class would have to know about the entire world to be able to change the map and Object list. An alternative solution is to store a function pointer in Object. As of now the function prototype is void EventHandler( Object Source, Object Sender, World World ). Then I can do this (pseudo-code): static void PickUpHandlers::ItemPickUpHandler( Object Source, Object Sender, World World) { Sender.Inventory.Add( Source ); World.DeleteObject( Source ); } void World::Load() { // Load map data Object TreasureChest = new Object( X, Y, "TreasureChest", PickUpHandlers.ItemPickUpHandler ); } void Input::UseButtonPressed() { // Find target object based on player position: TargetObject.Use( TargetObject, Player, World ); } Questions: - Is this approach reasonable? It certainly seems better than the first approach to me. - Where should the event handlers go? Static functions in an event-handler class seems clumsy. - Is there a better solution to this problem that I have overlooked? Thank you very much for your time and advice, ms
  13. In C++ it was possible to say class MyClass { int i : 4; int j : 3; int k : 1; }; is there a way to do the same thing in C#? Thank you for your time!
  14. I'm trying to do something in a program that I believe is quite similar to double dispatch, but I was wondering if there is a better way without mucking all my classes. I have a base class, Animal, and two sub-classes, Tiger and Deer. I load up an array of animals, and for each pair I call Interact(). I desire that the call to interact would select the appropriate call based on the types (i.e. derived types) of the arguments, and not just always call Interact( Animal, Animal ). Is there a way to accomplish this? Sample code (C#): using System; using System.Collections.Generic; using System.Text; namespace Test { class Animal { public virtual void makeSound() { System.Console.Out.WriteLine( "Generic animal noise" ); } }; class Deer : Animal { public override void makeSound() { System.Console.Out.WriteLine( "Deer make no sound." ); } } class Tiger : Animal { public override void makeSound() { System.Console.Out.WriteLine( "Rawr from the Tiger" ); } } class MainClass { static void Interact( Animal a, Animal b ) { System.Console.Out.WriteLine( "Generic Interaction" ); } static void Interact( Deer d, Deer t ) { System.Console.Out.WriteLine( "New deer produced." ); } static void Interact( Tiger d, Tiger t ) { System.Console.Out.WriteLine( "New tiger produced" ); } static void Interact( Deer d, Tiger t ) { System.Console.Out.WriteLine( "There is now one less deer in existance" ); } public static void Main( string[] Args ) { Animal[] Animals = new Animal[ 4 ]; Animals[ 0 ] = new Deer(); Animals[ 1 ] = new Deer(); Animals[ 2 ] = new Tiger(); Animals[ 3 ] = new Tiger(); for( int i = 0; i &lt; Animals.Length; i++ ) Animals[ i ].makeSound(); System.Console.Out.WriteLine(); for( int i = 0; i &lt; Animals.Length; i++ ) for( int j = i + 1; j &lt; Animals.Length; j++ ) Interact( Animals[ i ], Animals[ j ] ); } } } The output is always a string of "Generic Interaction" whereas it should in fact be: New deer produced. There is now one less deer in existance There is now one less deer in existance There is now one less deer in existance There is now one less deer in existance New tiger produced Any help would be appreciated, in any language. Thank you very much for your time.
  15. Thank you all. That makes sense. Now perhaps I can more successfully read .NET code.