I'm trying to find better methods to handle the events of a polled socket (fd). I'm stuck on this to exploit the poll()
function with a different pollfd
like this:
#define out std::cout
#define el std::endl
#define line(string) out<<string<<el
class int_t
{
private:
int _int;
int _owner;
public:
operator int() const;
int_t();
int_t(const int& in);
int_t& operator=(const int& in);
int_t operator|(const int& in) const;
int_t operator&(const int& in) const;
/*
...
...
*/
void setowner(const int& fd);
~int_t();
};
int_t::operator int() const
{
return this->_int;
}
int_t::int_t()
{
this->_int = 0;
this->_owner = 0;
}
int_t::int_t(const int& in)
{
this->_int = in;
this->_owner = 0;
}
int_t& int_t::operator=(const int& in)
{
line("operator '=' called"<<" owner:"<<this->_owner);
this->_int = in;
return *this;
}
int_t int_t::operator|(const int& in) const
{
line("operator '|' called"<<" owner:"<<this->_owner);
return (this->_int|in);
}
int_t int_t::operator&(const int& in) const
{
line("operator '&' with arg called"<<" owner:"<<this->_owner);
return (this->_int&in);
}
/*
...
...
*/
void int_t::setowner(const int& fd)
{
this->_owner = fd;
}
int_t::~int_t()
{
this->_int = 0;
}
struct pollfd_other
{
// Valgrind returns me an error when i changing the type of the `revent` only
// but when i changing the type of the `event` and `revent` works without error
// and `poll()` gets the flags normally from the `event` if ill put for example
// a `POLLIN` flag.
int fd;
int_t events;
int_t revents;
void setowner(const int& pollfdowner){
this->fd = pollfdowner;
this->revents.setowner(pollfdowner);
}
};
int main(int argc,char* argv[])
{
Server server;
pollfd_other pfd[1];
pfd[0].setowner(server.socket);
::poll(reinterpret_cast<pollfd*>(&pfd),1,-1);
// ...
}
The class int_t
works very well as an integer and struct pollfd_other
... but the poll(开发者_高级运维)
doesn't access it like a class to invoke the operators ...
revents
member of the struct pollfd_other
.
There is any other method to do something like this? Suggestions are welcomed ...That looks very convoluted. If you want to encapsulate the poll functionality in an object, don't encapsulate an int, encapsulate the poll. Something like this:
class poller_callback {
public:
void handle_pollin(int fd) = 0;
};
class poller {
public:
poller(const vector<int>& fds);
void do_poll(const poller_callback& c)
{
// poll(&pfd, ...);
if(fd[i].revents & POLLIN) {
c.handle_pollin(fd[i].fd);
}
}
};
int main(void)
{
my_poller_callback my_handler;
// get fds
poller my_poller(fds);
do_poll(my_handler);
// ...
return 0;
}
This is how I'd do it. Have a class that encapsulates poll()
, which is parameterized on what it should do on events. poller_callback
is an interface for objects that handle events. You can then write my_poller_callback
which, in its handle_pollin
, creates the thread.
On Linux I recommend epoll
(http://kovyrin.net/2006/04/13/epoll-asynchronous-network-programming/, manual pages). It's much faster then poll
/select
and easier to use becasue you can asign pointer for each handled descriptor.
精彩评论