File indexing completed on 2024-05-05 17:50:04

0001 /*
0002    Copyright (C) 2015 Andreas Hartmetz <ahartmetz@gmail.com>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LGPL.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017    Boston, MA 02110-1301, USA.
0018 
0019    Alternatively, this file is available under the Mozilla Public License
0020    Version 1.1.  You may obtain a copy of the License at
0021    http://www.mozilla.org/MPL/
0022 */
0023 
0024 #ifndef SELECTEVENTPOLLER_H
0025 #define SELECTEVENTPOLLER_H
0026 
0027 #include "ieventpoller.h"
0028 
0029 #include <unordered_map>
0030 
0031 #define WIN32_LEAN_AND_MEAN
0032 #include <windows.h>
0033 #ifdef FD_SETSIZE
0034 #error We must be able to set FD_SETSIZE - make sure that nothing else sets it!
0035 #endif
0036 #define FD_SETSIZE 1024
0037 #include <winsock2.h>
0038 
0039 // Windows select() diverges from "proper Unix" select() just enough to seriously hurt readability
0040 // when handling the differences with ifdefs, so use a separate implementation.
0041 // Besides, the fd_set from winsock2.h is actually an array of sockets (not a bitmap like on Unix), which
0042 // can be exploited to achive poll()-like performance characteristics without dealing with the problems
0043 // that WSAPoll() has. That is currently not implemented.
0044 
0045 class SelectEventPoller : public IEventPoller
0046 {
0047 public:
0048     SelectEventPoller(EventDispatcher *dispatcher);
0049     ~SelectEventPoller();
0050     IEventPoller::InterruptAction poll(int timeout) override;
0051     void interrupt(IEventPoller::InterruptAction) override;
0052 
0053     // reimplemented from IEventPoller
0054     void addFileDescriptor(FileDescriptor fd, uint32 ioRw) override;
0055     void removeFileDescriptor(FileDescriptor fd) override;
0056     void setReadWriteInterest(FileDescriptor fd, uint32 ioRw) override;
0057 
0058 private:
0059     void notifyRead(int fd);
0060     void resetFdSets();
0061 
0062     std::unordered_map<FileDescriptor, uint32 /*ioRw*/> m_fds;
0063 
0064     fd_set m_readSet;
0065     fd_set m_writeSet;
0066 
0067     FileDescriptor m_interruptSocket[2];
0068 };
0069 
0070 #endif // SELECTEVENTPOLLER_H