Software and Services, Portable Libraries  2019.Mar.01
A library for managing digital certificates
Poll.hpp
Go to the documentation of this file.
1 //
2 //
3 // BEGIN_SOFTWARE_AND_SERVICES_COPYRIGHT
4 //
5 // Copyright (C) 2010-2015,2018 Software and Services LLC, All Rights Reserved.
6 //
7 // NOTICE:
8 //
9 // THIS SOURCE CODE BELONGS TO Software and Services LLC.
10 // THIS CODE IS PROPERTY OF THE COPYRIGHT HOLDER AND IS NOT TO
11 // BE DIVULGED OR USED BY PARTIES WHO HAVE NOT RECEIVED WRITTEN
12 // AUTHORIZATION FROM THE OWNER.
13 //
14 // THIS SOFTWARE AND MANUAL ARE BOTH PROTECTED BY UNITED STATES
15 // COPYRIGHT LAW (TITLE 17 UNITED STATES CODE). UNAUTHORIZED REPRODUCTION
16 // AND/OR SALE MAY RESULT IN IMPRISONMENT OF UP TO ONE YEAR AND FINES
17 // OF UP TO $100,000 (17 USC 506). COPYRIGHT INFRINGERS MAY ALSO
18 // BE SUBJECT TO CIVIL LIABILITY.
19 //
20 // THIS NOTICE AND THE ABOVE COPYRIGHT NOTICE MUST REMAIN IN ALL
21 // COPIES AND ANY DERIVED WORK.
22 //
23 // ALL OF THIS CODE IS ORIGINAL WORK BY THE COPYRIGHT HOLDER.
24 //
25 // THIS DOCUMENT AND THE INFORMATION CONTAINED HEREIN IS
26 // PROVIDED ON AN "AS IS" BASIS AND Software and Services LLC.
27 // DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT
28 // NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
29 // HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED
30 // WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR
31 // PURPOSE.
32 //
33 // END_SOFTWARE_AND_SERVICES_COPYRIGHT
34 //
72 #ifndef SOFTWARE_AND_SERVICES_LIBRARY_COMMON_POLL_HPP_
73 #define SOFTWARE_AND_SERVICES_LIBRARY_COMMON_POLL_HPP_
74 
75 #ifdef WIN32
76 #pragma warning (disable : 4251)
77 #endif
78 
79 #ifdef BUILDING_SAS_COMMON_LIBRARY
80 #include "osver.h"
81 #include "instance.hpp"
82 #include "Network.hpp"
83 #include "File.hpp"
84 #else
85 #include <SaS/Common/osver.h>
86 #include <SaS/Common/instance.hpp>
87 #include <SaS/Common/Network.hpp>
88 #include <SaS/Common/File.hpp>
89 #endif
90 
91 #ifndef EXPORT_COMMON
92 #ifdef BUILDING_SAS_COMMON_LIBRARY
93 #define EXPORT_COMMON EXPORT_OUT
94 #else
95 #define EXPORT_COMMON EXPORT_IN
96 #endif
97 #endif
98 
99 
100 #ifdef WIN32
101 #ifdef BUILDING_SAS_COMMON_LIBRARY
102 #include "poll.h"
103 #else
104 #include <SaS/Common/poll.h>
105 #endif
106 #else
107 #include <poll.h>
108 #endif
109 
110 #ifdef WIN32
111 typedef unsigned int nfds_t;
112 typedef unsigned int uid_t;
113 #ifdef BUILDING_SAS_COMMON_LIBRARY
114 #include "TmpFile.hpp"
115 #else
116 #include <SaS/Common/TmpFile.hpp>
117 #endif
118 #endif
119 
120 #ifndef S_IFIFO
121 #ifdef _IFIFO
122 #define S_IFIFO _IFIFO
123 #endif
124 #endif
125 
126 #include <semaphore.h>
127 #include <deque>
128 #include <map>
129 #include <vector>
130 #include <pthread.h>
131 
132 namespace SoftwareAndServices
133 {
134  namespace Library
135  {
136  namespace Common
137  {
138 
139 #ifdef WINDOWS
140 
150  extern "C" void * _WakeUpThread(void * AnyValue);
151 #endif
152 
160 #define POLL_ACCEPT_FD 0x1000
161 
237  class EXPORT_COMMON Poll
238  : virtual public instance<Poll>
239  {
240 
241  public:
242  class CallBacks;
243 
267  EXPORT_COMMON typedef int
268  (*PollFunc_t)(const int fd,
269  const unsigned short revents,
271  const unsigned int len,
272  const char * data);
273 
274 
278  friend void AtExitCleanup();
279 
283  Poll();
284 
288  virtual ~Poll();
289 
299  File * GetFile(const int fd);
300 
308  bool RemoveFd(const int fd);
309 
313  const struct pollfd * PollFd() const;
314 
318  nfds_t PollFdSize() const;
319 
327  int Wait(bool Once = false);
328 
332  void SetTimeout(const int milliseconds);
333 
345  void
346  SetTransportDefaultCallback(const unsigned short events,
347  const PollFunc_t ThePollFunc,
348  const unsigned int bufSize);
349 
362  PollFunc_t
363  GetTransportDefaultCallback(const unsigned short events) const;
364 
375  void SetUserDefaultCallback(const unsigned short events,
376  const PollFunc_t ThePollFunc,
377  const unsigned int bufSize = 0);
378 
391  PollFunc_t GetUserDefaultCallback(const unsigned short events)
392  const;
393 
404  void
405  SetUserAcceptCallback(const PollFunc_t ThePollFunc,
406  const void * userData = nullptr);
407 
411  PollFunc_t GetUserAcceptCallback() const;
412 
423  void
424  SetTransportAcceptCallback(const PollFunc_t ThePollFunc,
425  const void * userData = nullptr);
426 
430  PollFunc_t GetTransportAcceptCallback() const;
431 
446  void SetTransportCallback(const int fd,
447  const unsigned short events,
448  const PollFunc_t ThePollFunc,
449  const unsigned int bufSize);
450 
464  void SetUserCallback(const int fd,
465  const unsigned short events,
466  const PollFunc_t ThePollFunc,
467  const unsigned int bufSize);
468 
476  PollFunc_t
477  GetTransportCallback(const int fd,
478  const unsigned short events) const;
479 
492  PollFunc_t GetUserCallback(const int Fd,
493  unsigned short Events) const;
494 
498  void SetUserData(const void * Data);
499 
506  void SetUserData(int fd, const void * Data);
507 
511  void * GetUserData() const;
512 
519  static void * GetUserData(int fd);
520 
524  void SetTransportData(const void * Data);
525 
529  void * GetTransportData() const;
530 
545  void
546  SetTransportFillRdCallback(const int fd,
547  const unsigned short events,
548  const PollFunc_t ThePollFunc,
549  const unsigned int bufsize);
550 
566  void SetUserFillRdCallback(const int fd,
567  const unsigned short events,
568  const PollFunc_t ThePollFunc,
569  const unsigned int bufsize);
570 
578  void
579  TransportDisable(const int fd, const unsigned short events);
580 
588  void UserDisable(const int fd,
589  const unsigned short events);
590 
598  void Disable(const int fd, const unsigned short events);
599 
607  void
608  TransportEnable(const int fd, const unsigned short events);
609 
617  void UserEnable(const int fd,
618  const unsigned short events);
619 
627  void Enable(const int fd, const unsigned short events);
628 
634  inline void Lock()
635  {
636  pthread_mutex_lock(_ModifyMutex);
637  }
638 
644  inline void Unlock()
645  {
646  pthread_mutex_unlock(_ModifyMutex);
647  }
648 
652  struct EXPORT_COMMON CallBacks
653  : public instance<CallBacks>
654  {
655 
659  CallBacks();
660 
666  CallBacks(const CallBacks * Other);
667 
671  virtual ~CallBacks();
672 
677  struct EXPORT_COMMON CbData
678  : public instance<CbData>
679  {
680 
686  CbData(const PollFunc_t func);
687 
691  virtual ~CbData();
692 
696  PollFunc_t Cb;
697 
701  unsigned int Size;
702 
706  char * Buf;
707 
711  bool IsMMapped;
712  };
713 
717  unsigned short Events;
718 
727  PollFunc_t AcceptCb;
728 
733 
738 
743 
748 
753 
758 
763 
768 
773 
778 
784  PollFunc_t GetFunc(const unsigned short event) const;
785  };
786 
798  bool AddFd(const int fd,
799  const unsigned short events,
800  const CallBacks * cb = nullptr);
801 
807  void CloseDown();
808 
809 #ifdef WINDOWS
810 
813  sem_t & WakeUpReady();
814 
820  const int WakeUpIO(int TheIO);
821 
827  const int WakeUpIO() const;
828 
834  const short WakeUpPort(const short TheIOPort);
835 
841  const short WakeUpPort() const;
842 
843 #endif
844 
861  int WakeUpFd() const;
862 
868  CallBacks * GetUserCallbacks() const;
869 
875  CallBacks * GetTransportCallbacks() const;
876 
877  private:
878 
883  void _WakeUpPoll();
884 
889  bool _MakeFifo(const bool remove = false);
890 
894  void _AtExitCleanup();
895 
899  bool _Done; // Time to exit ... forever.
900 
905  pthread_mutex_t * _ModifyMutex;
906 
910  pthread_mutex_t * _PausePoll;
911 
915  nfds_t _NumberInList;
916 
920  nfds_t _SizeOfPollFdList;
921 
925  int _MillisecondsTimeout;
926 
927 #ifndef WINDOWS
928 
931  int _WakeUpIO;
932 
936  short _WakeUpPort;
937 
942 #else
943 
946 #endif
947  int _WakeUpFd;
948 
952  pthread_t _WakeUpTid;
953 
954  // The thread that is spun off to do poll wake up.
955  //
956  friend void * _WakeUpThread(void *);
957 
961  std::vector<int> * _AcceptFds;
962 
968  void _AddAcceptFd(int fd);
969 
975  bool _IsAcceptFd(int fd);
980  unsigned long _MaxBufSize;
981 
985  struct pollfd * _PollFdList;
986 
990  CallBacks ** _TpollCallBacks;
991 
995  CallBacks ** _UpollCallBacks;
996 
1000  File ** _FileList;
1001 
1005  const void * _Default_UserData; // User.
1006 
1010  const void * _T_UserData; // Transport.
1011 
1015  CallBacks * _TdefaultCb;
1016 
1020  CallBacks * _UdefaultCb;
1021 
1025  void _MakePollFdList();
1026 
1030  pid_t _Pid;
1031 
1035  pthread_t _Tid;
1036 
1040  uid_t _Uid;
1041 
1046  sem_t _WakeUpReady;
1047 
1051  std::deque<int> * _ToRemove;
1052 
1056  void _RemoveFd();
1057 
1061  static std::map<int, void*> * _UserData;
1062  };
1063 
1064  }
1065  }
1066 }
1067 
1068 #endif // SOFTWARE_AND_SERVICES_LIBRARY_COMMON_POLL_HPP_
Like &#39;FILE&#39;, except C++ implementation and allows sockets and disk files to use same object on WINDOW...
Definition: File.hpp:150
Generic buffered IO.
CbData * PollRdBand
The POLLRDBAND callback.
Definition: Poll.hpp:737
CbData * PollPri
The POLLPRI callback.
Definition: Poll.hpp:742
void Unlock()
Unlock this data, changes are complete.
Definition: Poll.hpp:644
PollFunc_t AcceptCb
The Transport callbacks are always called first, then if there is any user callback, they are called.
Definition: Poll.hpp:727
CbData * PollNVal
The POLLNVAL callback.
Definition: Poll.hpp:767
Instance counting and tracking.
Definition: instance.hpp:90
CbData * PollWrBand
The POLLWRBAND callback.
Definition: Poll.hpp:752
An OS independent wrapper for polling I/O connections.
Definition: Poll.hpp:237
CbData * PollOut
The POLLOUT callback.
Definition: Poll.hpp:747
void Lock()
Lock this data so changes can be made.
Definition: Poll.hpp:634
Copyright Douglas Mark Royer DouglasRoyer@gmail.com.
Definition: Base.hpp:98
Base class for instance counting objects.
"<SaS/Common/TmpFile.hpp>"Temporary file management.
Generic network and socket related functions.
File * FilePtr
The IO buffered File pointer.
Definition: Poll.hpp:772
"<SaS/Common/psver.h>" Definitions specific to various OS&#39;s and OS versions.
Poll * PollPtr
Pointer to the Poll object.
Definition: Poll.hpp:777
CbData * PollErr
The POLLERR callback.
Definition: Poll.hpp:757
CbData * PollHup
The POLLHUP callback.
Definition: Poll.hpp:762
unsigned short Events
Enabled events.
Definition: Poll.hpp:717