# iomux

This class is typically allocated by the platform specific main function and then passed to the application classes and all interface modules for initialization and then the Run function is called for the wait on filedescriptors.

Members:

static IIoMux * Create()
  Allocates the IoMux class
void Run()
  Called after the initialization to wait for events (epoll)
void Terminate()
  Called to terminate the Run() loop of IoMux to terminate the main function.
void FDAdd(int fd, UIoContext * context, bool pollOut)
  Add a filedescriptor to wait for. If the filedescriptor becomes ready for read/write the UIoContext::IoContextNotify function is called. If pollOut is true, not only ready for read, but also ready for write triggers the Notify.
void FDClose(UIoContext * context)
  Removes a filedescriptor to wait on and calls close(fd).
void FDRem(UIoContext * context)
  Removes a filedescriptor to wait on without closing the fd.
void FDUnblock(int fd)
  Unblocks a file descriptor.
void bool CanAcceptFD()
  Returns true if the maximum number of file descriptors is not yet reached.
void Lock(), void UnLock()
  Used for the support of interfaces, which do not support asynchronous operation. These typically start one and more threads for the I/O. All callbacks to the application need to be synchronized with the application thread by these functions.
void SetExec(UIoContext * ioContext, void * execContext)
  Destined for construction of asynchronous callback functions that must not interfere with invocations of corresponding interface functions. '''Requirement:''' A class derived from ''UIoContext'' and being passed into SetExec() must implement ''void UIoContext::IoExec(void * execContext)''
void SetExecLocked(UIoContext * ioContext, void * execContext)
  Synchronizes with the application thread and asserts SetExec(). This might be called from a different thread to schedule a callback.
void CancelSetExec(UIoExec * const ioContext)
  Cancels all outstanding exec calls for the given ioContext.
void RegisterSignalConsumer(ISignalConsumer * consumer)
  Registers a class derived from ISignalConsumer to receive OnSignal(int signr) callbacks.
  Currently only SIGHUP is signaled.
void RegisterShutdownHandler(IShutdownHandler * shutdownHandler)
  Registers a class derived from IShutdownHandler to receive Shutdown() callbacks. A Shutdown() callback is triggerd on SIGTERM, SIGINT and SIGKILL.
  In case that IShutdownHandlers are registered, these handlers have to call iomux->Terminate() when they have finished their shutdown handling!
void RegisterChildExitedHandler(IChildExitedHandler * childExitedHandler)
  Registers a class derived from IChildExitedHandler to receive ChildExited(signed int pid, int signr, int exitCode, bool sigFault) callbacks.
void UnRegisterChildExitedHandler(IChildExitedHandler * childExitedHandler)
  Unregisters a class derived from IChildExitedHandler which then doesn't receive these callbacks anymore.
void PrintBacktrace()
  Prints the current backtrace to the standard logfile.

## ITimer

Timer implementation, resolution in milliseconds.

Pattern example:
  - Class ''LdapServerConn'' contains a timer(timerIdle) and is derived from UTimer.`

        class LdapServerConn : public UTimer {

            ITimer       timerIdle;
            void TimerOnTimeout(ITimer * timer);
            ..
        };

    `
  - Within the constructor of LdapServerConn ioMux will be passed into the constructor of timerIdle`

        LdapServerConn::LdapServerConn(IIoMux * ioMux, ISockContext * srvContext, LdapSrv * ldapSrv) :
            timerIdle(ioMux, this)
        {
            ..
        }

    `
  - Start of an idle-timer with 30 seconds. Purpose: after 30s of „silence“ a timeout shall trigger.`

        void LdapServerConn::SockContextReceive(void * data, unsigned len)
        {
            this->timerIdle.Start(30000);
            ..
        }

    `
  - Timer-Routine: The affected timer is ruled-out means of an address check(necessary only in case of multiple timers).`

        void LdapServerConn::TimerOnTimeout(ITimer * timer)
        {
            if (timer == &this->timerIdle) {
                this->Release();
            }
        }

    `

## ITimerAbsolute

Timer implementation, which takes an absolute timestamp to trigger. Note that this version uses system timers from the Linux OS, so use them carefully. ITImerAbsolute is used in the same way as ITimer (except that the user for callback is UTimerAbsolute).