"Fossies" - the Fresh Open Source Software Archive

Member "ivtools-ivtools-2.0.4/src/AceDispatch/ace_dispatcher.c" (9 Oct 2020, 4498 Bytes) of package /linux/misc/ivtools-ivtools-2.0.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ace_dispatcher.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 1996 Vectaport Inc.
    3  *
    4  * Permission to use, copy, modify, distribute, and sell this software and
    5  * its documentation for any purpose is hereby granted without fee, provided
    6  * that the above copyright notice appear in all copies and that both that
    7  * copyright notice and this permission notice appear in supporting
    8  * documentation, and that the names of the copyright holders not be used in
    9  * advertising or publicity pertaining to distribution of the software
   10  * without specific, written prior permission.  The copyright holders make
   11  * no representations about the suitability of this software for any purpose.
   12  * It is provided "as is" without express or implied warranty.
   13  *
   14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
   15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
   16  * IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL,
   17  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
   18  * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
   19  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
   20  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
   21  * 
   22  */
   23 
   24 #ifdef HAVE_ACE
   25 #ifdef __llvm__
   26 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
   27 #endif
   28 
   29 #include <AceDispatch/ace_dispatcher.h>
   30 #include <AceDispatch/ace_iohandler.h>
   31 #include <ace/Event_Handler.h>
   32 #include <ace/Reactor.h>
   33 
   34 implementTable(TimerTable,IOHandler*,int)
   35 
   36 AceDispatcher::AceDispatcher() : Dispatcher() {
   37     _reactor = new ACE_Reactor();
   38     _table = new TimerTable(1000);
   39 }
   40 
   41 AceDispatcher::AceDispatcher(ACE_Reactor* ar) : Dispatcher() {
   42     _reactor = ar;
   43     _table = new TimerTable(1000);
   44 }
   45 
   46 AceDispatcher::~AceDispatcher() {
   47     delete _reactor;
   48     delete _table;
   49 }
   50 
   51 void AceDispatcher::attach(int fd, DispatcherMask mask, IOHandler* handler) {
   52     if (mask == ReadMask) {
   53       //    _rmask->setBit(fd);
   54         _reactor->register_handler((ACE_HANDLE)fd,
   55                    new ACE_IO_Handler(handler),
   56                    ACE_Event_Handler::READ_MASK);
   57     _rtable[fd] = handler;
   58     } else if (mask == WriteMask) {
   59       //    _wmask->setBit(fd);
   60         _reactor->register_handler((ACE_HANDLE)fd,
   61                    new ACE_IO_Handler(handler),
   62                    ACE_Event_Handler::WRITE_MASK);
   63     _wtable[fd] = handler;
   64     } else if (mask == ExceptMask) {
   65       //    _emask->setBit(fd);
   66         _reactor->register_handler((ACE_HANDLE)fd,
   67                    new ACE_IO_Handler(handler),
   68                    ACE_Event_Handler::EXCEPT_MASK);
   69     _etable[fd] = handler;
   70     } else {
   71     abort();
   72     }
   73     if (_nfds < fd+1) {
   74     _nfds = fd+1;
   75     }
   76 }
   77 
   78 void AceDispatcher::detach(int fd) {
   79   //    _rmask->clrBit(fd);
   80     _reactor->remove_handler((ACE_HANDLE)fd, ACE_Event_Handler::RWE_MASK);
   81     _rtable[fd] = nil;
   82     //    _wmask->clrBit(fd);
   83     _wtable[fd] = nil;
   84     //    _emask->clrBit(fd);
   85     _etable[fd] = nil;
   86     if (_nfds == fd+1) {
   87     while (_nfds > 0 && _rtable[_nfds-1] == nil &&
   88            _wtable[_nfds-1] == nil && _etable[_nfds-1] == nil
   89     ) {
   90         _nfds--;
   91     }
   92     }
   93 }
   94 
   95 void AceDispatcher::startTimer(long sec, long usec, IOHandler* handler) {
   96     ACE_Time_Value delta(sec, usec);
   97     ACE_IO_Handler* ehandler = new ACE_IO_Handler(handler);
   98     int timer_id = _reactor->schedule_timer(ehandler, NULL, delta);
   99     _table->insert(handler, timer_id);
  100 }
  101 
  102 void AceDispatcher::stopTimer(IOHandler* handler) {
  103     int timer_id;
  104     if (_table->find_and_remove(timer_id, handler)) {
  105         _reactor->cancel_timer(timer_id);
  106     }
  107 }
  108 
  109 void AceDispatcher::dispatch() {
  110     _reactor->handle_events();
  111 }
  112 
  113 boolean AceDispatcher::dispatch(long& sec, long& usec) {
  114     ACE_Time_Value* tv = new ACE_Time_Value(sec, usec);
  115 
  116     timeval curTime1;
  117 #if defined(svr4) && !defined(__GNUC__)
  118     gettimeofday(&curTime1);
  119 #else
  120     struct timezone curZone;
  121     gettimeofday(&curTime1, &curZone);
  122 #endif
  123 
  124     int nfound = _reactor->handle_events(tv);
  125 
  126     timeval curTime2;
  127 #if defined(svr4) && !defined(__GNUC__)
  128     gettimeofday(&curTime2);
  129 #else
  130     gettimeofday(&curTime2, &curZone);
  131 #endif
  132 
  133     ACE_Time_Value* t1 = new ACE_Time_Value(curTime1);
  134     ACE_Time_Value* t2 = new ACE_Time_Value(curTime2);
  135     ACE_Time_Value* elapsed = new ACE_Time_Value();
  136     ACE_Time_Value* howlong = new ACE_Time_Value();
  137     *elapsed = *t2 - *t1;
  138     if (*tv > *elapsed) {
  139         *howlong = *tv - *elapsed;
  140     }
  141     sec = howlong->sec();
  142     usec = howlong->usec();
  143 
  144     delete tv;
  145     delete t1;
  146     delete t2;
  147     delete elapsed;
  148     delete howlong;
  149     return nfound > 0;
  150 }
  151 
  152 #endif