Thread: serialization and object types

  1. #1
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20

    serialization and object types

    I run an open source project that was started in Java, but for performance considerations I am re-writing in C++. With Java it was easy to implement an object-oriented client-server communication model which passed self-extracting classes between sockets. There was a "virtual" base class that specified a method to perform the unpacking. To create a type of message (lets say sending a username and a message) I would create a new serializable class with the needed variables and implement the unpacking function as needed. To send the data I would create that new class and send it to the socket. To recieve, I would read in an object of the base class type from the socket and call that "virtual" function to unpack: Java took care of all the serialization and type handling.

    I'm having trouble planning this in C++. I was able to create the base-class and sub-classes and emulate serialization using overloaded stream operators. The problem is that I cannot figure out how to read the data from the socket, figure out what type of object I recieved and call the correct unpack function.

    I havn't used C++ in a long time and i'm a little rusty, so its taking me a while to get used to inheritance and type changing.

    Could anyone offer any suggestions or point me in the right direction? This project is very important to me and this change to C++ will really make this project usable. If you want to check out the project idea, you can visit our homepage

    Any help would be *greatly* appreciated. Thanks!
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Do you have any pseudocode that we may look at?

  3. #3
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    Here's some psudo-ish C++-ish Java-ish code. It is the basic jist of how I would like things to work. The idea is to make creating and handling different types of data to be sent over the network as easy as possible.

    Code:
    abstract class SocketData {
    	// this does the unpacking, sc is a base class which
    	//  is implemented by the client and server
    	void performAction (SocketConnection sc);
    }
    
    class SD_Chat : public SocketData {
    	string name;
    	string message;
    	
    	void performAction (SocketConnection sc) {
    		sc.chatMessage(name, message);
    	}
    }
    
    class ClientConnection : public SocketConnection {
    	// the mulithreaded function
    	void run() {
    		SocketData sd = (SocketData) socket.recieveObject();
    		sd.performAction(this);
    	}
    }
    
    void main() {
    	ClientConneciton.sendObject(new SD_Chat("joe", "hi world"));
    }
    I have this model working in another open source java-based project of mine. You can see the API docs here, or the CVS here

    Does this help?
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  4. #4
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    Basically the problem I am running into is how do I send type information? and how do I use that type information to create the correct object when reading?
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Read this . This is a sample chapter from Andrei Alexandrescus excellent work modern c++ design. You should have some ideas after reading through it.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  6. #6
    Registered User Joe Monti's Avatar
    Join Date
    Feb 2003
    Posts
    20
    That really helps. Thanks!
    - Joseph Monti
    __________________
    This message is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation. For more info visit http://joe.tgpr.org/

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. object from/to XML
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 05-07-2008, 06:59 AM
  2. Replies: 5
    Last Post: 06-16-2007, 11:56 PM
  3. Serialization yay!
    By Shamino in forum C++ Programming
    Replies: 11
    Last Post: 06-10-2007, 05:53 PM
  4. Object serialization
    By Mario F. in forum C++ Programming
    Replies: 10
    Last Post: 07-20-2006, 06:20 AM
  5. Serialization
    By Asagohan in forum C++ Programming
    Replies: 8
    Last Post: 10-11-2005, 10:57 PM