Hey guys, ya so I've been thinking about this and coming to a blank. I'd appreciate some advice. I've been using Asio for basic HTTP and SMTP. Just a couple hundred lines of usage code. I've been trying to structure it. If I have classes http_server and smtp_server, whats the most professional way to implement that (ie. not copy and pasting them). First I'm thinking they inherit server_base, but then I'm thinking maybe they are typedefs for basic_server<http::accepter, etc>.
I did a bit of googling and ran into libnetpp (which wraps Asio). It uses the last method, has an object to the accepter, and relays the requests. That means it has to re-define every method (link is below.. eg. async_connect would exist in the basic_server AND http::accepter). Is that the best way? I was thinking why wouldn't you just have basic_server inherit public http::accepter so you have everything without having to do extra work. Is it because it has no well-defined interface?
net/client/client.hpp at ab417c151903e2093a0730cd2ea2cea02768caa2 from vinzenz's libnetpp - GitHub
Are the any patterns or methods associated with solving this problem?
Below I'll try and show what I'm thinking simplified..
Code:
template<typename ACCEPTER>
class basic_server
{
public:
void accept() { this->accepter.accept(); }
private:
ACCEPTER accepter;
};
namespace http
{
class accepter
{
public:
void accept() {}
}
typedef basic_server<accepter> server;
}
typedef http::server http_server;
//same thing with smtp
That's basic what I see in other people's code.
Code:
template<typename ACCEPTER>
class basic_server
{
//dont need anything
};
namespace http
{
class accepter
{
public:
void accept() {}
}
typedef basic_server<accepter> server;
}
typedef http::server http_server;
//same thing with smtp
That's what I was thinking about doing. Sucks? Both suck? I dunno.
Sorry I'm kinda tired.. I fell asleep thinking about this. :-/ Thanks!
Edit: connecter didn't make much sense for a server, so I changed it to accepter
Also thought this might make more sense (non-virtual interface pattern or CRTP):
Code:
class server_base
{
public:
void accept() { asio::accept(); this->onAccept(); }
protected:
virtual void onAccept() = 0;
};
namespace http
{
class server : public server_base
{
private:
void onAccept()
{
//do something here
}
}
}
typedef http::server http_server;
//same thing with smtp
I think Python's Twisted library looks sort of like that. That's more for usage code though.