| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
#ifdef OS_WIN32 |
|---|
| 23 |
#include "emwin.h" |
|---|
| 24 |
#endif |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 |
#ifndef __EventMachine__H_ |
|---|
| 31 |
#define __EventMachine__H_ |
|---|
| 32 |
|
|---|
| 33 |
#ifdef BUILD_FOR_RUBY |
|---|
| 34 |
#include <ruby.h> |
|---|
| 35 |
#define EmSelect rb_thread_select |
|---|
| 36 |
#else |
|---|
| 37 |
#define EmSelect select |
|---|
| 38 |
#endif |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
#ifdef OS_UNIX |
|---|
| 42 |
typedef long long Int64; |
|---|
| 43 |
#endif |
|---|
| 44 |
#ifdef OS_WIN32 |
|---|
| 45 |
typedef __int64 Int64; |
|---|
| 46 |
#endif |
|---|
| 47 |
|
|---|
| 48 |
extern time_t gCurrentLoopTime; |
|---|
| 49 |
|
|---|
| 50 |
class EventableDescriptor; |
|---|
| 51 |
|
|---|
| 52 |
|
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
class EventMachine_t |
|---|
| 58 |
{ |
|---|
| 59 |
public: |
|---|
| 60 |
static void SetMaxTimerCount (int); |
|---|
| 61 |
|
|---|
| 62 |
public: |
|---|
| 63 |
EventMachine_t (void(*event_callback)(const char*, int, const char*, int)); |
|---|
| 64 |
virtual ~EventMachine_t(); |
|---|
| 65 |
|
|---|
| 66 |
void Run(); |
|---|
| 67 |
void ScheduleHalt(); |
|---|
| 68 |
void SignalLoopBreaker(); |
|---|
| 69 |
const char *InstallOneshotTimer (int); |
|---|
| 70 |
const char *ConnectToServer (const char *, int); |
|---|
| 71 |
const char *ConnectToUnixServer (const char *); |
|---|
| 72 |
const char *CreateTcpServer (const char *, int); |
|---|
| 73 |
const char *OpenDatagramSocket (const char *, int); |
|---|
| 74 |
const char *CreateUnixDomainServer (const char*); |
|---|
| 75 |
const char *_OpenFileForWriting (const char*); |
|---|
| 76 |
const char *OpenKeyboard(); |
|---|
| 77 |
|
|---|
| 78 |
const char *Socketpair (char* const*); |
|---|
| 79 |
|
|---|
| 80 |
void Add (EventableDescriptor*); |
|---|
| 81 |
void Modify (EventableDescriptor*); |
|---|
| 82 |
void ArmKqueueWriter (EventableDescriptor*); |
|---|
| 83 |
void ArmKqueueReader (EventableDescriptor*); |
|---|
| 84 |
|
|---|
| 85 |
void SetTimerQuantum (int); |
|---|
| 86 |
static void SetuidString (const char*); |
|---|
| 87 |
static int SetRlimitNofile (int); |
|---|
| 88 |
|
|---|
| 89 |
int SubprocessExitStatus; |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
void _UseEpoll(); |
|---|
| 93 |
void _UseKqueue(); |
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 |
private: |
|---|
| 97 |
bool _RunOnce(); |
|---|
| 98 |
bool _RunTimers(); |
|---|
| 99 |
void _AddNewDescriptors(); |
|---|
| 100 |
void _ModifyDescriptors(); |
|---|
| 101 |
void _InitializeLoopBreaker(); |
|---|
| 102 |
|
|---|
| 103 |
bool _RunSelectOnce(); |
|---|
| 104 |
bool _RunEpollOnce(); |
|---|
| 105 |
bool _RunKqueueOnce(); |
|---|
| 106 |
|
|---|
| 107 |
void _ModifyEpollEvent (EventableDescriptor*); |
|---|
| 108 |
|
|---|
| 109 |
public: |
|---|
| 110 |
void _ReadLoopBreaker(); |
|---|
| 111 |
|
|---|
| 112 |
private: |
|---|
| 113 |
enum { |
|---|
| 114 |
HeartbeatInterval = 2, |
|---|
| 115 |
MaxEpollDescriptors = 64*1024 |
|---|
| 116 |
}; |
|---|
| 117 |
void (*EventCallback)(const char*, int, const char*, int); |
|---|
| 118 |
|
|---|
| 119 |
class Timer_t: public Bindable_t { |
|---|
| 120 |
}; |
|---|
| 121 |
|
|---|
| 122 |
multimap<Int64, Timer_t> Timers; |
|---|
| 123 |
vector<EventableDescriptor*> Descriptors; |
|---|
| 124 |
vector<EventableDescriptor*> NewDescriptors; |
|---|
| 125 |
set<EventableDescriptor*> ModifiedDescriptors; |
|---|
| 126 |
|
|---|
| 127 |
time_t NextHeartbeatTime; |
|---|
| 128 |
|
|---|
| 129 |
int LoopBreakerReader; |
|---|
| 130 |
int LoopBreakerWriter; |
|---|
| 131 |
#ifdef OS_WIN32 |
|---|
| 132 |
struct sockaddr_in LoopBreakerTarget; |
|---|
| 133 |
#endif |
|---|
| 134 |
|
|---|
| 135 |
timeval Quantum; |
|---|
| 136 |
|
|---|
| 137 |
private: |
|---|
| 138 |
bool bEpoll; |
|---|
| 139 |
int epfd; |
|---|
| 140 |
|
|---|
| 141 |
bool bKqueue; |
|---|
| 142 |
int kqfd; |
|---|
| 143 |
}; |
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
struct SelectData_t |
|---|
| 151 |
{ |
|---|
| 152 |
SelectData_t(); |
|---|
| 153 |
|
|---|
| 154 |
int _Select(); |
|---|
| 155 |
|
|---|
| 156 |
int maxsocket; |
|---|
| 157 |
fd_set fdreads; |
|---|
| 158 |
fd_set fdwrites; |
|---|
| 159 |
timeval tv; |
|---|
| 160 |
int nSockets; |
|---|
| 161 |
}; |
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 |
|
|---|
| 165 |
#endif |
|---|
| 166 |
|
|---|
| 167 |
|
|---|