/**
* \file
*
* \author Georg Hopp
*
* \copyright
* Copyright © 2014 Georg Hopp
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include
#include
#include "trbase.h"
#include "trevent.h"
#include "tr/comm_manager.h"
#include "tr/comm_manager_poll.h"
#include "tr/interface/comm_manager.h"
#include "tr/comm_end_point.h"
#include "tr/connection.h"
#include "tr/connect_entry_point.h"
#include "tr/_comm_manager.h"
static
int
commManagerPollCtor(void * _this, va_list * params)
{
TR_CommManagerPoll this = _this;
TR_CommManager cmgr = _this;
nfds_t i;
TR_PARENTCALL(TR_CommManagerPoll, _this, TR_Class, ctor, params);
this->fds = TR_malloc(sizeof(struct pollfd) * cmgr->n_endpoints);
for (i = 0; i < cmgr->n_endpoints; i++) {
this->fds[i].fd = -1;
this->fds[i].events = 0;
this->fds[i].revents = 0;
}
return 0;
}
static
void
commManagerPollDtor(void * _this)
{
TR_CommManagerPoll this = _this;
TR_MEM_FREE(this->fds);
}
static
void
TR_commManagerPollAddEndpoint(void * _this, TR_CommEndPoint endpoint)
{
TR_CommManagerPoll this = _this;
this->fds[endpoint->transport->handle].fd = endpoint->transport->handle;
this->fds[endpoint->transport->handle].events = 0;
}
static
void
TR_commManagerPollSelect(void * _this, TR_Event event, int timeout)
{
TR_CommManagerPoll this = _this;
TR_CommManager cmgr = _this;
nfds_t i;
int nevents;
nevents = poll(this->fds, cmgr->max_handle+1, timeout);
if (nevents) {
for (i = 0; i < cmgr->max_handle+1; i++) {
if (this->fds[i].revents != 0) {
TR_CommEndPoint endpoint = cmgr->endpoints[i];
nevents--;
if ((this->fds[i].revents & POLLIN) == POLLIN) {
if (TR_INSTANCE_OF(TR_TcpSocket, endpoint->transport)
&& ((TR_TcpSocket)endpoint->transport)->listen) {
TR_hashAdd(cmgr->accept, endpoint);
} else {
TR_hashAdd(cmgr->read, endpoint);
}
this->fds[endpoint->transport->handle].events &= ~POLLIN;
}
if ((this->fds[i].revents & POLLOUT) == POLLOUT) {
TR_hashAdd(cmgr->write, endpoint);
this->fds[endpoint->transport->handle].events &= ~POLLOUT;
}
if (nevents <= 0) break;
}
}
}
}
static
void
TR_commManagerPollEnableWrite(void * _this, TR_Event event)
{
TR_CommManagerPoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
if (! TR_socketFinWr(endpoint->transport)) {
this->fds[endpoint->transport->handle].events |= POLLOUT;
}
}
static
void
TR_commManagerPollEnableRead(void * _this, TR_Event event)
{
TR_CommManagerPoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
if (! TR_socketFinRd(endpoint->transport)) {
this->fds[endpoint->transport->handle].events |= POLLIN;
}
}
static
void
TR_commManagerPollClose(void * _this, TR_Event event)
{
TR_CommManagerPoll this = _this;
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
this->fds[endpoint->transport->handle].events = 0;
this->fds[endpoint->transport->handle].fd = -1;
}
static
void
TR_commManagerPollCvInit(TR_class_ptr cls) {
TR_INHERIT_CLASSVARS(TR_CommManagerPoll, TR_CommManager);
}
TR_INIT_IFACE(TR_Class, commManagerPollCtor, commManagerPollDtor, NULL);
TR_INIT_IFACE(
TR_CommManager,
TR_commManagerPollAddEndpoint,
TR_commManagerPollSelect,
TR_commManagerPollEnableWrite,
TR_commManagerPollEnableRead,
TR_commManagerPollClose);
TR_CREATE_CLASS(
TR_CommManagerPoll,
TR_CommManager,
TR_commManagerPollCvInit,
TR_IF(TR_Class),
TR_IF(TR_CommManager));
// vim: set ts=4 sw=4: