Wisp 1.3.1 C++ Unit Test Coverage
Current view: top level - ssrc/wisp/service/detail - FreeBSDEventPort.h (source / functions) Hit Total Coverage
Test: Wisp 1.3.1 C++ Unit Tests Lines: 8 11 72.7 %
Date: 2017-01-16 Functions: 2 2 100.0 %
Branches: 2 8 25.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     http://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the FreeBSDEventPort class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WISP_SERVICE_DETAIL_FREEBSD_EVENT_PORT_H
      23                 :            : #define __SSRC_WISP_SERVICE_DETAIL_FREEBSD_EVENT_PORT_H
      24                 :            : 
      25                 :            : #include <ssrc/wisp/service/EventHandler.h>
      26                 :            : 
      27                 :            : #include <sys/types.h>
      28                 :            : #include <sys/event.h>
      29                 :            : 
      30                 :            : __BEGIN_NS_SSRC_WISP_SERVICE
      31                 :            : 
      32                 :            : namespace detail {
      33                 :            : 
      34                 :            : // EventHandlers registered with a FreeBSDEventPort are limited to a single
      35                 :            : // event of interest, currently either a Read or a Write, but not both.
      36                 :            : // We'll resolve all the EventPort cross-platform inconsistencies in
      37                 :            : // a future redesign.
      38                 :            : class FreeBSDEventPort {
      39                 :            :   int _kqueue_fd;
      40                 :            : 
      41                 :            : public:
      42                 :            :   typedef struct ::kevent event_type;
      43                 :            :   typedef ::u_short events_type;
      44                 :            : 
      45                 :            :   enum EventIO {
      46                 :            :     None   = 0,
      47                 :            :     /*
      48                 :            :     Read   = EVFILT_READ,
      49                 :            :     Write  = EVFILT_WRITE,
      50                 :            :     */
      51                 :            :     // We translate Read to EVFILT_READ and Write to EVFILT_WRITE in add()
      52                 :            :     Read   = EV_ADD,
      53                 :            :     Write  = EV_DELETE,
      54                 :            :     Error  = EV_ERROR,
      55                 :            :     Hangup = EV_EOF
      56                 :            :     /*
      57                 :            :     Signal = EV_SIGNAL
      58                 :            :     */
      59                 :            :   };
      60                 :            : 
      61                 :          6 :   static EventHandler * get_handler(event_type & e) {
      62                 :          6 :     return static_cast<EventHandler *>(e.udata);
      63                 :            :   }
      64                 :            : 
      65                 :          6 :   static events_type get_events(const event_type & e) {
      66                 :          6 :     events_type result = 0;
      67                 :            : 
      68   [ -  +  #  # ]:          6 :     if(e.flags != 0 && !(e.flags & EV_ONESHOT)) {
      69                 :          0 :       result |= e.flags;
      70                 :            :     }
      71                 :            : 
      72         [ +  - ]:          6 :     if(e.filter == EVFILT_READ) {
      73                 :          6 :       result |= Read;
      74         [ #  # ]:          0 :     } else if(e.filter == EVFILT_WRITE) {
      75                 :          0 :       result |= Write;
      76                 :            :     }
      77                 :            : 
      78                 :          6 :     return result;
      79                 :            :   }
      80                 :            : 
      81                 :            :   FreeBSDEventPort() SSRC_DECL_THROW(std::runtime_error);
      82                 :            : 
      83                 :            :   ~FreeBSDEventPort();
      84                 :            : 
      85                 :            :   bool add(int fd, const events_type events, void *user_data, const bool once)
      86                 :            :     SSRC_DECL_THROW(std::runtime_error);
      87                 :            : 
      88                 :            :   bool remove(int fd, events_type events) SSRC_DECL_THROW(std::runtime_error);
      89                 :            : 
      90                 :            :   int wait(event_type *events, int max_events, int timeout);
      91                 :            : };
      92                 :            : 
      93                 :            : }
      94                 :            : 
      95                 :            : __END_NS_SSRC_WISP_SERVICE
      96                 :            : 
      97                 :            : #endif