comm_manager_poll.c 5.25 KB
/**
 * \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"
#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);
	TR_PARENTCALL(TR_CommManagerPoll, _this, TR_Class, dtor);
}

static
TR_EventDone
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;

	return TR_EVENT_DONE;
}

static
TR_EventDone
TR_commManagerPollSelect(void * _this, TR_Event event, unsigned long 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_socketFdGetter(endpoint->transport)) {
						TR_setAdd(cmgr->accept, endpoint);
					} else {
						if (! event->subject->fin) {
							TR_setAdd(cmgr->read, endpoint);
						}
					}
					this->fds[endpoint->transport->handle].events &= ~POLLIN;
				}

				if ((this->fds[i].revents & POLLOUT) == POLLOUT) {
					if (! event->subject->fin) {
						TR_setAdd(cmgr->write, endpoint);
					}
					this->fds[endpoint->transport->handle].events &=
						~(POLLOUT|POLLHUP);
				}

				if ((this->fds[i].revents & POLLHUP) == POLLHUP) {
					TR_eventHandlerIssueEvent(
							(TR_EventHandler)_this,
							TR_eventSubjectEmit(
								(TR_EventSubject)endpoint,
								TR_CEP_EVENT_SHUT_WRITE,
								NULL));
				}

				this->fds[i].revents = 0;
				if (nevents <= 0) break;
			}
		}
	}

	return TR_EVENT_DONE;
}

static
TR_EventDone
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|POLLHUP;
	}

	return TR_EVENT_DONE;
}

static
TR_EventDone
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;
	}

	return TR_EVENT_DONE;
}

static
TR_EventDone
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|POLLHUP);

	return TR_EVENT_DONE;
}

static
TR_EventDone
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;

	return TR_EVENT_DONE;
}

static
TR_EventDone
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;

	return TR_EVENT_DONE;
}

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_commManagerPollDisableWrite,
		TR_commManagerPollDisableRead,
		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: