+#if defined _WIN32
+ MaxSetSize = FD_SETSIZE;
+#else
+ struct rlimit limits;
+ if (!getrlimit(RLIMIT_NOFILE, &limits))
+ MaxSetSize = limits.rlim_cur;
+
+#if defined __APPLE__
+ limits.rlim_cur = limits.rlim_max == RLIM_INFINITY ? OPEN_MAX : limits.rlim_max;
+#else
+ limits.rlim_cur = limits.rlim_max;
+#endif
+ if (!setrlimit(RLIMIT_NOFILE, &limits))
+ MaxSetSize = limits.rlim_cur;
+#endif
+}
+
+void SocketEngine::ChangeEventMask(EventHandler* eh, int change)
+{
+ int old_m = eh->event_mask;
+ int new_m = old_m;
+
+ // if we are changing read/write type, remove the previously set bit
+ if (change & FD_WANT_READ_MASK)
+ new_m &= ~FD_WANT_READ_MASK;
+ if (change & FD_WANT_WRITE_MASK)
+ new_m &= ~FD_WANT_WRITE_MASK;
+
+ // if adding a trial read/write, insert it into the set
+ if (change & FD_TRIAL_NOTE_MASK && !(old_m & FD_TRIAL_NOTE_MASK))
+ trials.insert(eh->GetFd());
+
+ new_m |= change;
+ if (new_m == old_m)
+ return;
+
+ eh->event_mask = new_m;
+ OnSetEvent(eh, old_m, new_m);