This is a bit of a long post but I'll be grateful to anyone who cam help out
I've been coding a server application for a few weeks now. As it gets more and more complicated, some design issues are coming up which I'm having trouble deciding on, and thought I'd ask you what you think.
My application uses a select() statement to control the I/O and a callback function system similar to that of squid and ircd-hybrid, although I've decided to try and implement some of this using C++ OOP rather than pure C as in squid and ircd-hybrid.
The way the system works is like this:
Each socket (whether it be a listening socket, client connection or whatever) has an associated callback function to call when the socket has data waiting, and when it is possible to write to the socket. Using the output from select, the appropriate callback functions are called, and the I/O is handled. For example, with a listening socket, it's readability callback would be a function which accepts the new connection, and with a client socket, the function would parse the incoming data and do whatever is required.
I'm finding it hard to decide exactly how to turn this system into C++ classes in an elegant way. I'm very new to OOP so I lack practice.
I've been trying to think about it in an "is a" fashion, as most OOP books tell you to do.
I thought of having a Socket class, which has the following member variables0:
SOCKET sock
callback* read_handler // Pointer to callback function
callback* write_handler // Pointer to callback function
Then I could derive from this class to have more specific objects like a Listener class and a Client class. Then using a pointer to the base class I can still call the read/write handlers of all the Listeners and Clients as they are derived from the Socket class. How's that sound?
Another way I thought of doing it is to actually have the read/write handlers as pure virtual methods of the Socket class and implement them as needed in the derived classes (Listener and Client). Then using a pointer to the base class again I can call the appropriate handler function using polymorphism.
I suppose these functions would be called something like
onReadability and onWritability (Like events)
What does everyone think?
Sorry if it's a bit long. Thanks for any help, much appreciated.
Daniel