Re: Implementing call back functions effeciantly
"Chris Thomasson" <xxx@xxx.xxx> wrote in message
news:g5mtjp$eb$1@aioe.org...
"shiva" <dudeofindia@gmail.com> wrote in message
news:12ed3f3f-251d-4765-a60a-29f38cc1146e@56g2000hsm.googlegroups.com...
Hi All..
I am a novice to C++ and working on one networking project.
In which, some of my application layer functions should be executed
when some event occur in network.
For this, i thought of using callback functions concept.
is it the best way? It will be helpful, if some of you suggest best
way for such implementation.
If callback concept is best, how i use this efficiently.
it will be helpful, if ypu provide some good URL to study.
[...]
Here is a VERY simple and NAIVE approach that you can try:
[...]>
A simple echo server might look like:
class My_Echo_Server : public Network_Connection_Callbacks {
void On_Connect(Network_Connection& this_connection) {
this_connection.Send(7, "Repeat");
}
void On_Disconnect(Network_Connection& this_connection) {
[Handle the disconnection...];
}
void On_Send(Network_Connection& this_connection, std::size_t Size, void*
Buf) {
this_connection.Recv(Size, buf);
}
void On_Recv(Network_Connection& this_connection, std::size_t Size, void*
Buf) {
this_connection.Send(Size, Buf);
}
};
Ummm, the above would be an echo client NOT server! Sorry for any
confusions.
;^(...
To build servers you would need to add at least one other callback to the
`Network_Connection_Callbacks' function:
class Network_Connection_Callbacks {
virtual void On_Accept(Network_Connection&) = 0;
virtual void On_Connect(Network_Connection&) = 0;
virtual void On_Disconnect(Network_Connection&) = 0;
virtual void On_Send(Network_Connection&, std::size_t, void*) = 0;
virtual void On_Recv(Network_Connection&, std::size_t, void*) = 0;
public:
virtual ~Network_Connection_Callbacks() = 0;
};
Network_Connection_Callbacks::~Network_Connection_Callbacks() {}
such that `On_Accept' gets fired when a new connection is accepted by the
server. BTW, here is a fairly clean server framework (Windows Sockets under
IOCP) w/ full C++ source-code you can take a look at:
http://www.lenholgate.com/archives/000637.html
[...]
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]