tags:

views:

96

answers:

2

I am writing a client/server app in C++ and need to realize simple protocol to sent and receive data correctly. I have a class for server protocol which can convert the message to my format and then convert it again from this format.
That what is server-side protocol looks like: class BaseProtocol { protected: int NumberOfBytesInPackOfText; std::string serviceString; public: BaseProtocol(int SizeOfTextPack, const char* extraString): NumberOfBytesInPackOfText(SizeOfTextPack), serviceString(extraString) {} virtual std::string& convertToSystemMessage(const char* message)=0; virtual std::string& convertToNativeMessage(const char* message)=0; virtual ~BaseProtocol() { NumberOfBytesInPackOfText = 0; serviceString = ""; } };

class SimpleProtocol: public BaseProtocol { public: SimpleProtocol(int SizeOfTextPack, const char* service): BaseProtocol(SizeOfTextPack, service) {} std::string& convertToSystemMessage(const char* nativeMessage); std::string& convertToNativeMessage(const char* systemMessage); };

Now I can not decide what will be the better decision to make to implement that protocol on client application. A friend class to the server protocol? Or may be I need to extract subclass from server protocol and then the client protocol will be derived from this class?

+3  A: 

I would create a protocol class independent of both the client and the server. They can each call the functions of that class rather than inheriting from the class.

This follows the principle of favoring composition over inheritance. Also, neither the client nor the server "is a" protocol interpreter. Rather, they each simply need to use the protocol.

Joe Soul-bringer
A: 

you sure you need a different implementation for server and client?

at the level your protocol is at, it looks like it should be designed to be the same for both the server and the client.

at a higher level (the types of messages you are sending) the client and server should vary.

Keith Nicholas