smparkes-eventmachine 0.12.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/.gitignore +15 -0
  2. data/README +81 -0
  3. data/Rakefile +374 -0
  4. data/docs/COPYING +60 -0
  5. data/docs/ChangeLog +211 -0
  6. data/docs/DEFERRABLES +133 -0
  7. data/docs/EPOLL +141 -0
  8. data/docs/GNU +281 -0
  9. data/docs/INSTALL +13 -0
  10. data/docs/KEYBOARD +38 -0
  11. data/docs/LEGAL +25 -0
  12. data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
  13. data/docs/PURE_RUBY +75 -0
  14. data/docs/RELEASE_NOTES +94 -0
  15. data/docs/SMTP +2 -0
  16. data/docs/SPAWNED_PROCESSES +89 -0
  17. data/docs/TODO +8 -0
  18. data/eventmachine.gemspec +40 -0
  19. data/examples/ex_channel.rb +43 -0
  20. data/examples/ex_queue.rb +2 -0
  21. data/examples/helper.rb +2 -0
  22. data/ext/binder.cpp +125 -0
  23. data/ext/binder.h +46 -0
  24. data/ext/cmain.cpp +827 -0
  25. data/ext/cplusplus.cpp +202 -0
  26. data/ext/ed.cpp +1901 -0
  27. data/ext/ed.h +424 -0
  28. data/ext/em.cpp +2288 -0
  29. data/ext/em.h +229 -0
  30. data/ext/emwin.cpp +300 -0
  31. data/ext/emwin.h +94 -0
  32. data/ext/epoll.cpp +26 -0
  33. data/ext/epoll.h +25 -0
  34. data/ext/eventmachine.h +122 -0
  35. data/ext/eventmachine_cpp.h +96 -0
  36. data/ext/extconf.rb +150 -0
  37. data/ext/fastfilereader/extconf.rb +85 -0
  38. data/ext/fastfilereader/mapper.cpp +214 -0
  39. data/ext/fastfilereader/mapper.h +59 -0
  40. data/ext/fastfilereader/rubymain.cpp +127 -0
  41. data/ext/files.cpp +94 -0
  42. data/ext/files.h +65 -0
  43. data/ext/kb.cpp +81 -0
  44. data/ext/page.cpp +107 -0
  45. data/ext/page.h +51 -0
  46. data/ext/pipe.cpp +349 -0
  47. data/ext/project.h +156 -0
  48. data/ext/rubymain.cpp +1194 -0
  49. data/ext/sigs.cpp +89 -0
  50. data/ext/sigs.h +32 -0
  51. data/ext/ssl.cpp +460 -0
  52. data/ext/ssl.h +94 -0
  53. data/java/.classpath +8 -0
  54. data/java/.project +17 -0
  55. data/java/src/com/rubyeventmachine/EmReactor.java +570 -0
  56. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  57. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  58. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  59. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  60. data/java/src/com/rubyeventmachine/application/Application.java +194 -0
  61. data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
  62. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
  63. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
  64. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
  65. data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
  66. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
  67. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -0
  68. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  69. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  70. data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -0
  71. data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
  72. data/lib/em/buftok.rb +138 -0
  73. data/lib/em/callback.rb +26 -0
  74. data/lib/em/channel.rb +57 -0
  75. data/lib/em/connection.rb +564 -0
  76. data/lib/em/deferrable.rb +192 -0
  77. data/lib/em/file_watch.rb +54 -0
  78. data/lib/em/future.rb +61 -0
  79. data/lib/em/messages.rb +66 -0
  80. data/lib/em/process_watch.rb +44 -0
  81. data/lib/em/processes.rb +119 -0
  82. data/lib/em/protocols/header_and_content.rb +138 -0
  83. data/lib/em/protocols/httpclient.rb +263 -0
  84. data/lib/em/protocols/httpclient2.rb +590 -0
  85. data/lib/em/protocols/line_and_text.rb +125 -0
  86. data/lib/em/protocols/linetext2.rb +161 -0
  87. data/lib/em/protocols/memcache.rb +323 -0
  88. data/lib/em/protocols/object_protocol.rb +45 -0
  89. data/lib/em/protocols/postgres3.rb +247 -0
  90. data/lib/em/protocols/saslauth.rb +175 -0
  91. data/lib/em/protocols/smtpclient.rb +357 -0
  92. data/lib/em/protocols/smtpserver.rb +547 -0
  93. data/lib/em/protocols/socks4.rb +66 -0
  94. data/lib/em/protocols/stomp.rb +200 -0
  95. data/lib/em/protocols/tcptest.rb +53 -0
  96. data/lib/em/protocols.rb +36 -0
  97. data/lib/em/queue.rb +61 -0
  98. data/lib/em/spawnable.rb +85 -0
  99. data/lib/em/streamer.rb +130 -0
  100. data/lib/em/timers.rb +56 -0
  101. data/lib/em/version.rb +3 -0
  102. data/lib/eventmachine.rb +1592 -0
  103. data/lib/evma/callback.rb +32 -0
  104. data/lib/evma/container.rb +75 -0
  105. data/lib/evma/factory.rb +77 -0
  106. data/lib/evma/protocol.rb +87 -0
  107. data/lib/evma/reactor.rb +48 -0
  108. data/lib/evma.rb +32 -0
  109. data/lib/jeventmachine.rb +257 -0
  110. data/lib/pr_eventmachine.rb +1022 -0
  111. data/setup.rb +1585 -0
  112. data/tasks/cpp.rake_example +77 -0
  113. data/tests/client.crt +31 -0
  114. data/tests/client.key +51 -0
  115. data/tests/test_attach.rb +126 -0
  116. data/tests/test_basic.rb +284 -0
  117. data/tests/test_channel.rb +63 -0
  118. data/tests/test_connection_count.rb +35 -0
  119. data/tests/test_defer.rb +47 -0
  120. data/tests/test_epoll.rb +160 -0
  121. data/tests/test_error_handler.rb +35 -0
  122. data/tests/test_errors.rb +82 -0
  123. data/tests/test_exc.rb +55 -0
  124. data/tests/test_file_watch.rb +49 -0
  125. data/tests/test_futures.rb +198 -0
  126. data/tests/test_get_sock_opt.rb +30 -0
  127. data/tests/test_handler_check.rb +37 -0
  128. data/tests/test_hc.rb +218 -0
  129. data/tests/test_httpclient.rb +218 -0
  130. data/tests/test_httpclient2.rb +153 -0
  131. data/tests/test_inactivity_timeout.rb +50 -0
  132. data/tests/test_kb.rb +60 -0
  133. data/tests/test_ltp.rb +182 -0
  134. data/tests/test_ltp2.rb +317 -0
  135. data/tests/test_next_tick.rb +133 -0
  136. data/tests/test_object_protocol.rb +37 -0
  137. data/tests/test_pause.rb +70 -0
  138. data/tests/test_pending_connect_timeout.rb +48 -0
  139. data/tests/test_process_watch.rb +48 -0
  140. data/tests/test_processes.rb +128 -0
  141. data/tests/test_proxy_connection.rb +92 -0
  142. data/tests/test_pure.rb +125 -0
  143. data/tests/test_queue.rb +44 -0
  144. data/tests/test_running.rb +42 -0
  145. data/tests/test_sasl.rb +72 -0
  146. data/tests/test_send_file.rb +242 -0
  147. data/tests/test_servers.rb +76 -0
  148. data/tests/test_smtpclient.rb +83 -0
  149. data/tests/test_smtpserver.rb +85 -0
  150. data/tests/test_spawn.rb +322 -0
  151. data/tests/test_ssl_args.rb +79 -0
  152. data/tests/test_ssl_methods.rb +50 -0
  153. data/tests/test_ssl_verify.rb +82 -0
  154. data/tests/test_timers.rb +162 -0
  155. data/tests/test_ud.rb +36 -0
  156. data/tests/testem.rb +31 -0
  157. data/web/whatis +7 -0
  158. metadata +237 -0
data/ext/ed.cpp ADDED
@@ -0,0 +1,1901 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: ed.cpp
6
+ Date: 06Apr06
7
+
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
+
11
+ This program is free software; you can redistribute it and/or modify
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
17
+
18
+ *****************************************************************************/
19
+
20
+ #include "project.h"
21
+
22
+
23
+
24
+ /********************
25
+ SetSocketNonblocking
26
+ ********************/
27
+
28
+ bool SetSocketNonblocking (SOCKET sd)
29
+ {
30
+ #ifdef OS_UNIX
31
+ int val = fcntl (sd, F_GETFL, 0);
32
+ return (fcntl (sd, F_SETFL, val | O_NONBLOCK) != SOCKET_ERROR) ? true : false;
33
+ #endif
34
+
35
+ #ifdef OS_WIN32
36
+ #ifdef BUILD_FOR_RUBY
37
+ // 14Jun09 Ruby provides its own wrappers for ioctlsocket. On 1.8 this is a simple wrapper,
38
+ // however, 1.9 keeps its own state about the socket.
39
+ // NOTE: F_GETFL is not supported
40
+ return (fcntl (sd, F_SETFL, O_NONBLOCK) == 0) ? true : false;
41
+ #else
42
+ unsigned long one = 1;
43
+ return (ioctlsocket (sd, FIONBIO, &one) == 0) ? true : false;
44
+ #endif
45
+ #endif
46
+ }
47
+
48
+
49
+ /****************************************
50
+ EventableDescriptor::EventableDescriptor
51
+ ****************************************/
52
+
53
+ EventableDescriptor::EventableDescriptor (int sd, EventMachine_t *em):
54
+ bCloseNow (false),
55
+ bCloseAfterWriting (false),
56
+ MySocket (sd),
57
+ EventCallback (NULL),
58
+ bCallbackUnbind (true),
59
+ UnbindReasonCode (0),
60
+ ProxyTarget(NULL),
61
+ ProxiedFrom(NULL),
62
+ MaxOutboundBufSize(0),
63
+ MyEventMachine (em),
64
+ PendingConnectTimeout(20000000)
65
+ {
66
+ /* There are three ways to close a socket, all of which should
67
+ * automatically signal to the event machine that this object
68
+ * should be removed from the polling scheduler.
69
+ * First is a hard close, intended for bad errors or possible
70
+ * security violations. It immediately closes the connection
71
+ * and puts this object into an error state.
72
+ * Second is to set bCloseNow, which will cause the event machine
73
+ * to delete this object (and thus close the connection in our
74
+ * destructor) the next chance it gets. bCloseNow also inhibits
75
+ * the writing of new data on the socket (but not necessarily
76
+ * the reading of new data).
77
+ * The third way is to set bCloseAfterWriting, which inhibits
78
+ * the writing of new data and converts to bCloseNow as soon
79
+ * as everything in the outbound queue has been written.
80
+ * bCloseAfterWriting is really for use only by protocol handlers
81
+ * (for example, HTTP writes an HTML page and then closes the
82
+ * connection). All of the error states we generate internally
83
+ * cause an immediate close to be scheduled, which may have the
84
+ * effect of discarding outbound data.
85
+ */
86
+
87
+ if (sd == INVALID_SOCKET)
88
+ throw std::runtime_error ("bad eventable descriptor");
89
+ if (MyEventMachine == NULL)
90
+ throw std::runtime_error ("bad em in eventable descriptor");
91
+ CreatedAt = gCurrentLoopTime;
92
+
93
+ #ifdef HAVE_EPOLL
94
+ EpollEvent.events = 0;
95
+ EpollEvent.data.ptr = this;
96
+ #endif
97
+ }
98
+
99
+
100
+ /*****************************************
101
+ EventableDescriptor::~EventableDescriptor
102
+ *****************************************/
103
+
104
+ EventableDescriptor::~EventableDescriptor()
105
+ {
106
+ if (EventCallback && bCallbackUnbind)
107
+ (*EventCallback)(GetBinding(), EM_CONNECTION_UNBOUND, NULL, UnbindReasonCode);
108
+ if (ProxiedFrom) {
109
+ (*EventCallback)(ProxiedFrom->GetBinding(), EM_PROXY_TARGET_UNBOUND, NULL, 0);
110
+ ProxiedFrom->StopProxy();
111
+ }
112
+ StopProxy();
113
+ Close();
114
+ }
115
+
116
+
117
+ /*************************************
118
+ EventableDescriptor::SetEventCallback
119
+ *************************************/
120
+
121
+ void EventableDescriptor::SetEventCallback (EMCallback cb)
122
+ {
123
+ EventCallback = cb;
124
+ }
125
+
126
+
127
+ /**************************
128
+ EventableDescriptor::Close
129
+ **************************/
130
+
131
+ void EventableDescriptor::Close()
132
+ {
133
+ // Close the socket right now. Intended for emergencies.
134
+ if (MySocket != INVALID_SOCKET) {
135
+ shutdown (MySocket, 1);
136
+ closesocket (MySocket);
137
+ MySocket = INVALID_SOCKET;
138
+ }
139
+ }
140
+
141
+
142
+ /*********************************
143
+ EventableDescriptor::ShouldDelete
144
+ *********************************/
145
+
146
+ bool EventableDescriptor::ShouldDelete()
147
+ {
148
+ /* For use by a socket manager, which needs to know if this object
149
+ * should be removed from scheduling events and deleted.
150
+ * Has an immediate close been scheduled, or are we already closed?
151
+ * If either of these are the case, return true. In theory, the manager will
152
+ * then delete us, which in turn will make sure the socket is closed.
153
+ * Note, if bCloseAfterWriting is true, we check a virtual method to see
154
+ * if there is outbound data to write, and only request a close if there is none.
155
+ */
156
+
157
+ return ((MySocket == INVALID_SOCKET) || bCloseNow || (bCloseAfterWriting && (GetOutboundDataSize() <= 0)));
158
+ }
159
+
160
+
161
+ /**********************************
162
+ EventableDescriptor::ScheduleClose
163
+ **********************************/
164
+
165
+ void EventableDescriptor::ScheduleClose (bool after_writing)
166
+ {
167
+ // KEEP THIS SYNCHRONIZED WITH ::IsCloseScheduled.
168
+ if (after_writing)
169
+ bCloseAfterWriting = true;
170
+ else
171
+ bCloseNow = true;
172
+ }
173
+
174
+
175
+ /*************************************
176
+ EventableDescriptor::IsCloseScheduled
177
+ *************************************/
178
+
179
+ bool EventableDescriptor::IsCloseScheduled()
180
+ {
181
+ // KEEP THIS SYNCHRONIZED WITH ::ScheduleClose.
182
+ return (bCloseNow || bCloseAfterWriting);
183
+ }
184
+
185
+
186
+ /*******************************
187
+ EventableDescriptor::StartProxy
188
+ *******************************/
189
+
190
+ void EventableDescriptor::StartProxy(const unsigned long to, const unsigned long bufsize)
191
+ {
192
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (to));
193
+ if (ed) {
194
+ StopProxy();
195
+ ProxyTarget = ed;
196
+ ed->SetProxiedFrom(this, bufsize);
197
+ return;
198
+ }
199
+ throw std::runtime_error ("Tried to proxy to an invalid descriptor");
200
+ }
201
+
202
+
203
+ /******************************
204
+ EventableDescriptor::StopProxy
205
+ ******************************/
206
+
207
+ void EventableDescriptor::StopProxy()
208
+ {
209
+ if (ProxyTarget) {
210
+ ProxyTarget->SetProxiedFrom(NULL, 0);
211
+ ProxyTarget = NULL;
212
+ }
213
+ }
214
+
215
+
216
+ /***********************************
217
+ EventableDescriptor::SetProxiedFrom
218
+ ***********************************/
219
+
220
+ void EventableDescriptor::SetProxiedFrom(EventableDescriptor *from, const unsigned long bufsize)
221
+ {
222
+ ProxiedFrom = from;
223
+ MaxOutboundBufSize = bufsize;
224
+ }
225
+
226
+
227
+ /********************************************
228
+ EventableDescriptor::_GenericInboundDispatch
229
+ ********************************************/
230
+
231
+ void EventableDescriptor::_GenericInboundDispatch(const char *buf, int size)
232
+ {
233
+ assert(EventCallback);
234
+
235
+ if (ProxyTarget)
236
+ ProxyTarget->SendOutboundData(buf, size);
237
+ else
238
+ (*EventCallback)(GetBinding(), EM_CONNECTION_READ, buf, size);
239
+ }
240
+
241
+
242
+ /*********************************************
243
+ EventableDescriptor::GetPendingConnectTimeout
244
+ *********************************************/
245
+
246
+ float EventableDescriptor::GetPendingConnectTimeout()
247
+ {
248
+ return ((float)PendingConnectTimeout / 1000000);
249
+ }
250
+
251
+
252
+ /*********************************************
253
+ EventableDescriptor::SetPendingConnectTimeout
254
+ *********************************************/
255
+
256
+ int EventableDescriptor::SetPendingConnectTimeout (float value)
257
+ {
258
+ if (value > 0) {
259
+ PendingConnectTimeout = (Int64)(value * 1000000);
260
+ return 1;
261
+ }
262
+ return 0;
263
+ }
264
+
265
+
266
+ /******************************************
267
+ ConnectionDescriptor::ConnectionDescriptor
268
+ ******************************************/
269
+
270
+ ConnectionDescriptor::ConnectionDescriptor (int sd, EventMachine_t *em):
271
+ EventableDescriptor (sd, em),
272
+ bPaused (false),
273
+ bConnectPending (false),
274
+ bNotifyReadable (false),
275
+ bNotifyWritable (false),
276
+ bWatchOnly (false),
277
+ bReadAttemptedAfterClose (false),
278
+ bWriteAttemptedAfterClose (false),
279
+ OutboundDataSize (0),
280
+ #ifdef WITH_SSL
281
+ SslBox (NULL),
282
+ bHandshakeSignaled (false),
283
+ bSslVerifyPeer (false),
284
+ bSslPeerAccepted(false),
285
+ #endif
286
+ #ifdef HAVE_KQUEUE
287
+ bGotExtraKqueueEvent(false),
288
+ #endif
289
+ bIsServer (false),
290
+ LastIo (gCurrentLoopTime),
291
+ InactivityTimeout (0)
292
+ {
293
+ // 22Jan09: Moved ArmKqueueWriter into SetConnectPending() to fix assertion failure in _WriteOutboundData()
294
+ // 5May09: Moved EPOLLOUT into SetConnectPending() so it doesn't happen for attached read pipes
295
+ }
296
+
297
+
298
+ /*******************************************
299
+ ConnectionDescriptor::~ConnectionDescriptor
300
+ *******************************************/
301
+
302
+ ConnectionDescriptor::~ConnectionDescriptor()
303
+ {
304
+ // Run down any stranded outbound data.
305
+ for (size_t i=0; i < OutboundPages.size(); i++)
306
+ OutboundPages[i].Free();
307
+
308
+ #ifdef WITH_SSL
309
+ if (SslBox)
310
+ delete SslBox;
311
+ #endif
312
+ }
313
+
314
+
315
+ /**************************************************
316
+ STATIC: ConnectionDescriptor::SendDataToConnection
317
+ **************************************************/
318
+
319
+ int ConnectionDescriptor::SendDataToConnection (const unsigned long binding, const char *data, int data_length)
320
+ {
321
+ // TODO: This is something of a hack, or at least it's a static method of the wrong class.
322
+ // TODO: Poor polymorphism here. We should be calling one virtual method
323
+ // instead of hacking out the runtime information of the target object.
324
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
325
+ if (cd)
326
+ return cd->SendOutboundData (data, data_length);
327
+ DatagramDescriptor *ds = dynamic_cast <DatagramDescriptor*> (Bindable_t::GetObject (binding));
328
+ if (ds)
329
+ return ds->SendOutboundData (data, data_length);
330
+ #ifdef OS_UNIX
331
+ PipeDescriptor *ps = dynamic_cast <PipeDescriptor*> (Bindable_t::GetObject (binding));
332
+ if (ps)
333
+ return ps->SendOutboundData (data, data_length);
334
+ #endif
335
+ return -1;
336
+ }
337
+
338
+
339
+ /*********************************************
340
+ STATIC: ConnectionDescriptor::CloseConnection
341
+ *********************************************/
342
+
343
+ void ConnectionDescriptor::CloseConnection (const unsigned long binding, bool after_writing)
344
+ {
345
+ // TODO: This is something of a hack, or at least it's a static method of the wrong class.
346
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
347
+ if (ed)
348
+ ed->ScheduleClose (after_writing);
349
+ }
350
+
351
+ /***********************************************
352
+ STATIC: ConnectionDescriptor::ReportErrorStatus
353
+ ***********************************************/
354
+
355
+ int ConnectionDescriptor::ReportErrorStatus (const unsigned long binding)
356
+ {
357
+ // TODO: This is something of a hack, or at least it's a static method of the wrong class.
358
+ // TODO: Poor polymorphism here. We should be calling one virtual method
359
+ // instead of hacking out the runtime information of the target object.
360
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
361
+ if (cd)
362
+ return cd->_ReportErrorStatus();
363
+ return -1;
364
+ }
365
+
366
+ /***********************************
367
+ ConnectionDescriptor::_UpdateEvents
368
+ ************************************/
369
+
370
+ void ConnectionDescriptor::_UpdateEvents()
371
+ {
372
+ _UpdateEvents(true, true);
373
+ }
374
+
375
+ void ConnectionDescriptor::_UpdateEvents(bool read, bool write)
376
+ {
377
+ if (MySocket == INVALID_SOCKET)
378
+ return;
379
+
380
+ #ifdef HAVE_EPOLL
381
+ unsigned int old = EpollEvent.events;
382
+
383
+ if (read) {
384
+ if (SelectForRead())
385
+ EpollEvent.events |= EPOLLIN;
386
+ else
387
+ EpollEvent.events &= ~EPOLLIN;
388
+ }
389
+
390
+ if (write) {
391
+ if (SelectForWrite())
392
+ EpollEvent.events |= EPOLLOUT;
393
+ else
394
+ EpollEvent.events &= ~EPOLLOUT;
395
+ }
396
+
397
+ if (old != EpollEvent.events)
398
+ MyEventMachine->Modify (this);
399
+ #endif
400
+
401
+ #ifdef HAVE_KQUEUE
402
+ if (read && SelectForRead())
403
+ MyEventMachine->ArmKqueueReader (this);
404
+ if (write && SelectForWrite())
405
+ MyEventMachine->ArmKqueueWriter (this);
406
+ #endif
407
+ }
408
+
409
+ /***************************************
410
+ ConnectionDescriptor::SetConnectPending
411
+ ****************************************/
412
+
413
+ void ConnectionDescriptor::SetConnectPending(bool f)
414
+ {
415
+ bConnectPending = f;
416
+ _UpdateEvents();
417
+ }
418
+
419
+
420
+ /**********************************
421
+ ConnectionDescriptor::SetWatchOnly
422
+ ***********************************/
423
+
424
+ void ConnectionDescriptor::SetWatchOnly(bool watching)
425
+ {
426
+ bWatchOnly = watching;
427
+ _UpdateEvents();
428
+ }
429
+
430
+
431
+ /*********************************
432
+ ConnectionDescriptor::HandleError
433
+ *********************************/
434
+
435
+ void ConnectionDescriptor::HandleError()
436
+ {
437
+ if (bWatchOnly) {
438
+ // An EPOLLHUP | EPOLLIN condition will call Read() before HandleError(), in which case the
439
+ // socket is already detached and invalid, so we don't need to do anything.
440
+ if (MySocket == INVALID_SOCKET) return;
441
+
442
+ // HandleError() is called on WatchOnly descriptors by the epoll reactor
443
+ // when it gets a EPOLLERR | EPOLLHUP. Usually this would show up as a readable and
444
+ // writable event on other reactors, so we have to fire those events ourselves.
445
+ if (bNotifyReadable) Read();
446
+ if (bNotifyWritable) Write();
447
+ } else {
448
+ ScheduleClose (false);
449
+ }
450
+ }
451
+
452
+
453
+ /***********************************
454
+ ConnectionDescriptor::ScheduleClose
455
+ ***********************************/
456
+
457
+ void ConnectionDescriptor::ScheduleClose (bool after_writing)
458
+ {
459
+ if (bWatchOnly)
460
+ throw std::runtime_error ("cannot close 'watch only' connections");
461
+
462
+ EventableDescriptor::ScheduleClose(after_writing);
463
+ }
464
+
465
+
466
+ /***************************************
467
+ ConnectionDescriptor::SetNotifyReadable
468
+ ****************************************/
469
+
470
+ void ConnectionDescriptor::SetNotifyReadable(bool readable)
471
+ {
472
+ if (!bWatchOnly)
473
+ throw std::runtime_error ("notify_readable must be on 'watch only' connections");
474
+
475
+ bNotifyReadable = readable;
476
+ _UpdateEvents(true, false);
477
+ }
478
+
479
+
480
+ /***************************************
481
+ ConnectionDescriptor::SetNotifyWritable
482
+ ****************************************/
483
+
484
+ void ConnectionDescriptor::SetNotifyWritable(bool writable)
485
+ {
486
+ if (!bWatchOnly)
487
+ throw std::runtime_error ("notify_writable must be on 'watch only' connections");
488
+
489
+ bNotifyWritable = writable;
490
+ _UpdateEvents(false, true);
491
+ }
492
+
493
+
494
+ /**************************************
495
+ ConnectionDescriptor::SendOutboundData
496
+ **************************************/
497
+
498
+ int ConnectionDescriptor::SendOutboundData (const char *data, int length)
499
+ {
500
+ if (bWatchOnly)
501
+ throw std::runtime_error ("cannot send data on a 'watch only' connection");
502
+
503
+ if (ProxiedFrom && MaxOutboundBufSize && GetOutboundDataSize() + length > MaxOutboundBufSize)
504
+ ProxiedFrom->Pause();
505
+
506
+ #ifdef WITH_SSL
507
+ if (SslBox) {
508
+ if (length > 0) {
509
+ int w = SslBox->PutPlaintext (data, length);
510
+ if (w < 0)
511
+ ScheduleClose (false);
512
+ else
513
+ _DispatchCiphertext();
514
+ }
515
+ // TODO: What's the correct return value?
516
+ return 1; // That's a wild guess, almost certainly wrong.
517
+ }
518
+ else
519
+ #endif
520
+ return _SendRawOutboundData (data, length);
521
+ }
522
+
523
+
524
+
525
+ /******************************************
526
+ ConnectionDescriptor::_SendRawOutboundData
527
+ ******************************************/
528
+
529
+ int ConnectionDescriptor::_SendRawOutboundData (const char *data, int length)
530
+ {
531
+ /* This internal method is called to schedule bytes that
532
+ * will be sent out to the remote peer.
533
+ * It's not directly accessed by the caller, who hits ::SendOutboundData,
534
+ * which may or may not filter or encrypt the caller's data before
535
+ * sending it here.
536
+ */
537
+
538
+ // Highly naive and incomplete implementation.
539
+ // There's no throttle for runaways (which should abort only this connection
540
+ // and not the whole process), and no coalescing of small pages.
541
+ // (Well, not so bad, small pages are coalesced in ::Write)
542
+
543
+ if (IsCloseScheduled())
544
+ //if (bCloseNow || bCloseAfterWriting)
545
+ return 0;
546
+
547
+ if (!data && (length > 0))
548
+ throw std::runtime_error ("bad outbound data");
549
+ char *buffer = (char *) malloc (length + 1);
550
+ if (!buffer)
551
+ throw std::runtime_error ("no allocation for outbound data");
552
+
553
+ memcpy (buffer, data, length);
554
+ buffer [length] = 0;
555
+ OutboundPages.push_back (OutboundPage (buffer, length));
556
+ OutboundDataSize += length;
557
+
558
+ _UpdateEvents(false, true);
559
+
560
+ return length;
561
+ }
562
+
563
+
564
+
565
+ /***********************************
566
+ ConnectionDescriptor::SelectForRead
567
+ ***********************************/
568
+
569
+ bool ConnectionDescriptor::SelectForRead()
570
+ {
571
+ /* A connection descriptor is always scheduled for read,
572
+ * UNLESS it's in a pending-connect state.
573
+ * On Linux, unlike Unix, a nonblocking socket on which
574
+ * connect has been called, does NOT necessarily select
575
+ * both readable and writable in case of error.
576
+ * The socket will select writable when the disposition
577
+ * of the connect is known. On the other hand, a socket
578
+ * which successfully connects and selects writable may
579
+ * indeed have some data available on it, so it will
580
+ * select readable in that case, violating expectations!
581
+ * So we will not poll for readability until the socket
582
+ * is known to be in a connected state.
583
+ */
584
+
585
+ if (bPaused)
586
+ return false;
587
+ else if (bConnectPending)
588
+ return false;
589
+ else if (bWatchOnly)
590
+ return bNotifyReadable ? true : false;
591
+ else
592
+ return true;
593
+ }
594
+
595
+
596
+ /************************************
597
+ ConnectionDescriptor::SelectForWrite
598
+ ************************************/
599
+
600
+ bool ConnectionDescriptor::SelectForWrite()
601
+ {
602
+ /* Cf the notes under SelectForRead.
603
+ * In a pending-connect state, we ALWAYS select for writable.
604
+ * In a normal state, we only select for writable when we
605
+ * have outgoing data to send.
606
+ */
607
+
608
+ if (bPaused)
609
+ return false;
610
+ else if (bConnectPending)
611
+ return true;
612
+ else if (bWatchOnly)
613
+ return bNotifyWritable ? true : false;
614
+ else
615
+ return (GetOutboundDataSize() > 0);
616
+ }
617
+
618
+ /***************************
619
+ ConnectionDescriptor::Pause
620
+ ***************************/
621
+
622
+ bool ConnectionDescriptor::Pause()
623
+ {
624
+ if (bWatchOnly)
625
+ throw std::runtime_error ("cannot pause/resume 'watch only' connections, set notify readable/writable instead");
626
+
627
+ bool old = bPaused;
628
+ bPaused = true;
629
+ _UpdateEvents();
630
+ return old == false;
631
+ }
632
+
633
+ /****************************
634
+ ConnectionDescriptor::Resume
635
+ ****************************/
636
+
637
+ bool ConnectionDescriptor::Resume()
638
+ {
639
+ if (bWatchOnly)
640
+ throw std::runtime_error ("cannot pause/resume 'watch only' connections, set notify readable/writable instead");
641
+
642
+ bool old = bPaused;
643
+ bPaused = false;
644
+ _UpdateEvents();
645
+ return old == true;
646
+ }
647
+
648
+ /**************************
649
+ ConnectionDescriptor::Read
650
+ **************************/
651
+
652
+ void ConnectionDescriptor::Read()
653
+ {
654
+ /* Read and dispatch data on a socket that has selected readable.
655
+ * It's theoretically possible to get and dispatch incoming data on
656
+ * a socket that has already been scheduled for closing or close-after-writing.
657
+ * In those cases, we'll leave it up the to protocol handler to "do the
658
+ * right thing" (which probably means to ignore the incoming data).
659
+ *
660
+ * 22Aug06: Chris Ochs reports that on FreeBSD, it's possible to come
661
+ * here with the socket already closed, after the process receives
662
+ * a ctrl-C signal (not sure if that's TERM or INT on BSD). The application
663
+ * was one in which network connections were doing a lot of interleaved reads
664
+ * and writes.
665
+ * Since we always write before reading (in order to keep the outbound queues
666
+ * as light as possible), I think what happened is that an interrupt caused
667
+ * the socket to be closed in ConnectionDescriptor::Write. We'll then
668
+ * come here in the same pass through the main event loop, and won't get
669
+ * cleaned up until immediately after.
670
+ * We originally asserted that the socket was valid when we got here.
671
+ * To deal properly with the possibility that we are closed when we get here,
672
+ * I removed the assert. HOWEVER, the potential for an infinite loop scares me,
673
+ * so even though this is really clunky, I added a flag to assert that we never
674
+ * come here more than once after being closed. (FCianfrocca)
675
+ */
676
+
677
+ int sd = GetSocket();
678
+ //assert (sd != INVALID_SOCKET); (original, removed 22Aug06)
679
+ if (sd == INVALID_SOCKET) {
680
+ assert (!bReadAttemptedAfterClose);
681
+ bReadAttemptedAfterClose = true;
682
+ return;
683
+ }
684
+
685
+ if (bWatchOnly) {
686
+ if (bNotifyReadable && EventCallback)
687
+ (*EventCallback)(GetBinding(), EM_CONNECTION_NOTIFY_READABLE, NULL, 0);
688
+ return;
689
+ }
690
+
691
+ LastIo = gCurrentLoopTime;
692
+
693
+ int total_bytes_read = 0;
694
+ char readbuffer [16 * 1024 + 1];
695
+
696
+ for (int i=0; i < 10; i++) {
697
+ // Don't read just one buffer and then move on. This is faster
698
+ // if there is a lot of incoming.
699
+ // But don't read indefinitely. Give other sockets a chance to run.
700
+ // NOTICE, we're reading one less than the buffer size.
701
+ // That's so we can put a guard byte at the end of what we send
702
+ // to user code.
703
+
704
+
705
+ int r = read (sd, readbuffer, sizeof(readbuffer) - 1);
706
+ //cerr << "<R:" << r << ">";
707
+
708
+ if (r > 0) {
709
+ total_bytes_read += r;
710
+
711
+ // Add a null-terminator at the the end of the buffer
712
+ // that we will send to the callback.
713
+ // DO NOT EVER CHANGE THIS. We want to explicitly allow users
714
+ // to be able to depend on this behavior, so they will have
715
+ // the option to do some things faster. Additionally it's
716
+ // a security guard against buffer overflows.
717
+ readbuffer [r] = 0;
718
+ _DispatchInboundData (readbuffer, r);
719
+ }
720
+ else if (r == 0) {
721
+ break;
722
+ }
723
+ else {
724
+ // Basically a would-block, meaning we've read everything there is to read.
725
+ break;
726
+ }
727
+
728
+ }
729
+
730
+
731
+ if (total_bytes_read == 0) {
732
+ // If we read no data on a socket that selected readable,
733
+ // it generally means the other end closed the connection gracefully.
734
+ ScheduleClose (false);
735
+ //bCloseNow = true;
736
+ }
737
+
738
+ }
739
+
740
+
741
+
742
+ /******************************************
743
+ ConnectionDescriptor::_DispatchInboundData
744
+ ******************************************/
745
+
746
+ void ConnectionDescriptor::_DispatchInboundData (const char *buffer, int size)
747
+ {
748
+ #ifdef WITH_SSL
749
+ if (SslBox) {
750
+ SslBox->PutCiphertext (buffer, size);
751
+
752
+ int s;
753
+ char B [2048];
754
+ while ((s = SslBox->GetPlaintext (B, sizeof(B) - 1)) > 0) {
755
+ _CheckHandshakeStatus();
756
+ B [s] = 0;
757
+ _GenericInboundDispatch(B, s);
758
+ }
759
+
760
+ // If our SSL handshake had a problem, shut down the connection.
761
+ if (s == -2) {
762
+ ScheduleClose(false);
763
+ return;
764
+ }
765
+
766
+ _CheckHandshakeStatus();
767
+ _DispatchCiphertext();
768
+ }
769
+ else {
770
+ _GenericInboundDispatch(buffer, size);
771
+ }
772
+ #endif
773
+
774
+ #ifdef WITHOUT_SSL
775
+ _GenericInboundDispatch(buffer, size);
776
+ #endif
777
+ }
778
+
779
+
780
+
781
+ /*******************************************
782
+ ConnectionDescriptor::_CheckHandshakeStatus
783
+ *******************************************/
784
+
785
+ void ConnectionDescriptor::_CheckHandshakeStatus()
786
+ {
787
+ #ifdef WITH_SSL
788
+ if (SslBox && (!bHandshakeSignaled) && SslBox->IsHandshakeCompleted()) {
789
+ bHandshakeSignaled = true;
790
+ if (EventCallback)
791
+ (*EventCallback)(GetBinding(), EM_SSL_HANDSHAKE_COMPLETED, NULL, 0);
792
+ }
793
+ #endif
794
+ }
795
+
796
+
797
+
798
+ /***************************
799
+ ConnectionDescriptor::Write
800
+ ***************************/
801
+
802
+ void ConnectionDescriptor::Write()
803
+ {
804
+ /* A socket which is in a pending-connect state will select
805
+ * writable when the disposition of the connect is known.
806
+ * At that point, check to be sure there are no errors,
807
+ * and if none, then promote the socket out of the pending
808
+ * state.
809
+ * TODO: I haven't figured out how Windows signals errors on
810
+ * unconnected sockets. Maybe it does the untraditional but
811
+ * logical thing and makes the socket selectable for error.
812
+ * If so, it's unsupported here for the time being, and connect
813
+ * errors will have to be caught by the timeout mechanism.
814
+ */
815
+
816
+ if (bConnectPending) {
817
+ int error;
818
+ socklen_t len;
819
+ len = sizeof(error);
820
+ #ifdef OS_UNIX
821
+ int o = getsockopt (GetSocket(), SOL_SOCKET, SO_ERROR, &error, &len);
822
+ #endif
823
+ #ifdef OS_WIN32
824
+ int o = getsockopt (GetSocket(), SOL_SOCKET, SO_ERROR, (char*)&error, &len);
825
+ #endif
826
+ if ((o == 0) && (error == 0)) {
827
+ if (EventCallback)
828
+ (*EventCallback)(GetBinding(), EM_CONNECTION_COMPLETED, "", 0);
829
+
830
+ // 5May09: Moved epoll/kqueue read/write arming into SetConnectPending, so it can be called
831
+ // from EventMachine_t::AttachFD as well.
832
+ SetConnectPending (false);
833
+ }
834
+ else
835
+ ScheduleClose (false);
836
+ //bCloseNow = true;
837
+ }
838
+ else {
839
+
840
+ if (bNotifyWritable) {
841
+ if (EventCallback)
842
+ (*EventCallback)(GetBinding(), EM_CONNECTION_NOTIFY_WRITABLE, NULL, 0);
843
+
844
+ _UpdateEvents(false, true);
845
+ return;
846
+ }
847
+
848
+ assert(!bWatchOnly);
849
+
850
+ /* 5May09: Kqueue bugs on OSX cause one extra writable event to fire even though we're using
851
+ EV_ONESHOT. We ignore this extra event once, but only the first time. If it happens again,
852
+ we should fall through to the assert(nbytes>0) failure to catch any EM bugs which might cause
853
+ ::Write to be called in a busy-loop.
854
+ */
855
+ #ifdef HAVE_KQUEUE
856
+ if (MyEventMachine->UsingKqueue()) {
857
+ if (OutboundDataSize == 0 && !bGotExtraKqueueEvent) {
858
+ bGotExtraKqueueEvent = true;
859
+ return;
860
+ } else if (OutboundDataSize > 0) {
861
+ bGotExtraKqueueEvent = false;
862
+ }
863
+ }
864
+ #endif
865
+
866
+ _WriteOutboundData();
867
+ }
868
+ }
869
+
870
+
871
+ /****************************************
872
+ ConnectionDescriptor::_WriteOutboundData
873
+ ****************************************/
874
+
875
+ void ConnectionDescriptor::_WriteOutboundData()
876
+ {
877
+ /* This is a helper function called by ::Write.
878
+ * It's possible for a socket to select writable and then no longer
879
+ * be writable by the time we get around to writing. The kernel might
880
+ * have used up its available output buffers between the select call
881
+ * and when we get here. So this condition is not an error.
882
+ *
883
+ * 20Jul07, added the same kind of protection against an invalid socket
884
+ * that is at the top of ::Read. Not entirely how this could happen in
885
+ * real life (connection-reset from the remote peer, perhaps?), but I'm
886
+ * doing it to address some reports of crashing under heavy loads.
887
+ */
888
+
889
+ int sd = GetSocket();
890
+ //assert (sd != INVALID_SOCKET);
891
+ if (sd == INVALID_SOCKET) {
892
+ assert (!bWriteAttemptedAfterClose);
893
+ bWriteAttemptedAfterClose = true;
894
+ return;
895
+ }
896
+
897
+ LastIo = gCurrentLoopTime;
898
+ size_t nbytes = 0;
899
+
900
+ #ifdef HAVE_WRITEV
901
+ int iovcnt = OutboundPages.size();
902
+ // Max of 16 outbound pages at a time
903
+ if (iovcnt > 16) iovcnt = 16;
904
+
905
+ #ifdef CC_SUNWspro
906
+ struct iovec iov[16];
907
+ #else
908
+ struct iovec iov[ iovcnt ];
909
+ #endif
910
+
911
+ for(int i = 0; i < iovcnt; i++){
912
+ OutboundPage *op = &(OutboundPages[i]);
913
+ #ifdef CC_SUNWspro
914
+ iov[i].iov_base = (char *)(op->Buffer + op->Offset);
915
+ #else
916
+ iov[i].iov_base = (void *)(op->Buffer + op->Offset);
917
+ #endif
918
+ iov[i].iov_len = op->Length - op->Offset;
919
+
920
+ nbytes += iov[i].iov_len;
921
+ }
922
+ #else
923
+ char output_buffer [16 * 1024];
924
+
925
+ while ((OutboundPages.size() > 0) && (nbytes < sizeof(output_buffer))) {
926
+ OutboundPage *op = &(OutboundPages[0]);
927
+ if ((nbytes + op->Length - op->Offset) < sizeof (output_buffer)) {
928
+ memcpy (output_buffer + nbytes, op->Buffer + op->Offset, op->Length - op->Offset);
929
+ nbytes += (op->Length - op->Offset);
930
+ op->Free();
931
+ OutboundPages.pop_front();
932
+ }
933
+ else {
934
+ int len = sizeof(output_buffer) - nbytes;
935
+ memcpy (output_buffer + nbytes, op->Buffer + op->Offset, len);
936
+ op->Offset += len;
937
+ nbytes += len;
938
+ }
939
+ }
940
+ #endif
941
+
942
+ // We should never have gotten here if there were no data to write,
943
+ // so assert that as a sanity check.
944
+ // Don't bother to make sure nbytes is less than output_buffer because
945
+ // if it were we probably would have crashed already.
946
+ assert (nbytes > 0);
947
+
948
+ assert (GetSocket() != INVALID_SOCKET);
949
+ #ifdef HAVE_WRITEV
950
+ int bytes_written = writev (GetSocket(), iov, iovcnt);
951
+ #else
952
+ int bytes_written = write (GetSocket(), output_buffer, nbytes);
953
+ #endif
954
+
955
+ bool err = false;
956
+ if (bytes_written < 0) {
957
+ err = true;
958
+ bytes_written = 0;
959
+ }
960
+
961
+ assert (bytes_written >= 0);
962
+ OutboundDataSize -= bytes_written;
963
+
964
+ if (ProxiedFrom && MaxOutboundBufSize && GetOutboundDataSize() < MaxOutboundBufSize && ProxiedFrom->IsPaused())
965
+ ProxiedFrom->Resume();
966
+
967
+ #ifdef HAVE_WRITEV
968
+ if (!err) {
969
+ unsigned int sent = bytes_written;
970
+ deque<OutboundPage>::iterator op = OutboundPages.begin();
971
+
972
+ for (int i = 0; i < iovcnt; i++) {
973
+ if (iov[i].iov_len <= sent) {
974
+ // Sent this page in full, free it.
975
+ op->Free();
976
+ OutboundPages.pop_front();
977
+
978
+ sent -= iov[i].iov_len;
979
+ } else {
980
+ // Sent part (or none) of this page, increment offset to send the remainder
981
+ op->Offset += sent;
982
+ break;
983
+ }
984
+
985
+ // Shouldn't be possible run out of pages before the loop ends
986
+ assert(op != OutboundPages.end());
987
+ *op++;
988
+ }
989
+ }
990
+ #else
991
+ if ((size_t)bytes_written < nbytes) {
992
+ int len = nbytes - bytes_written;
993
+ char *buffer = (char*) malloc (len + 1);
994
+ if (!buffer)
995
+ throw std::runtime_error ("bad alloc throwing back data");
996
+ memcpy (buffer, output_buffer + bytes_written, len);
997
+ buffer [len] = 0;
998
+ OutboundPages.push_front (OutboundPage (buffer, len));
999
+ }
1000
+ #endif
1001
+
1002
+ _UpdateEvents(false, true);
1003
+
1004
+ if (err) {
1005
+ #ifdef OS_UNIX
1006
+ if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK) && (errno != EINTR))
1007
+ #endif
1008
+ #ifdef OS_WIN32
1009
+ if ((errno != WSAEINPROGRESS) && (errno != WSAEWOULDBLOCK))
1010
+ #endif
1011
+ Close();
1012
+ }
1013
+ }
1014
+
1015
+
1016
+ /****************************************
1017
+ ConnectionDescriptor::_ReportErrorStatus
1018
+ ****************************************/
1019
+
1020
+ int ConnectionDescriptor::_ReportErrorStatus()
1021
+ {
1022
+ int error;
1023
+ socklen_t len;
1024
+ len = sizeof(error);
1025
+ #ifdef OS_UNIX
1026
+ int o = getsockopt (GetSocket(), SOL_SOCKET, SO_ERROR, &error, &len);
1027
+ #endif
1028
+ #ifdef OS_WIN32
1029
+ int o = getsockopt (GetSocket(), SOL_SOCKET, SO_ERROR, (char*)&error, &len);
1030
+ #endif
1031
+ if ((o == 0) && (error == 0))
1032
+ return 0;
1033
+ else
1034
+ return 1;
1035
+ }
1036
+
1037
+
1038
+ /******************************
1039
+ ConnectionDescriptor::StartTls
1040
+ ******************************/
1041
+
1042
+ void ConnectionDescriptor::StartTls()
1043
+ {
1044
+ #ifdef WITH_SSL
1045
+ if (SslBox)
1046
+ throw std::runtime_error ("SSL/TLS already running on connection");
1047
+
1048
+ SslBox = new SslBox_t (bIsServer, PrivateKeyFilename, CertChainFilename, bSslVerifyPeer, GetBinding());
1049
+ _DispatchCiphertext();
1050
+ #endif
1051
+
1052
+ #ifdef WITHOUT_SSL
1053
+ throw std::runtime_error ("Encryption not available on this event-machine");
1054
+ #endif
1055
+ }
1056
+
1057
+
1058
+ /*********************************
1059
+ ConnectionDescriptor::SetTlsParms
1060
+ *********************************/
1061
+
1062
+ void ConnectionDescriptor::SetTlsParms (const char *privkey_filename, const char *certchain_filename, bool verify_peer)
1063
+ {
1064
+ #ifdef WITH_SSL
1065
+ if (SslBox)
1066
+ throw std::runtime_error ("call SetTlsParms before calling StartTls");
1067
+ if (privkey_filename && *privkey_filename)
1068
+ PrivateKeyFilename = privkey_filename;
1069
+ if (certchain_filename && *certchain_filename)
1070
+ CertChainFilename = certchain_filename;
1071
+ bSslVerifyPeer = verify_peer;
1072
+ #endif
1073
+
1074
+ #ifdef WITHOUT_SSL
1075
+ throw std::runtime_error ("Encryption not available on this event-machine");
1076
+ #endif
1077
+ }
1078
+
1079
+
1080
+ /*********************************
1081
+ ConnectionDescriptor::GetPeerCert
1082
+ *********************************/
1083
+
1084
+ #ifdef WITH_SSL
1085
+ X509 *ConnectionDescriptor::GetPeerCert()
1086
+ {
1087
+ if (!SslBox)
1088
+ throw std::runtime_error ("SSL/TLS not running on this connection");
1089
+ return SslBox->GetPeerCert();
1090
+ }
1091
+ #endif
1092
+
1093
+
1094
+ /***********************************
1095
+ ConnectionDescriptor::VerifySslPeer
1096
+ ***********************************/
1097
+
1098
+ #ifdef WITH_SSL
1099
+ bool ConnectionDescriptor::VerifySslPeer(const char *cert)
1100
+ {
1101
+ bSslPeerAccepted = false;
1102
+
1103
+ if (EventCallback)
1104
+ (*EventCallback)(GetBinding(), EM_SSL_VERIFY, cert, strlen(cert));
1105
+
1106
+ return bSslPeerAccepted;
1107
+ }
1108
+ #endif
1109
+
1110
+
1111
+ /***********************************
1112
+ ConnectionDescriptor::AcceptSslPeer
1113
+ ***********************************/
1114
+
1115
+ #ifdef WITH_SSL
1116
+ void ConnectionDescriptor::AcceptSslPeer()
1117
+ {
1118
+ bSslPeerAccepted = true;
1119
+ }
1120
+ #endif
1121
+
1122
+
1123
+ /*****************************************
1124
+ ConnectionDescriptor::_DispatchCiphertext
1125
+ *****************************************/
1126
+
1127
+ #ifdef WITH_SSL
1128
+ void ConnectionDescriptor::_DispatchCiphertext()
1129
+ {
1130
+ assert (SslBox);
1131
+
1132
+
1133
+ char BigBuf [2048];
1134
+ bool did_work;
1135
+
1136
+ do {
1137
+ did_work = false;
1138
+
1139
+ // try to drain ciphertext
1140
+ while (SslBox->CanGetCiphertext()) {
1141
+ int r = SslBox->GetCiphertext (BigBuf, sizeof(BigBuf));
1142
+ assert (r > 0);
1143
+ _SendRawOutboundData (BigBuf, r);
1144
+ did_work = true;
1145
+ }
1146
+
1147
+ // Pump the SslBox, in case it has queued outgoing plaintext
1148
+ // This will return >0 if data was written,
1149
+ // 0 if no data was written, and <0 if there was a fatal error.
1150
+ bool pump;
1151
+ do {
1152
+ pump = false;
1153
+ int w = SslBox->PutPlaintext (NULL, 0);
1154
+ if (w > 0) {
1155
+ did_work = true;
1156
+ pump = true;
1157
+ }
1158
+ else if (w < 0)
1159
+ ScheduleClose (false);
1160
+ } while (pump);
1161
+
1162
+ // try to put plaintext. INCOMPLETE, doesn't belong here?
1163
+ // In SendOutboundData, we're spooling plaintext directly
1164
+ // into SslBox. That may be wrong, we may need to buffer it
1165
+ // up here!
1166
+ /*
1167
+ const char *ptr;
1168
+ int ptr_length;
1169
+ while (OutboundPlaintext.GetPage (&ptr, &ptr_length)) {
1170
+ assert (ptr && (ptr_length > 0));
1171
+ int w = SslMachine.PutPlaintext (ptr, ptr_length);
1172
+ if (w > 0) {
1173
+ OutboundPlaintext.DiscardBytes (w);
1174
+ did_work = true;
1175
+ }
1176
+ else
1177
+ break;
1178
+ }
1179
+ */
1180
+
1181
+ } while (did_work);
1182
+
1183
+ }
1184
+ #endif
1185
+
1186
+
1187
+
1188
+ /*******************************
1189
+ ConnectionDescriptor::Heartbeat
1190
+ *******************************/
1191
+
1192
+ void ConnectionDescriptor::Heartbeat()
1193
+ {
1194
+ /* Only allow a certain amount of time to go by while waiting
1195
+ * for a pending connect. If it expires, then kill the socket.
1196
+ * For a connected socket, close it if its inactivity timer
1197
+ * has expired.
1198
+ */
1199
+
1200
+ if (bConnectPending) {
1201
+ if ((gCurrentLoopTime - CreatedAt) >= PendingConnectTimeout)
1202
+ ScheduleClose (false);
1203
+ //bCloseNow = true;
1204
+ }
1205
+ else {
1206
+ if (InactivityTimeout && ((gCurrentLoopTime - LastIo) >= InactivityTimeout))
1207
+ ScheduleClose (false);
1208
+ //bCloseNow = true;
1209
+ }
1210
+ }
1211
+
1212
+
1213
+ /****************************************
1214
+ LoopbreakDescriptor::LoopbreakDescriptor
1215
+ ****************************************/
1216
+
1217
+ LoopbreakDescriptor::LoopbreakDescriptor (int sd, EventMachine_t *parent_em):
1218
+ EventableDescriptor (sd, parent_em)
1219
+ {
1220
+ /* This is really bad and ugly. Change someday if possible.
1221
+ * We have to know about an event-machine (probably the one that owns us),
1222
+ * so we can pass newly-created connections to it.
1223
+ */
1224
+
1225
+ bCallbackUnbind = false;
1226
+
1227
+ #ifdef HAVE_EPOLL
1228
+ EpollEvent.events = EPOLLIN;
1229
+ #endif
1230
+ #ifdef HAVE_KQUEUE
1231
+ MyEventMachine->ArmKqueueReader (this);
1232
+ #endif
1233
+ }
1234
+
1235
+
1236
+
1237
+
1238
+ /*************************
1239
+ LoopbreakDescriptor::Read
1240
+ *************************/
1241
+
1242
+ void LoopbreakDescriptor::Read()
1243
+ {
1244
+ // TODO, refactor, this code is probably in the wrong place.
1245
+ assert (MyEventMachine);
1246
+ MyEventMachine->_ReadLoopBreaker();
1247
+ }
1248
+
1249
+
1250
+ /**************************
1251
+ LoopbreakDescriptor::Write
1252
+ **************************/
1253
+
1254
+ void LoopbreakDescriptor::Write()
1255
+ {
1256
+ // Why are we here?
1257
+ throw std::runtime_error ("bad code path in loopbreak");
1258
+ }
1259
+
1260
+ /**************************************
1261
+ AcceptorDescriptor::AcceptorDescriptor
1262
+ **************************************/
1263
+
1264
+ AcceptorDescriptor::AcceptorDescriptor (int sd, EventMachine_t *parent_em):
1265
+ EventableDescriptor (sd, parent_em)
1266
+ {
1267
+ #ifdef HAVE_EPOLL
1268
+ EpollEvent.events = EPOLLIN;
1269
+ #endif
1270
+ #ifdef HAVE_KQUEUE
1271
+ MyEventMachine->ArmKqueueReader (this);
1272
+ #endif
1273
+ }
1274
+
1275
+
1276
+ /***************************************
1277
+ AcceptorDescriptor::~AcceptorDescriptor
1278
+ ***************************************/
1279
+
1280
+ AcceptorDescriptor::~AcceptorDescriptor()
1281
+ {
1282
+ }
1283
+
1284
+ /****************************************
1285
+ STATIC: AcceptorDescriptor::StopAcceptor
1286
+ ****************************************/
1287
+
1288
+ void AcceptorDescriptor::StopAcceptor (const unsigned long binding)
1289
+ {
1290
+ // TODO: This is something of a hack, or at least it's a static method of the wrong class.
1291
+ AcceptorDescriptor *ad = dynamic_cast <AcceptorDescriptor*> (Bindable_t::GetObject (binding));
1292
+ if (ad)
1293
+ ad->ScheduleClose (false);
1294
+ else
1295
+ throw std::runtime_error ("failed to close nonexistent acceptor");
1296
+ }
1297
+
1298
+
1299
+ /************************
1300
+ AcceptorDescriptor::Read
1301
+ ************************/
1302
+
1303
+ void AcceptorDescriptor::Read()
1304
+ {
1305
+ /* Accept up to a certain number of sockets on the listening connection.
1306
+ * Don't try to accept all that are present, because this would allow a DoS attack
1307
+ * in which no data were ever read or written. We should accept more than one,
1308
+ * if available, to keep the partially accepted sockets from backing up in the kernel.
1309
+ */
1310
+
1311
+ /* Make sure we use non-blocking i/o on the acceptor socket, since we're selecting it
1312
+ * for readability. According to Stevens UNP, it's possible for an acceptor to select readable
1313
+ * and then block when we call accept. For example, the other end resets the connection after
1314
+ * the socket selects readable and before we call accept. The kernel will remove the dead
1315
+ * socket from the accept queue. If the accept queue is now empty, accept will block.
1316
+ */
1317
+
1318
+
1319
+ struct sockaddr_in pin;
1320
+ socklen_t addrlen = sizeof (pin);
1321
+
1322
+ for (int i=0; i < 10; i++) {
1323
+ int sd = accept (GetSocket(), (struct sockaddr*)&pin, &addrlen);
1324
+ if (sd == INVALID_SOCKET) {
1325
+ // This breaks the loop when we've accepted everything on the kernel queue,
1326
+ // up to 10 new connections. But what if the *first* accept fails?
1327
+ // Does that mean anything serious is happening, beyond the situation
1328
+ // described in the note above?
1329
+ break;
1330
+ }
1331
+
1332
+ // Set the newly-accepted socket non-blocking.
1333
+ // On Windows, this may fail because, weirdly, Windows inherits the non-blocking
1334
+ // attribute that we applied to the acceptor socket into the accepted one.
1335
+ if (!SetSocketNonblocking (sd)) {
1336
+ //int val = fcntl (sd, F_GETFL, 0);
1337
+ //if (fcntl (sd, F_SETFL, val | O_NONBLOCK) == -1) {
1338
+ shutdown (sd, 1);
1339
+ closesocket (sd);
1340
+ continue;
1341
+ }
1342
+
1343
+
1344
+ // Disable slow-start (Nagle algorithm). Eventually make this configurable.
1345
+ int one = 1;
1346
+ setsockopt (sd, IPPROTO_TCP, TCP_NODELAY, (char*) &one, sizeof(one));
1347
+
1348
+
1349
+ ConnectionDescriptor *cd = new ConnectionDescriptor (sd, MyEventMachine);
1350
+ if (!cd)
1351
+ throw std::runtime_error ("no newly accepted connection");
1352
+ cd->SetServerMode();
1353
+ if (EventCallback) {
1354
+ (*EventCallback) (GetBinding(), EM_CONNECTION_ACCEPTED, NULL, cd->GetBinding());
1355
+ }
1356
+ #ifdef HAVE_EPOLL
1357
+ cd->GetEpollEvent()->events = EPOLLIN | (cd->SelectForWrite() ? EPOLLOUT : 0);
1358
+ #endif
1359
+ assert (MyEventMachine);
1360
+ MyEventMachine->Add (cd);
1361
+ #ifdef HAVE_KQUEUE
1362
+ if (cd->SelectForWrite())
1363
+ MyEventMachine->ArmKqueueWriter (cd);
1364
+ MyEventMachine->ArmKqueueReader (cd);
1365
+ #endif
1366
+ }
1367
+
1368
+ }
1369
+
1370
+
1371
+ /*************************
1372
+ AcceptorDescriptor::Write
1373
+ *************************/
1374
+
1375
+ void AcceptorDescriptor::Write()
1376
+ {
1377
+ // Why are we here?
1378
+ throw std::runtime_error ("bad code path in acceptor");
1379
+ }
1380
+
1381
+
1382
+ /*****************************
1383
+ AcceptorDescriptor::Heartbeat
1384
+ *****************************/
1385
+
1386
+ void AcceptorDescriptor::Heartbeat()
1387
+ {
1388
+ // No-op
1389
+ }
1390
+
1391
+
1392
+ /*******************************
1393
+ AcceptorDescriptor::GetSockname
1394
+ *******************************/
1395
+
1396
+ bool AcceptorDescriptor::GetSockname (struct sockaddr *s)
1397
+ {
1398
+ bool ok = false;
1399
+ if (s) {
1400
+ socklen_t len = sizeof(*s);
1401
+ int gp = getsockname (GetSocket(), s, &len);
1402
+ if (gp == 0)
1403
+ ok = true;
1404
+ }
1405
+ return ok;
1406
+ }
1407
+
1408
+
1409
+
1410
+ /**************************************
1411
+ DatagramDescriptor::DatagramDescriptor
1412
+ **************************************/
1413
+
1414
+ DatagramDescriptor::DatagramDescriptor (int sd, EventMachine_t *parent_em):
1415
+ EventableDescriptor (sd, parent_em),
1416
+ OutboundDataSize (0),
1417
+ LastIo (gCurrentLoopTime),
1418
+ InactivityTimeout (0)
1419
+ {
1420
+ memset (&ReturnAddress, 0, sizeof(ReturnAddress));
1421
+
1422
+ /* Provisionally added 19Oct07. All datagram sockets support broadcasting.
1423
+ * Until now, sending to a broadcast address would give EACCES (permission denied)
1424
+ * on systems like Linux and BSD that require the SO_BROADCAST socket-option in order
1425
+ * to accept a packet to a broadcast address. Solaris doesn't require it. I think
1426
+ * Windows DOES require it but I'm not sure.
1427
+ *
1428
+ * Ruby does NOT do what we're doing here. In Ruby, you have to explicitly set SO_BROADCAST
1429
+ * on a UDP socket in order to enable broadcasting. The reason for requiring the option
1430
+ * in the first place is so that applications don't send broadcast datagrams by mistake.
1431
+ * I imagine that could happen if a user of an application typed in an address that happened
1432
+ * to be a broadcast address on that particular subnet.
1433
+ *
1434
+ * This is provisional because someone may eventually come up with a good reason not to
1435
+ * do it for all UDP sockets. If that happens, then we'll need to add a usercode-level API
1436
+ * to set the socket option, just like Ruby does. AND WE'LL ALSO BREAK CODE THAT DOESN'T
1437
+ * EXPLICITLY SET THE OPTION.
1438
+ */
1439
+
1440
+ int oval = 1;
1441
+ setsockopt (GetSocket(), SOL_SOCKET, SO_BROADCAST, (char*)&oval, sizeof(oval));
1442
+
1443
+ #ifdef HAVE_EPOLL
1444
+ EpollEvent.events = EPOLLIN;
1445
+ #endif
1446
+ #ifdef HAVE_KQUEUE
1447
+ MyEventMachine->ArmKqueueReader (this);
1448
+ #endif
1449
+ }
1450
+
1451
+
1452
+ /***************************************
1453
+ DatagramDescriptor::~DatagramDescriptor
1454
+ ***************************************/
1455
+
1456
+ DatagramDescriptor::~DatagramDescriptor()
1457
+ {
1458
+ // Run down any stranded outbound data.
1459
+ for (size_t i=0; i < OutboundPages.size(); i++)
1460
+ OutboundPages[i].Free();
1461
+ }
1462
+
1463
+
1464
+ /*****************************
1465
+ DatagramDescriptor::Heartbeat
1466
+ *****************************/
1467
+
1468
+ void DatagramDescriptor::Heartbeat()
1469
+ {
1470
+ // Close it if its inactivity timer has expired.
1471
+
1472
+ if (InactivityTimeout && ((gCurrentLoopTime - LastIo) >= InactivityTimeout))
1473
+ ScheduleClose (false);
1474
+ //bCloseNow = true;
1475
+ }
1476
+
1477
+
1478
+ /************************
1479
+ DatagramDescriptor::Read
1480
+ ************************/
1481
+
1482
+ void DatagramDescriptor::Read()
1483
+ {
1484
+ int sd = GetSocket();
1485
+ assert (sd != INVALID_SOCKET);
1486
+ LastIo = gCurrentLoopTime;
1487
+
1488
+ // This is an extremely large read buffer.
1489
+ // In many cases you wouldn't expect to get any more than 4K.
1490
+ char readbuffer [16 * 1024];
1491
+
1492
+ for (int i=0; i < 10; i++) {
1493
+ // Don't read just one buffer and then move on. This is faster
1494
+ // if there is a lot of incoming.
1495
+ // But don't read indefinitely. Give other sockets a chance to run.
1496
+ // NOTICE, we're reading one less than the buffer size.
1497
+ // That's so we can put a guard byte at the end of what we send
1498
+ // to user code.
1499
+
1500
+ struct sockaddr_in sin;
1501
+ socklen_t slen = sizeof (sin);
1502
+ memset (&sin, 0, slen);
1503
+
1504
+ int r = recvfrom (sd, readbuffer, sizeof(readbuffer) - 1, 0, (struct sockaddr*)&sin, &slen);
1505
+ //cerr << "<R:" << r << ">";
1506
+
1507
+ // In UDP, a zero-length packet is perfectly legal.
1508
+ if (r >= 0) {
1509
+
1510
+ // Add a null-terminator at the the end of the buffer
1511
+ // that we will send to the callback.
1512
+ // DO NOT EVER CHANGE THIS. We want to explicitly allow users
1513
+ // to be able to depend on this behavior, so they will have
1514
+ // the option to do some things faster. Additionally it's
1515
+ // a security guard against buffer overflows.
1516
+ readbuffer [r] = 0;
1517
+
1518
+
1519
+ // Set up a "temporary" return address so that callers can "reply" to us
1520
+ // from within the callback we are about to invoke. That means that ordinary
1521
+ // calls to "send_data_to_connection" (which is of course misnamed in this
1522
+ // case) will result in packets being sent back to the same place that sent
1523
+ // us this one.
1524
+ // There is a different call (evma_send_datagram) for cases where the caller
1525
+ // actually wants to send a packet somewhere else.
1526
+
1527
+ memset (&ReturnAddress, 0, sizeof(ReturnAddress));
1528
+ memcpy (&ReturnAddress, &sin, slen);
1529
+
1530
+ _GenericInboundDispatch(readbuffer, r);
1531
+
1532
+ }
1533
+ else {
1534
+ // Basically a would-block, meaning we've read everything there is to read.
1535
+ break;
1536
+ }
1537
+
1538
+ }
1539
+
1540
+
1541
+ }
1542
+
1543
+
1544
+ /*************************
1545
+ DatagramDescriptor::Write
1546
+ *************************/
1547
+
1548
+ void DatagramDescriptor::Write()
1549
+ {
1550
+ /* It's possible for a socket to select writable and then no longer
1551
+ * be writable by the time we get around to writing. The kernel might
1552
+ * have used up its available output buffers between the select call
1553
+ * and when we get here. So this condition is not an error.
1554
+ * This code is very reminiscent of ConnectionDescriptor::_WriteOutboundData,
1555
+ * but differs in the that the outbound data pages (received from the
1556
+ * user) are _message-structured._ That is, we send each of them out
1557
+ * one message at a time.
1558
+ * TODO, we are currently suppressing the EMSGSIZE error!!!
1559
+ */
1560
+
1561
+ int sd = GetSocket();
1562
+ assert (sd != INVALID_SOCKET);
1563
+ LastIo = gCurrentLoopTime;
1564
+
1565
+ assert (OutboundPages.size() > 0);
1566
+
1567
+ // Send out up to 10 packets, then cycle the machine.
1568
+ for (int i = 0; i < 10; i++) {
1569
+ if (OutboundPages.size() <= 0)
1570
+ break;
1571
+ OutboundPage *op = &(OutboundPages[0]);
1572
+
1573
+ // The nasty cast to (char*) is needed because Windows is brain-dead.
1574
+ int s = sendto (sd, (char*)op->Buffer, op->Length, 0, (struct sockaddr*)&(op->From), sizeof(op->From));
1575
+ int e = errno;
1576
+
1577
+ OutboundDataSize -= op->Length;
1578
+ op->Free();
1579
+ OutboundPages.pop_front();
1580
+
1581
+ if (s == SOCKET_ERROR) {
1582
+ #ifdef OS_UNIX
1583
+ if ((e != EINPROGRESS) && (e != EWOULDBLOCK) && (e != EINTR)) {
1584
+ #endif
1585
+ #ifdef OS_WIN32
1586
+ if ((e != WSAEINPROGRESS) && (e != WSAEWOULDBLOCK)) {
1587
+ #endif
1588
+ Close();
1589
+ break;
1590
+ }
1591
+ }
1592
+ }
1593
+
1594
+ #ifdef HAVE_EPOLL
1595
+ EpollEvent.events = (EPOLLIN | (SelectForWrite() ? EPOLLOUT : 0));
1596
+ assert (MyEventMachine);
1597
+ MyEventMachine->Modify (this);
1598
+ #endif
1599
+ #ifdef HAVE_KQUEUE
1600
+ if (SelectForWrite())
1601
+ MyEventMachine->ArmKqueueWriter (this);
1602
+ #endif
1603
+ }
1604
+
1605
+
1606
+ /**********************************
1607
+ DatagramDescriptor::SelectForWrite
1608
+ **********************************/
1609
+
1610
+ bool DatagramDescriptor::SelectForWrite()
1611
+ {
1612
+ /* Changed 15Nov07, per bug report by Mark Zvillius.
1613
+ * The outbound data size will be zero if there are zero-length outbound packets,
1614
+ * so we now select writable in case the outbound page buffer is not empty.
1615
+ * Note that the superclass ShouldDelete method still checks for outbound data size,
1616
+ * which may be wrong.
1617
+ */
1618
+ //return (GetOutboundDataSize() > 0); (Original)
1619
+ return (OutboundPages.size() > 0);
1620
+ }
1621
+
1622
+
1623
+ /************************************
1624
+ DatagramDescriptor::SendOutboundData
1625
+ ************************************/
1626
+
1627
+ int DatagramDescriptor::SendOutboundData (const char *data, int length)
1628
+ {
1629
+ // This is an exact clone of ConnectionDescriptor::SendOutboundData.
1630
+ // That means it needs to move to a common ancestor.
1631
+
1632
+ if (IsCloseScheduled())
1633
+ //if (bCloseNow || bCloseAfterWriting)
1634
+ return 0;
1635
+
1636
+ if (!data && (length > 0))
1637
+ throw std::runtime_error ("bad outbound data");
1638
+ char *buffer = (char *) malloc (length + 1);
1639
+ if (!buffer)
1640
+ throw std::runtime_error ("no allocation for outbound data");
1641
+ memcpy (buffer, data, length);
1642
+ buffer [length] = 0;
1643
+ OutboundPages.push_back (OutboundPage (buffer, length, ReturnAddress));
1644
+ OutboundDataSize += length;
1645
+
1646
+ #ifdef HAVE_EPOLL
1647
+ EpollEvent.events = (EPOLLIN | EPOLLOUT);
1648
+ assert (MyEventMachine);
1649
+ MyEventMachine->Modify (this);
1650
+ #endif
1651
+ #ifdef HAVE_KQUEUE
1652
+ MyEventMachine->ArmKqueueWriter (this);
1653
+ #endif
1654
+
1655
+ return length;
1656
+ }
1657
+
1658
+
1659
+ /****************************************
1660
+ DatagramDescriptor::SendOutboundDatagram
1661
+ ****************************************/
1662
+
1663
+ int DatagramDescriptor::SendOutboundDatagram (const char *data, int length, const char *address, int port)
1664
+ {
1665
+ // This is an exact clone of ConnectionDescriptor::SendOutboundData.
1666
+ // That means it needs to move to a common ancestor.
1667
+ // TODO: Refactor this so there's no overlap with SendOutboundData.
1668
+
1669
+ if (IsCloseScheduled())
1670
+ //if (bCloseNow || bCloseAfterWriting)
1671
+ return 0;
1672
+
1673
+ if (!address || !*address || !port)
1674
+ return 0;
1675
+
1676
+ sockaddr_in pin;
1677
+ unsigned long HostAddr;
1678
+
1679
+ HostAddr = inet_addr (address);
1680
+ if (HostAddr == INADDR_NONE) {
1681
+ // The nasty cast to (char*) is because Windows is brain-dead.
1682
+ hostent *hp = gethostbyname ((char*)address);
1683
+ if (!hp)
1684
+ return 0;
1685
+ HostAddr = ((in_addr*)(hp->h_addr))->s_addr;
1686
+ }
1687
+
1688
+ memset (&pin, 0, sizeof(pin));
1689
+ pin.sin_family = AF_INET;
1690
+ pin.sin_addr.s_addr = HostAddr;
1691
+ pin.sin_port = htons (port);
1692
+
1693
+
1694
+ if (!data && (length > 0))
1695
+ throw std::runtime_error ("bad outbound data");
1696
+ char *buffer = (char *) malloc (length + 1);
1697
+ if (!buffer)
1698
+ throw std::runtime_error ("no allocation for outbound data");
1699
+ memcpy (buffer, data, length);
1700
+ buffer [length] = 0;
1701
+ OutboundPages.push_back (OutboundPage (buffer, length, pin));
1702
+ OutboundDataSize += length;
1703
+
1704
+ #ifdef HAVE_EPOLL
1705
+ EpollEvent.events = (EPOLLIN | EPOLLOUT);
1706
+ assert (MyEventMachine);
1707
+ MyEventMachine->Modify (this);
1708
+ #endif
1709
+ #ifdef HAVE_KQUEUE
1710
+ MyEventMachine->ArmKqueueWriter (this);
1711
+ #endif
1712
+
1713
+ return length;
1714
+ }
1715
+
1716
+
1717
+ /****************************************
1718
+ STATIC: DatagramDescriptor::SendDatagram
1719
+ ****************************************/
1720
+
1721
+ int DatagramDescriptor::SendDatagram (const unsigned long binding, const char *data, int length, const char *address, int port)
1722
+ {
1723
+ DatagramDescriptor *dd = dynamic_cast <DatagramDescriptor*> (Bindable_t::GetObject (binding));
1724
+ if (dd)
1725
+ return dd->SendOutboundDatagram (data, length, address, port);
1726
+ else
1727
+ return -1;
1728
+ }
1729
+
1730
+
1731
+ /*********************************
1732
+ ConnectionDescriptor::GetPeername
1733
+ *********************************/
1734
+
1735
+ bool ConnectionDescriptor::GetPeername (struct sockaddr *s)
1736
+ {
1737
+ bool ok = false;
1738
+ if (s) {
1739
+ socklen_t len = sizeof(*s);
1740
+ int gp = getpeername (GetSocket(), s, &len);
1741
+ if (gp == 0)
1742
+ ok = true;
1743
+ }
1744
+ return ok;
1745
+ }
1746
+
1747
+ /*********************************
1748
+ ConnectionDescriptor::GetSockname
1749
+ *********************************/
1750
+
1751
+ bool ConnectionDescriptor::GetSockname (struct sockaddr *s)
1752
+ {
1753
+ bool ok = false;
1754
+ if (s) {
1755
+ socklen_t len = sizeof(*s);
1756
+ int gp = getsockname (GetSocket(), s, &len);
1757
+ if (gp == 0)
1758
+ ok = true;
1759
+ }
1760
+ return ok;
1761
+ }
1762
+
1763
+
1764
+ /**********************************************
1765
+ ConnectionDescriptor::GetCommInactivityTimeout
1766
+ **********************************************/
1767
+
1768
+ float ConnectionDescriptor::GetCommInactivityTimeout()
1769
+ {
1770
+ return ((float)InactivityTimeout / 1000000);
1771
+ }
1772
+
1773
+
1774
+ /**********************************************
1775
+ ConnectionDescriptor::SetCommInactivityTimeout
1776
+ **********************************************/
1777
+
1778
+ int ConnectionDescriptor::SetCommInactivityTimeout (float value)
1779
+ {
1780
+ if (value > 0) {
1781
+ InactivityTimeout = (Int64)(value * 1000000);
1782
+ return 1;
1783
+ }
1784
+ return 0;
1785
+ }
1786
+
1787
+ /*******************************
1788
+ DatagramDescriptor::GetPeername
1789
+ *******************************/
1790
+
1791
+ bool DatagramDescriptor::GetPeername (struct sockaddr *s)
1792
+ {
1793
+ bool ok = false;
1794
+ if (s) {
1795
+ memset (s, 0, sizeof(struct sockaddr));
1796
+ memcpy (s, &ReturnAddress, sizeof(ReturnAddress));
1797
+ ok = true;
1798
+ }
1799
+ return ok;
1800
+ }
1801
+
1802
+ /*******************************
1803
+ DatagramDescriptor::GetSockname
1804
+ *******************************/
1805
+
1806
+ bool DatagramDescriptor::GetSockname (struct sockaddr *s)
1807
+ {
1808
+ bool ok = false;
1809
+ if (s) {
1810
+ socklen_t len = sizeof(*s);
1811
+ int gp = getsockname (GetSocket(), s, &len);
1812
+ if (gp == 0)
1813
+ ok = true;
1814
+ }
1815
+ return ok;
1816
+ }
1817
+
1818
+
1819
+
1820
+ /********************************************
1821
+ DatagramDescriptor::GetCommInactivityTimeout
1822
+ ********************************************/
1823
+
1824
+ float DatagramDescriptor::GetCommInactivityTimeout()
1825
+ {
1826
+ return ((float)InactivityTimeout / 1000000);
1827
+ }
1828
+
1829
+ /********************************************
1830
+ DatagramDescriptor::SetCommInactivityTimeout
1831
+ ********************************************/
1832
+
1833
+ int DatagramDescriptor::SetCommInactivityTimeout (float value)
1834
+ {
1835
+ if (value > 0) {
1836
+ InactivityTimeout = (Int64)(value * 1000000);
1837
+ return 1;
1838
+ }
1839
+ return 0;
1840
+ }
1841
+
1842
+
1843
+ /************************************
1844
+ InotifyDescriptor::InotifyDescriptor
1845
+ *************************************/
1846
+
1847
+ InotifyDescriptor::InotifyDescriptor (EventMachine_t *em):
1848
+ EventableDescriptor(0, em)
1849
+ {
1850
+ bCallbackUnbind = false;
1851
+
1852
+ #ifndef HAVE_INOTIFY
1853
+ throw std::runtime_error("no inotify support on this system");
1854
+ #else
1855
+
1856
+ int fd = inotify_init();
1857
+ if (fd == -1) {
1858
+ char buf[200];
1859
+ snprintf (buf, sizeof(buf)-1, "unable to create inotify descriptor: %s", strerror(errno));
1860
+ throw std::runtime_error (buf);
1861
+ }
1862
+
1863
+ MySocket = fd;
1864
+ SetSocketNonblocking(MySocket);
1865
+ #ifdef HAVE_EPOLL
1866
+ EpollEvent.events = EPOLLIN;
1867
+ #endif
1868
+
1869
+ #endif
1870
+ }
1871
+
1872
+
1873
+ /*************************************
1874
+ InotifyDescriptor::~InotifyDescriptor
1875
+ **************************************/
1876
+
1877
+ InotifyDescriptor::~InotifyDescriptor()
1878
+ {
1879
+ close(MySocket);
1880
+ MySocket = INVALID_SOCKET;
1881
+ }
1882
+
1883
+ /***********************
1884
+ InotifyDescriptor::Read
1885
+ ************************/
1886
+
1887
+ void InotifyDescriptor::Read()
1888
+ {
1889
+ assert (MyEventMachine);
1890
+ MyEventMachine->_ReadInotifyEvents();
1891
+ }
1892
+
1893
+
1894
+ /************************
1895
+ InotifyDescriptor::Write
1896
+ *************************/
1897
+
1898
+ void InotifyDescriptor::Write()
1899
+ {
1900
+ throw std::runtime_error("bad code path in inotify");
1901
+ }