120 lines
3.2 KiB
C++
120 lines
3.2 KiB
C++
/*
|
|
* PVVMUD a 3D MUD
|
|
* Copyright (C) 1998-1999 Programvareverkstedet (pvv@pvv.org)
|
|
*
|
|
* 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 2 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, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
*
|
|
*/
|
|
#ifndef _TIMEKEEPER_H
|
|
#define _TIMEKEEPER_H
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
// BUGS + KNOWN PROBLEMS
|
|
//
|
|
// When using TIMEKEEPER_OUT to notify a write queue, poll never suspend
|
|
// when idle. Have to create some functions that can controll this flag
|
|
// and remove TIMEKEEPER_OUT when not needed.
|
|
//
|
|
|
|
#include <poll.h>
|
|
#include <sys/time.h>
|
|
#include "object.H"
|
|
|
|
// valid events:
|
|
// #define TIMEKEEPER_PRI POLLPRI
|
|
#define TIMEKEEPER_IN (POLLIN | POLLPRI)
|
|
#define TIMEKEEPER_OUT POLLOUT
|
|
#define TIMEKEEPER_ERR POLLERR
|
|
#define TIMEKEEPER_HUP POLLHUP
|
|
#define TIMEKEEPER_NVAL POLLNVAL
|
|
|
|
class CTimeKeeper;
|
|
|
|
class CTimeKeeperItem : public CObject {
|
|
CTimeKeeper * m_timeKeeper;
|
|
public:
|
|
void timeKeeperSetTimeKeeper(CTimeKeeper * timeKeeper){ m_timeKeeper = timeKeeper; }
|
|
virtual int timeKeeperFD(int event);
|
|
int timeKeeperSetEvents(int event);
|
|
virtual int timeKeeperHB();
|
|
};
|
|
|
|
struct heartbeat {
|
|
CTimeKeeperItem * fn;
|
|
int timeout; /* millisecounds */
|
|
};
|
|
|
|
class CTimeKeeper {
|
|
struct heartbeat *hbs;
|
|
int num_hbs, size_hbs;
|
|
|
|
int mintimeout;
|
|
int num;
|
|
|
|
struct pollfd *fds;
|
|
CTimeKeeperItem ** timeKeeperFD;
|
|
int num_fdns, size_fds;
|
|
//, size_fns;
|
|
|
|
int running;
|
|
struct timeval lasttime;
|
|
|
|
void setTime();
|
|
int getTime();
|
|
|
|
public:
|
|
|
|
CTimeKeeper();
|
|
~CTimeKeeper();
|
|
|
|
/* Listen for events on fd, and call with the pending events
|
|
as parameter. Returns 1 successfull, 0 othervise. */
|
|
int addFd(int fd, CTimeKeeperItem * fn, short events);
|
|
|
|
/* Do not listen for events on fd.
|
|
Returns 1 successfull, 0 othervise. */
|
|
int rmFd(int fd);
|
|
|
|
int setEvents(CTimeKeeperItem * fn,short events);
|
|
|
|
/* Add heartbeat function fn, to be run every timeout millisecounds.
|
|
timeout is rounded down to nearest 2^n.
|
|
Returns 1 successfull, 0 othervise. */
|
|
int addHeartBeat(int timeout, CTimeKeeperItem * fn);
|
|
|
|
/* Removes heartbeat function fn.
|
|
Returns 1 successfull, 0 othervise. */
|
|
int rmHeartBeat(CTimeKeeperItem * fn);
|
|
|
|
/* Run the mainLoop. Listening for events on registered fds, and
|
|
running heartbeatfunctions. */
|
|
void mainLoop();
|
|
|
|
/* If running the mainLoop isn't possible. Poll for events on registered fds, and
|
|
running heartbeatfunctions. */
|
|
void poll(int timeOut);
|
|
|
|
/* Stop the Loop now. */
|
|
void stopLoop();
|
|
};
|
|
|
|
|
|
|
|
#endif // _TIMEKEEPER_H
|
|
|
|
|
|
|
|
|