You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
185 lines
4.5 KiB
185 lines
4.5 KiB
/**
|
|
* \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 <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
#include <poll.h>
|
|
|
|
#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"
|
|
|
|
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);
|
|
TR_PARENTCALL(TR_CommManagerPoll, _this, TR_Class, dtor);
|
|
}
|
|
|
|
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 = POLLIN;
|
|
}
|
|
|
|
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->n_endpoints, timeout);
|
|
|
|
for (i = 0; i < cmgr->n_endpoints; i++) {
|
|
TR_CommEndPoint endpoint = cmgr->endpoints[i];
|
|
|
|
if ((this->fds[i].revents & POLLIN) == POLLIN) {
|
|
nevents--;
|
|
if (TR_INSTANCE_OF(TR_TcpSocket, endpoint->transport)
|
|
&& ((TR_TcpSocket)endpoint->transport)->listen) {
|
|
TR_eventHandlerIssueEvent(
|
|
(TR_EventHandler)this,
|
|
(TR_EventSubject)endpoint,
|
|
TR_CET_EVENT_ACC_READY,
|
|
NULL);
|
|
} else {
|
|
TR_eventHandlerIssueEvent(
|
|
(TR_EventHandler)this,
|
|
(TR_EventSubject)endpoint,
|
|
TR_CEP_EVENT_READ_READY,
|
|
NULL);
|
|
}
|
|
}
|
|
|
|
if ((this->fds[i].revents & POLLOUT) == POLLOUT) {
|
|
nevents--;
|
|
TR_eventHandlerIssueEvent(
|
|
(TR_EventHandler)this,
|
|
(TR_EventSubject)endpoint,
|
|
TR_CEP_EVENT_WRITE_READY,
|
|
NULL);
|
|
}
|
|
|
|
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_commManagerPollDisableWrite(void * _this, TR_Event event)
|
|
{
|
|
TR_CommManagerPoll this = _this;
|
|
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
|
|
|
|
this->fds[endpoint->transport->handle].events &= ~POLLOUT;
|
|
}
|
|
|
|
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_commManagerPollDisableRead(void * _this, TR_Event event)
|
|
{
|
|
TR_CommManagerPoll this = _this;
|
|
TR_CommEndPoint endpoint = (TR_CommEndPoint)event->subject;
|
|
|
|
this->fds[endpoint->transport->handle].events &= ~POLLIN;
|
|
}
|
|
|
|
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_commManagerPollDisableWrite,
|
|
TR_commManagerPollClose,
|
|
TR_commManagerPollDisableRead,
|
|
TR_commManagerPollDisableWrite);
|
|
TR_CREATE_CLASS(
|
|
TR_CommManagerPoll,
|
|
TR_CommManager,
|
|
TR_commManagerPollCvInit,
|
|
TR_IF(TR_Class),
|
|
TR_IF(TR_CommManager));
|
|
|
|
// vim: set ts=4 sw=4:
|