forked from eventmachine/eventmachine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ed.h
421 lines (309 loc) · 10.3 KB
/
ed.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*****************************************************************************
$Id$
File: ed.h
Date: 06Apr06
Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
Gmail: blackhedd
This program is free software; you can redistribute it and/or modify
it under the terms of either: 1) the GNU General Public License
as published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version; or 2) Ruby's License.
See the file COPYING for complete licensing information.
*****************************************************************************/
#ifndef __EventableDescriptor__H_
#define __EventableDescriptor__H_
class EventMachine_t; // forward reference
#ifdef WITH_SSL
class SslBox_t; // forward reference
#endif
bool SetSocketNonblocking (SOCKET);
/*************************
class EventableDescriptor
*************************/
class EventableDescriptor: public Bindable_t
{
public:
EventableDescriptor (int, EventMachine_t*);
virtual ~EventableDescriptor();
int GetSocket() {return MySocket;}
void SetSocketInvalid() { MySocket = INVALID_SOCKET; }
void Close();
virtual void Read() = 0;
virtual void Write() = 0;
virtual void Heartbeat() = 0;
// These methods tell us whether the descriptor
// should be selected or polled for read/write.
virtual bool SelectForRead() = 0;
virtual bool SelectForWrite() = 0;
// are we scheduled for a close, or in an error state, or already closed?
bool ShouldDelete();
// Do we have any data to write? This is used by ShouldDelete.
virtual int GetOutboundDataSize() {return 0;}
virtual bool IsWatchOnly(){ return bWatchOnly; }
virtual void ScheduleClose (bool after_writing);
bool IsCloseScheduled();
virtual void HandleError(){ ScheduleClose (false); }
void SetEventCallback (EMCallback);
virtual bool GetPeername (struct sockaddr*, socklen_t*) {return false;}
virtual bool GetSockname (struct sockaddr*, socklen_t*) {return false;}
virtual bool GetSubprocessPid (pid_t*) {return false;}
virtual void StartTls() {}
virtual void SetTlsParms (const char *ca_filename, const char *privkey_filename, const char *privkey_file_pwd, const char *certchain_filename, bool verify_peer) {}
#ifdef WITH_SSL
virtual X509 *GetPeerCert() {return NULL;}
#endif
virtual uint64_t GetCommInactivityTimeout() {return 0;}
virtual int SetCommInactivityTimeout (uint64_t value) {return 0;}
uint64_t GetPendingConnectTimeout();
int SetPendingConnectTimeout (uint64_t value);
#ifdef HAVE_EPOLL
struct epoll_event *GetEpollEvent() { return &EpollEvent; }
#endif
virtual void StartProxy(const unsigned long, const unsigned long, const unsigned long);
virtual void StopProxy();
virtual void SetProxiedFrom(EventableDescriptor*, const unsigned long);
virtual int SendOutboundData(const char*,int){ return -1; }
virtual bool IsPaused(){ return bPaused; }
virtual bool Pause(){ bPaused = true; return bPaused; }
virtual bool Resume(){ bPaused = false; return bPaused; }
void SetUnbindReasonCode(int code){ UnbindReasonCode = code; }
virtual int ReportErrorStatus(){ return 0; }
virtual bool IsConnectPending(){ return false; }
virtual uint64_t GetNextHeartbeat();
private:
bool bCloseNow;
bool bCloseAfterWriting;
protected:
int MySocket;
bool bAttached;
bool bWatchOnly;
EMCallback EventCallback;
void _GenericInboundDispatch(const char*, int);
uint64_t CreatedAt;
bool bCallbackUnbind;
int UnbindReasonCode;
unsigned long BytesToProxy;
EventableDescriptor *ProxyTarget;
EventableDescriptor *ProxiedFrom;
unsigned long MaxOutboundBufSize;
#ifdef HAVE_EPOLL
struct epoll_event EpollEvent;
#endif
EventMachine_t *MyEventMachine;
uint64_t PendingConnectTimeout;
uint64_t InactivityTimeout;
uint64_t LastActivity;
uint64_t NextHeartbeat;
bool bPaused;
};
/*************************
class LoopbreakDescriptor
*************************/
class LoopbreakDescriptor: public EventableDescriptor
{
public:
LoopbreakDescriptor (int, EventMachine_t*);
virtual ~LoopbreakDescriptor() {}
virtual void Read();
virtual void Write();
virtual void Heartbeat() {}
virtual bool SelectForRead() {return true;}
virtual bool SelectForWrite() {return false;}
};
/**************************
class ConnectionDescriptor
**************************/
class ConnectionDescriptor: public EventableDescriptor
{
public:
ConnectionDescriptor (int, EventMachine_t*);
virtual ~ConnectionDescriptor();
int SendOutboundData (const char*, int);
void SetConnectPending (bool f);
virtual void ScheduleClose (bool after_writing);
virtual void HandleError();
void SetNotifyReadable (bool);
void SetNotifyWritable (bool);
void SetAttached (bool);
void SetWatchOnly (bool);
bool Pause();
bool Resume();
bool IsNotifyReadable(){ return bNotifyReadable; }
bool IsNotifyWritable(){ return bNotifyWritable; }
virtual void Read();
virtual void Write();
virtual void Heartbeat();
virtual bool SelectForRead();
virtual bool SelectForWrite();
// Do we have any data to write? This is used by ShouldDelete.
virtual int GetOutboundDataSize() {return OutboundDataSize;}
virtual void StartTls();
virtual void SetTlsParms (const char *ca_filename, const char *privkey_filename, const char *privkey_pwd, const char *certchain_filename, bool verify_peer);
#ifdef WITH_SSL
virtual X509 *GetPeerCert();
virtual bool VerifySslPeer(const char*);
virtual void AcceptSslPeer();
#endif
void SetServerMode() {bIsServer = true;}
virtual bool GetPeername (struct sockaddr*, socklen_t*);
virtual bool GetSockname (struct sockaddr*, socklen_t*);
virtual uint64_t GetCommInactivityTimeout();
virtual int SetCommInactivityTimeout (uint64_t value);
virtual int ReportErrorStatus();
virtual bool IsConnectPending(){ return bConnectPending; }
protected:
struct OutboundPage {
OutboundPage (const char *b, int l, int o=0): Buffer(b), Length(l), Offset(o) {}
void Free() {if (Buffer) free ((char*)Buffer); }
const char *Buffer;
int Length;
int Offset;
};
protected:
bool bConnectPending;
bool bNotifyReadable;
bool bNotifyWritable;
bool bReadAttemptedAfterClose;
bool bWriteAttemptedAfterClose;
deque<OutboundPage> OutboundPages;
int OutboundDataSize;
#ifdef WITH_SSL
SslBox_t *SslBox;
std::string CertChainFilename;
std::string CAFilename;
std::string PrivateKeyFilename;
std::string PrivateKeyPwd;
bool bHandshakeSignaled;
bool bSslVerifyPeer;
bool bSslPeerAccepted;
#endif
#ifdef HAVE_KQUEUE
bool bGotExtraKqueueEvent;
#endif
bool bIsServer;
private:
void _UpdateEvents();
void _UpdateEvents(bool, bool);
void _WriteOutboundData();
void _DispatchInboundData (const char *buffer, int size);
void _DispatchCiphertext();
int _SendRawOutboundData (const char*, int);
void _CheckHandshakeStatus();
};
/************************
class DatagramDescriptor
************************/
class DatagramDescriptor: public EventableDescriptor
{
public:
DatagramDescriptor (int, EventMachine_t*);
virtual ~DatagramDescriptor();
virtual void Read();
virtual void Write();
virtual void Heartbeat();
virtual bool SelectForRead() {return true;}
virtual bool SelectForWrite();
int SendOutboundData (const char*, int);
int SendOutboundDatagram (const char*, int, const char*, int);
// Do we have any data to write? This is used by ShouldDelete.
virtual int GetOutboundDataSize() {return OutboundDataSize;}
virtual bool GetPeername (struct sockaddr*, socklen_t*);
virtual bool GetSockname (struct sockaddr*, socklen_t*);
virtual uint64_t GetCommInactivityTimeout();
virtual int SetCommInactivityTimeout (uint64_t value);
protected:
struct OutboundPage {
OutboundPage (const char *b, int l, struct sockaddr_in f, int o=0): Buffer(b), Length(l), Offset(o), From(f) {}
void Free() {if (Buffer) free ((char*)Buffer); }
const char *Buffer;
int Length;
int Offset;
struct sockaddr_in From;
};
deque<OutboundPage> OutboundPages;
int OutboundDataSize;
struct sockaddr_in ReturnAddress;
};
/************************
class AcceptorDescriptor
************************/
class AcceptorDescriptor: public EventableDescriptor
{
public:
AcceptorDescriptor (int, EventMachine_t*);
virtual ~AcceptorDescriptor();
virtual void Read();
virtual void Write();
virtual void Heartbeat();
virtual bool SelectForRead() {return true;}
virtual bool SelectForWrite() {return false;}
virtual bool GetSockname (struct sockaddr*, socklen_t*);
static void StopAcceptor (const unsigned long binding);
};
/********************
class PipeDescriptor
********************/
#ifdef OS_UNIX
class PipeDescriptor: public EventableDescriptor
{
public:
PipeDescriptor (int, pid_t, EventMachine_t*);
virtual ~PipeDescriptor();
virtual void Read();
virtual void Write();
virtual void Heartbeat();
virtual bool SelectForRead();
virtual bool SelectForWrite();
int SendOutboundData (const char*, int);
virtual int GetOutboundDataSize() {return OutboundDataSize;}
virtual bool GetSubprocessPid (pid_t*);
protected:
struct OutboundPage {
OutboundPage (const char *b, int l, int o=0): Buffer(b), Length(l), Offset(o) {}
void Free() {if (Buffer) free ((char*)Buffer); }
const char *Buffer;
int Length;
int Offset;
};
protected:
bool bReadAttemptedAfterClose;
deque<OutboundPage> OutboundPages;
int OutboundDataSize;
pid_t SubprocessPid;
private:
void _DispatchInboundData (const char *buffer, int size);
};
#endif // OS_UNIX
/************************
class KeyboardDescriptor
************************/
class KeyboardDescriptor: public EventableDescriptor
{
public:
KeyboardDescriptor (EventMachine_t*);
virtual ~KeyboardDescriptor();
virtual void Read();
virtual void Write();
virtual void Heartbeat();
virtual bool SelectForRead() {return true;}
virtual bool SelectForWrite() {return false;}
protected:
bool bReadAttemptedAfterClose;
private:
void _DispatchInboundData (const char *buffer, int size);
};
/***********************
class InotifyDescriptor
************************/
class InotifyDescriptor: public EventableDescriptor
{
public:
InotifyDescriptor (EventMachine_t*);
virtual ~InotifyDescriptor();
void Read();
void Write();
virtual void Heartbeat() {}
virtual bool SelectForRead() {return true;}
virtual bool SelectForWrite() {return false;}
};
#endif // __EventableDescriptor__H_