crusher-eventmachine 0.12.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/README +81 -0
  2. data/Rakefile +370 -0
  3. data/docs/COPYING +60 -0
  4. data/docs/ChangeLog +211 -0
  5. data/docs/DEFERRABLES +246 -0
  6. data/docs/EPOLL +141 -0
  7. data/docs/GNU +281 -0
  8. data/docs/INSTALL +13 -0
  9. data/docs/KEYBOARD +42 -0
  10. data/docs/LEGAL +25 -0
  11. data/docs/LIGHTWEIGHT_CONCURRENCY +130 -0
  12. data/docs/PURE_RUBY +75 -0
  13. data/docs/RELEASE_NOTES +94 -0
  14. data/docs/SMTP +4 -0
  15. data/docs/SPAWNED_PROCESSES +148 -0
  16. data/docs/TODO +8 -0
  17. data/eventmachine.gemspec +40 -0
  18. data/examples/ex_channel.rb +43 -0
  19. data/examples/ex_queue.rb +2 -0
  20. data/examples/ex_tick_loop_array.rb +15 -0
  21. data/examples/ex_tick_loop_counter.rb +32 -0
  22. data/examples/helper.rb +2 -0
  23. data/ext/binder.cpp +124 -0
  24. data/ext/binder.h +46 -0
  25. data/ext/cmain.cpp +852 -0
  26. data/ext/cplusplus.cpp +202 -0
  27. data/ext/ed.cpp +1884 -0
  28. data/ext/ed.h +418 -0
  29. data/ext/em.cpp +2377 -0
  30. data/ext/em.h +239 -0
  31. data/ext/emwin.cpp +300 -0
  32. data/ext/emwin.h +94 -0
  33. data/ext/epoll.cpp +26 -0
  34. data/ext/epoll.h +25 -0
  35. data/ext/eventmachine.h +124 -0
  36. data/ext/eventmachine_cpp.h +96 -0
  37. data/ext/extconf.rb +150 -0
  38. data/ext/fastfilereader/extconf.rb +85 -0
  39. data/ext/fastfilereader/mapper.cpp +214 -0
  40. data/ext/fastfilereader/mapper.h +59 -0
  41. data/ext/fastfilereader/rubymain.cpp +127 -0
  42. data/ext/files.cpp +94 -0
  43. data/ext/files.h +65 -0
  44. data/ext/kb.cpp +79 -0
  45. data/ext/page.cpp +107 -0
  46. data/ext/page.h +51 -0
  47. data/ext/pipe.cpp +347 -0
  48. data/ext/project.h +157 -0
  49. data/ext/rubymain.cpp +1215 -0
  50. data/ext/sigs.cpp +89 -0
  51. data/ext/sigs.h +32 -0
  52. data/ext/ssl.cpp +460 -0
  53. data/ext/ssl.h +94 -0
  54. data/java/.classpath +8 -0
  55. data/java/.project +17 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  61. data/java/src/com/rubyeventmachine/application/Application.java +194 -0
  62. data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
  63. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
  64. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
  65. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
  66. data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
  67. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
  68. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -0
  69. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  70. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  71. data/java/src/com/rubyeventmachine/tests/TestServers.java +75 -0
  72. data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -0
  73. data/lib/em/buftok.rb +138 -0
  74. data/lib/em/callback.rb +26 -0
  75. data/lib/em/channel.rb +57 -0
  76. data/lib/em/connection.rb +569 -0
  77. data/lib/em/deferrable.rb +206 -0
  78. data/lib/em/file_watch.rb +54 -0
  79. data/lib/em/future.rb +61 -0
  80. data/lib/em/iterator.rb +270 -0
  81. data/lib/em/messages.rb +66 -0
  82. data/lib/em/process_watch.rb +44 -0
  83. data/lib/em/processes.rb +119 -0
  84. data/lib/em/protocols.rb +36 -0
  85. data/lib/em/protocols/header_and_content.rb +138 -0
  86. data/lib/em/protocols/httpclient.rb +276 -0
  87. data/lib/em/protocols/httpclient2.rb +590 -0
  88. data/lib/em/protocols/line_and_text.rb +125 -0
  89. data/lib/em/protocols/linetext2.rb +161 -0
  90. data/lib/em/protocols/memcache.rb +323 -0
  91. data/lib/em/protocols/object_protocol.rb +45 -0
  92. data/lib/em/protocols/postgres3.rb +247 -0
  93. data/lib/em/protocols/saslauth.rb +175 -0
  94. data/lib/em/protocols/smtpclient.rb +357 -0
  95. data/lib/em/protocols/smtpserver.rb +640 -0
  96. data/lib/em/protocols/socks4.rb +66 -0
  97. data/lib/em/protocols/stomp.rb +200 -0
  98. data/lib/em/protocols/tcptest.rb +53 -0
  99. data/lib/em/pure_ruby.rb +1019 -0
  100. data/lib/em/queue.rb +62 -0
  101. data/lib/em/spawnable.rb +85 -0
  102. data/lib/em/streamer.rb +130 -0
  103. data/lib/em/tick_loop.rb +85 -0
  104. data/lib/em/timers.rb +57 -0
  105. data/lib/em/version.rb +3 -0
  106. data/lib/eventmachine.rb +1540 -0
  107. data/lib/evma.rb +32 -0
  108. data/lib/evma/callback.rb +32 -0
  109. data/lib/evma/container.rb +75 -0
  110. data/lib/evma/factory.rb +77 -0
  111. data/lib/evma/protocol.rb +87 -0
  112. data/lib/evma/reactor.rb +48 -0
  113. data/lib/jeventmachine.rb +257 -0
  114. data/setup.rb +1585 -0
  115. data/tasks/cpp.rake_example +77 -0
  116. data/tests/client.crt +31 -0
  117. data/tests/client.key +51 -0
  118. data/tests/test_attach.rb +136 -0
  119. data/tests/test_basic.rb +249 -0
  120. data/tests/test_channel.rb +63 -0
  121. data/tests/test_connection_count.rb +35 -0
  122. data/tests/test_defer.rb +49 -0
  123. data/tests/test_deferrable.rb +35 -0
  124. data/tests/test_epoll.rb +160 -0
  125. data/tests/test_error_handler.rb +35 -0
  126. data/tests/test_errors.rb +82 -0
  127. data/tests/test_exc.rb +55 -0
  128. data/tests/test_file_watch.rb +49 -0
  129. data/tests/test_futures.rb +198 -0
  130. data/tests/test_get_sock_opt.rb +30 -0
  131. data/tests/test_handler_check.rb +37 -0
  132. data/tests/test_hc.rb +190 -0
  133. data/tests/test_httpclient.rb +227 -0
  134. data/tests/test_httpclient2.rb +154 -0
  135. data/tests/test_inactivity_timeout.rb +50 -0
  136. data/tests/test_kb.rb +60 -0
  137. data/tests/test_ltp.rb +190 -0
  138. data/tests/test_ltp2.rb +317 -0
  139. data/tests/test_next_tick.rb +133 -0
  140. data/tests/test_object_protocol.rb +37 -0
  141. data/tests/test_pause.rb +70 -0
  142. data/tests/test_pending_connect_timeout.rb +48 -0
  143. data/tests/test_process_watch.rb +50 -0
  144. data/tests/test_processes.rb +128 -0
  145. data/tests/test_proxy_connection.rb +144 -0
  146. data/tests/test_pure.rb +134 -0
  147. data/tests/test_queue.rb +44 -0
  148. data/tests/test_running.rb +42 -0
  149. data/tests/test_sasl.rb +72 -0
  150. data/tests/test_send_file.rb +251 -0
  151. data/tests/test_servers.rb +76 -0
  152. data/tests/test_smtpclient.rb +83 -0
  153. data/tests/test_smtpserver.rb +85 -0
  154. data/tests/test_spawn.rb +322 -0
  155. data/tests/test_ssl_args.rb +79 -0
  156. data/tests/test_ssl_methods.rb +50 -0
  157. data/tests/test_ssl_verify.rb +82 -0
  158. data/tests/test_tick_loop.rb +59 -0
  159. data/tests/test_timers.rb +160 -0
  160. data/tests/test_ud.rb +36 -0
  161. data/tests/testem.rb +31 -0
  162. metadata +251 -0
data/ext/cplusplus.cpp ADDED
@@ -0,0 +1,202 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: cplusplus.cpp
6
+ Date: 27Jul07
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
+
21
+ #include "project.h"
22
+
23
+
24
+ namespace EM {
25
+ static map<unsigned long, Eventable*> Eventables;
26
+ static map<unsigned long, void(*)()> Timers;
27
+ }
28
+
29
+
30
+ /*******
31
+ EM::Run
32
+ *******/
33
+
34
+ void EM::Run (void (*start_func)())
35
+ {
36
+ evma_set_epoll (1);
37
+ evma_initialize_library ((EMCallback)EM::Callback);
38
+ if (start_func)
39
+ AddTimer (0, start_func);
40
+ evma_run_machine();
41
+ evma_release_library();
42
+ }
43
+
44
+ /************
45
+ EM::AddTimer
46
+ ************/
47
+
48
+ void EM::AddTimer (int milliseconds, void (*func)())
49
+ {
50
+ if (func) {
51
+ const unsigned long sig = evma_install_oneshot_timer (milliseconds);
52
+ #ifndef HAVE_MAKE_PAIR
53
+ Timers.insert (map<unsigned long, void(*)()>::value_type (sig, func));
54
+ #else
55
+ Timers.insert (make_pair (sig, func));
56
+ #endif
57
+ }
58
+ }
59
+
60
+
61
+ /***************
62
+ EM::StopReactor
63
+ ***************/
64
+
65
+ void EM::StopReactor()
66
+ {
67
+ evma_stop_machine();
68
+ }
69
+
70
+
71
+ /********************
72
+ EM::Acceptor::Accept
73
+ ********************/
74
+
75
+ void EM::Acceptor::Accept (const unsigned long signature)
76
+ {
77
+ Connection *c = MakeConnection();
78
+ c->Signature = signature;
79
+ #ifndef HAVE_MAKE_PAIR
80
+ Eventables.insert (std::map<unsigned long,EM::Eventable*>::value_type (c->Signature, c));
81
+ #else
82
+ Eventables.insert (make_pair (c->Signature, c));
83
+ #endif
84
+ c->PostInit();
85
+ }
86
+
87
+ /************************
88
+ EM::Connection::SendData
89
+ ************************/
90
+
91
+ void EM::Connection::SendData (const char *data)
92
+ {
93
+ if (data)
94
+ SendData (data, strlen (data));
95
+ }
96
+
97
+
98
+ /************************
99
+ EM::Connection::SendData
100
+ ************************/
101
+
102
+ void EM::Connection::SendData (const char *data, int length)
103
+ {
104
+ evma_send_data_to_connection (Signature, data, length);
105
+ }
106
+
107
+
108
+ /*********************
109
+ EM::Connection::Close
110
+ *********************/
111
+
112
+ void EM::Connection::Close (bool afterWriting)
113
+ {
114
+ evma_close_connection (Signature, afterWriting);
115
+ }
116
+
117
+
118
+ /***************************
119
+ EM::Connection::BindConnect
120
+ ***************************/
121
+
122
+ void EM::Connection::BindConnect (const char *bind_addr, int bind_port, const char *host, int port)
123
+ {
124
+ Signature = evma_connect_to_server (bind_addr, bind_port, host, port);
125
+ #ifndef HAVE_MAKE_PAIR
126
+ Eventables.insert (std::map<unsigned long,EM::Eventable*>::value_type (Signature, this));
127
+ #else
128
+ Eventables.insert (make_pair (Signature, this));
129
+ #endif
130
+ }
131
+
132
+ /***********************
133
+ EM::Connection::Connect
134
+ ***********************/
135
+
136
+ void EM::Connection::Connect (const char *host, int port)
137
+ {
138
+ this->BindConnect(NULL, 0, host, port);
139
+ }
140
+
141
+ /*******************
142
+ EM::Acceptor::Start
143
+ *******************/
144
+
145
+ void EM::Acceptor::Start (const char *host, int port)
146
+ {
147
+ Signature = evma_create_tcp_server (host, port);
148
+ #ifndef HAVE_MAKE_PAIR
149
+ Eventables.insert (std::map<unsigned long,EM::Eventable*>::value_type (Signature, this));
150
+ #else
151
+ Eventables.insert (make_pair (Signature, this));
152
+ #endif
153
+ }
154
+
155
+
156
+
157
+ /************
158
+ EM::Callback
159
+ ************/
160
+
161
+ void EM::Callback (const unsigned long sig, int ev, const char *data, const unsigned long length)
162
+ {
163
+ EM::Eventable *e;
164
+ void (*f)();
165
+
166
+ switch (ev) {
167
+ case EM_TIMER_FIRED:
168
+ f = Timers [length]; // actually a binding
169
+ if (f)
170
+ (*f)();
171
+ Timers.erase (length);
172
+ break;
173
+
174
+ case EM_CONNECTION_READ:
175
+ e = EM::Eventables [sig];
176
+ e->ReceiveData (data, length);
177
+ break;
178
+
179
+ case EM_CONNECTION_COMPLETED:
180
+ e = EM::Eventables [sig];
181
+ e->ConnectionCompleted();
182
+ break;
183
+
184
+ case EM_CONNECTION_ACCEPTED:
185
+ e = EM::Eventables [sig];
186
+ e->Accept (length); // actually a binding
187
+ break;
188
+
189
+ case EM_CONNECTION_UNBOUND:
190
+ e = EM::Eventables [sig];
191
+ e->Unbind();
192
+ EM::Eventables.erase (sig);
193
+ delete e;
194
+ break;
195
+
196
+ case EM_SSL_HANDSHAKE_COMPLETED:
197
+ e = EM::Eventables [sig];
198
+ e->SslHandshakeCompleted();
199
+ break;
200
+ }
201
+ }
202
+
data/ext/ed.cpp ADDED
@@ -0,0 +1,1884 @@
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
+ InactivityTimeout (0)
66
+ {
67
+ /* There are three ways to close a socket, all of which should
68
+ * automatically signal to the event machine that this object
69
+ * should be removed from the polling scheduler.
70
+ * First is a hard close, intended for bad errors or possible
71
+ * security violations. It immediately closes the connection
72
+ * and puts this object into an error state.
73
+ * Second is to set bCloseNow, which will cause the event machine
74
+ * to delete this object (and thus close the connection in our
75
+ * destructor) the next chance it gets. bCloseNow also inhibits
76
+ * the writing of new data on the socket (but not necessarily
77
+ * the reading of new data).
78
+ * The third way is to set bCloseAfterWriting, which inhibits
79
+ * the writing of new data and converts to bCloseNow as soon
80
+ * as everything in the outbound queue has been written.
81
+ * bCloseAfterWriting is really for use only by protocol handlers
82
+ * (for example, HTTP writes an HTML page and then closes the
83
+ * connection). All of the error states we generate internally
84
+ * cause an immediate close to be scheduled, which may have the
85
+ * effect of discarding outbound data.
86
+ */
87
+
88
+ if (sd == INVALID_SOCKET)
89
+ throw std::runtime_error ("bad eventable descriptor");
90
+ if (MyEventMachine == NULL)
91
+ throw std::runtime_error ("bad em in eventable descriptor");
92
+ CreatedAt = MyEventMachine->GetCurrentLoopTime();
93
+
94
+ #ifdef HAVE_EPOLL
95
+ EpollEvent.events = 0;
96
+ EpollEvent.data.ptr = this;
97
+ #endif
98
+ LastActivity = MyEventMachine->GetCurrentLoopTime();
99
+ }
100
+
101
+
102
+ /*****************************************
103
+ EventableDescriptor::~EventableDescriptor
104
+ *****************************************/
105
+
106
+ EventableDescriptor::~EventableDescriptor()
107
+ {
108
+ if (NextHeartbeat)
109
+ MyEventMachine->ClearHeartbeat(NextHeartbeat);
110
+ if (EventCallback && bCallbackUnbind)
111
+ (*EventCallback)(GetBinding(), EM_CONNECTION_UNBOUND, NULL, UnbindReasonCode);
112
+ if (ProxiedFrom) {
113
+ (*EventCallback)(ProxiedFrom->GetBinding(), EM_PROXY_TARGET_UNBOUND, NULL, 0);
114
+ ProxiedFrom->StopProxy();
115
+ }
116
+ StopProxy();
117
+ Close();
118
+ }
119
+
120
+
121
+ /*************************************
122
+ EventableDescriptor::SetEventCallback
123
+ *************************************/
124
+
125
+ void EventableDescriptor::SetEventCallback (EMCallback cb)
126
+ {
127
+ EventCallback = cb;
128
+ }
129
+
130
+
131
+ /**************************
132
+ EventableDescriptor::Close
133
+ **************************/
134
+
135
+ void EventableDescriptor::Close()
136
+ {
137
+ // Close the socket right now. Intended for emergencies.
138
+ if (MySocket != INVALID_SOCKET) {
139
+ shutdown (MySocket, 1);
140
+ close (MySocket);
141
+ MySocket = INVALID_SOCKET;
142
+ }
143
+ }
144
+
145
+
146
+ /*********************************
147
+ EventableDescriptor::ShouldDelete
148
+ *********************************/
149
+
150
+ bool EventableDescriptor::ShouldDelete()
151
+ {
152
+ /* For use by a socket manager, which needs to know if this object
153
+ * should be removed from scheduling events and deleted.
154
+ * Has an immediate close been scheduled, or are we already closed?
155
+ * If either of these are the case, return true. In theory, the manager will
156
+ * then delete us, which in turn will make sure the socket is closed.
157
+ * Note, if bCloseAfterWriting is true, we check a virtual method to see
158
+ * if there is outbound data to write, and only request a close if there is none.
159
+ */
160
+
161
+ return ((MySocket == INVALID_SOCKET) || bCloseNow || (bCloseAfterWriting && (GetOutboundDataSize() <= 0)));
162
+ }
163
+
164
+
165
+ /**********************************
166
+ EventableDescriptor::ScheduleClose
167
+ **********************************/
168
+
169
+ void EventableDescriptor::ScheduleClose (bool after_writing)
170
+ {
171
+ // KEEP THIS SYNCHRONIZED WITH ::IsCloseScheduled.
172
+ if (after_writing)
173
+ bCloseAfterWriting = true;
174
+ else
175
+ bCloseNow = true;
176
+ }
177
+
178
+
179
+ /*************************************
180
+ EventableDescriptor::IsCloseScheduled
181
+ *************************************/
182
+
183
+ bool EventableDescriptor::IsCloseScheduled()
184
+ {
185
+ // KEEP THIS SYNCHRONIZED WITH ::ScheduleClose.
186
+ return (bCloseNow || bCloseAfterWriting);
187
+ }
188
+
189
+
190
+ /*******************************
191
+ EventableDescriptor::StartProxy
192
+ *******************************/
193
+
194
+ void EventableDescriptor::StartProxy(const unsigned long to, const unsigned long bufsize, const unsigned long length)
195
+ {
196
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (to));
197
+ if (ed) {
198
+ StopProxy();
199
+ ProxyTarget = ed;
200
+ BytesToProxy = length;
201
+ ed->SetProxiedFrom(this, bufsize);
202
+ return;
203
+ }
204
+ throw std::runtime_error ("Tried to proxy to an invalid descriptor");
205
+ }
206
+
207
+
208
+ /******************************
209
+ EventableDescriptor::StopProxy
210
+ ******************************/
211
+
212
+ void EventableDescriptor::StopProxy()
213
+ {
214
+ if (ProxyTarget) {
215
+ ProxyTarget->SetProxiedFrom(NULL, 0);
216
+ ProxyTarget = NULL;
217
+ }
218
+ }
219
+
220
+
221
+ /***********************************
222
+ EventableDescriptor::SetProxiedFrom
223
+ ***********************************/
224
+
225
+ void EventableDescriptor::SetProxiedFrom(EventableDescriptor *from, const unsigned long bufsize)
226
+ {
227
+ if (from != NULL && ProxiedFrom != NULL)
228
+ throw std::runtime_error ("Tried to proxy to a busy target");
229
+
230
+ ProxiedFrom = from;
231
+ MaxOutboundBufSize = bufsize;
232
+ }
233
+
234
+
235
+ /********************************************
236
+ EventableDescriptor::_GenericInboundDispatch
237
+ ********************************************/
238
+
239
+ void EventableDescriptor::_GenericInboundDispatch(const char *buf, int size)
240
+ {
241
+ assert(EventCallback);
242
+
243
+ if (ProxyTarget) {
244
+ if (BytesToProxy > 0) {
245
+ unsigned long proxied = std::min(BytesToProxy, (unsigned long) size);
246
+ ProxyTarget->SendOutboundData(buf, proxied);
247
+ BytesToProxy -= proxied;
248
+ if (BytesToProxy == 0) {
249
+ StopProxy();
250
+ (*EventCallback)(GetBinding(), EM_PROXY_COMPLETED, NULL, 0);
251
+ if (proxied < size) {
252
+ (*EventCallback)(GetBinding(), EM_CONNECTION_READ, buf + proxied, size - proxied);
253
+ }
254
+ }
255
+ } else {
256
+ ProxyTarget->SendOutboundData(buf, size);
257
+ }
258
+ } else {
259
+ (*EventCallback)(GetBinding(), EM_CONNECTION_READ, buf, size);
260
+ }
261
+ }
262
+
263
+
264
+ /*********************************************
265
+ EventableDescriptor::GetPendingConnectTimeout
266
+ *********************************************/
267
+
268
+ uint64_t EventableDescriptor::GetPendingConnectTimeout()
269
+ {
270
+ return PendingConnectTimeout / 1000;
271
+ }
272
+
273
+
274
+ /*********************************************
275
+ EventableDescriptor::SetPendingConnectTimeout
276
+ *********************************************/
277
+
278
+ int EventableDescriptor::SetPendingConnectTimeout (uint64_t value)
279
+ {
280
+ if (value > 0) {
281
+ PendingConnectTimeout = value * 1000;
282
+ MyEventMachine->QueueHeartbeat(this);
283
+ return 1;
284
+ }
285
+ return 0;
286
+ }
287
+
288
+
289
+ /*************************************
290
+ EventableDescriptor::GetNextHeartbeat
291
+ *************************************/
292
+
293
+ uint64_t EventableDescriptor::GetNextHeartbeat()
294
+ {
295
+ if (NextHeartbeat)
296
+ MyEventMachine->ClearHeartbeat(NextHeartbeat);
297
+
298
+ NextHeartbeat = 0;
299
+
300
+ if (!ShouldDelete()) {
301
+ uint64_t time_til_next = GetCommInactivityTimeout() * 1000;
302
+ if (IsConnectPending()) {
303
+ if (time_til_next == 0 || PendingConnectTimeout < time_til_next)
304
+ time_til_next = PendingConnectTimeout;
305
+ }
306
+ if (time_til_next == 0)
307
+ return 0;
308
+ NextHeartbeat = time_til_next + MyEventMachine->GetRealTime();
309
+ }
310
+
311
+ return NextHeartbeat;
312
+ }
313
+
314
+
315
+ /******************************************
316
+ ConnectionDescriptor::ConnectionDescriptor
317
+ ******************************************/
318
+
319
+ ConnectionDescriptor::ConnectionDescriptor (int sd, EventMachine_t *em):
320
+ EventableDescriptor (sd, em),
321
+ bPaused (false),
322
+ bConnectPending (false),
323
+ bNotifyReadable (false),
324
+ bNotifyWritable (false),
325
+ bWatchOnly (false),
326
+ bReadAttemptedAfterClose (false),
327
+ bWriteAttemptedAfterClose (false),
328
+ OutboundDataSize (0),
329
+ #ifdef WITH_SSL
330
+ SslBox (NULL),
331
+ bHandshakeSignaled (false),
332
+ bSslVerifyPeer (false),
333
+ bSslPeerAccepted(false),
334
+ #endif
335
+ #ifdef HAVE_KQUEUE
336
+ bGotExtraKqueueEvent(false),
337
+ #endif
338
+ bIsServer (false)
339
+ {
340
+ // 22Jan09: Moved ArmKqueueWriter into SetConnectPending() to fix assertion failure in _WriteOutboundData()
341
+ // 5May09: Moved EPOLLOUT into SetConnectPending() so it doesn't happen for attached read pipes
342
+ }
343
+
344
+
345
+ /*******************************************
346
+ ConnectionDescriptor::~ConnectionDescriptor
347
+ *******************************************/
348
+
349
+ ConnectionDescriptor::~ConnectionDescriptor()
350
+ {
351
+ // Run down any stranded outbound data.
352
+ for (size_t i=0; i < OutboundPages.size(); i++)
353
+ OutboundPages[i].Free();
354
+
355
+ #ifdef WITH_SSL
356
+ if (SslBox)
357
+ delete SslBox;
358
+ #endif
359
+ }
360
+
361
+
362
+ /***********************************
363
+ ConnectionDescriptor::_UpdateEvents
364
+ ************************************/
365
+
366
+ void ConnectionDescriptor::_UpdateEvents()
367
+ {
368
+ _UpdateEvents(true, true);
369
+ }
370
+
371
+ void ConnectionDescriptor::_UpdateEvents(bool read, bool write)
372
+ {
373
+ if (MySocket == INVALID_SOCKET)
374
+ return;
375
+
376
+ #ifdef HAVE_EPOLL
377
+ unsigned int old = EpollEvent.events;
378
+
379
+ if (read) {
380
+ if (SelectForRead())
381
+ EpollEvent.events |= EPOLLIN;
382
+ else
383
+ EpollEvent.events &= ~EPOLLIN;
384
+ }
385
+
386
+ if (write) {
387
+ if (SelectForWrite())
388
+ EpollEvent.events |= EPOLLOUT;
389
+ else
390
+ EpollEvent.events &= ~EPOLLOUT;
391
+ }
392
+
393
+ if (old != EpollEvent.events)
394
+ MyEventMachine->Modify (this);
395
+ #endif
396
+
397
+ #ifdef HAVE_KQUEUE
398
+ if (read && SelectForRead())
399
+ MyEventMachine->ArmKqueueReader (this);
400
+ if (write && SelectForWrite())
401
+ MyEventMachine->ArmKqueueWriter (this);
402
+ #endif
403
+ }
404
+
405
+ /***************************************
406
+ ConnectionDescriptor::SetConnectPending
407
+ ****************************************/
408
+
409
+ void ConnectionDescriptor::SetConnectPending(bool f)
410
+ {
411
+ bConnectPending = f;
412
+ _UpdateEvents();
413
+ }
414
+
415
+
416
+ /**********************************
417
+ ConnectionDescriptor::SetWatchOnly
418
+ ***********************************/
419
+
420
+ void ConnectionDescriptor::SetWatchOnly(bool watching)
421
+ {
422
+ bWatchOnly = watching;
423
+ _UpdateEvents();
424
+ }
425
+
426
+
427
+ /*********************************
428
+ ConnectionDescriptor::HandleError
429
+ *********************************/
430
+
431
+ void ConnectionDescriptor::HandleError()
432
+ {
433
+ if (bWatchOnly) {
434
+ // An EPOLLHUP | EPOLLIN condition will call Read() before HandleError(), in which case the
435
+ // socket is already detached and invalid, so we don't need to do anything.
436
+ if (MySocket == INVALID_SOCKET) return;
437
+
438
+ // HandleError() is called on WatchOnly descriptors by the epoll reactor
439
+ // when it gets a EPOLLERR | EPOLLHUP. Usually this would show up as a readable and
440
+ // writable event on other reactors, so we have to fire those events ourselves.
441
+ if (bNotifyReadable) Read();
442
+ if (bNotifyWritable) Write();
443
+ } else {
444
+ ScheduleClose (false);
445
+ }
446
+ }
447
+
448
+
449
+ /***********************************
450
+ ConnectionDescriptor::ScheduleClose
451
+ ***********************************/
452
+
453
+ void ConnectionDescriptor::ScheduleClose (bool after_writing)
454
+ {
455
+ if (bWatchOnly)
456
+ throw std::runtime_error ("cannot close 'watch only' connections");
457
+
458
+ EventableDescriptor::ScheduleClose(after_writing);
459
+ }
460
+
461
+
462
+ /***************************************
463
+ ConnectionDescriptor::SetNotifyReadable
464
+ ****************************************/
465
+
466
+ void ConnectionDescriptor::SetNotifyReadable(bool readable)
467
+ {
468
+ if (!bWatchOnly)
469
+ throw std::runtime_error ("notify_readable must be on 'watch only' connections");
470
+
471
+ bNotifyReadable = readable;
472
+ _UpdateEvents(true, false);
473
+ }
474
+
475
+
476
+ /***************************************
477
+ ConnectionDescriptor::SetNotifyWritable
478
+ ****************************************/
479
+
480
+ void ConnectionDescriptor::SetNotifyWritable(bool writable)
481
+ {
482
+ if (!bWatchOnly)
483
+ throw std::runtime_error ("notify_writable must be on 'watch only' connections");
484
+
485
+ bNotifyWritable = writable;
486
+ _UpdateEvents(false, true);
487
+ }
488
+
489
+
490
+ /**************************************
491
+ ConnectionDescriptor::SendOutboundData
492
+ **************************************/
493
+
494
+ int ConnectionDescriptor::SendOutboundData (const char *data, int length)
495
+ {
496
+ if (bWatchOnly)
497
+ throw std::runtime_error ("cannot send data on a 'watch only' connection");
498
+
499
+ if (ProxiedFrom && MaxOutboundBufSize && (unsigned int)(GetOutboundDataSize() + length) > MaxOutboundBufSize)
500
+ ProxiedFrom->Pause();
501
+
502
+ #ifdef WITH_SSL
503
+ if (SslBox) {
504
+ if (length > 0) {
505
+ int w = SslBox->PutPlaintext (data, length);
506
+ if (w < 0)
507
+ ScheduleClose (false);
508
+ else
509
+ _DispatchCiphertext();
510
+ }
511
+ // TODO: What's the correct return value?
512
+ return 1; // That's a wild guess, almost certainly wrong.
513
+ }
514
+ else
515
+ #endif
516
+ return _SendRawOutboundData (data, length);
517
+ }
518
+
519
+
520
+
521
+ /******************************************
522
+ ConnectionDescriptor::_SendRawOutboundData
523
+ ******************************************/
524
+
525
+ int ConnectionDescriptor::_SendRawOutboundData (const char *data, int length)
526
+ {
527
+ /* This internal method is called to schedule bytes that
528
+ * will be sent out to the remote peer.
529
+ * It's not directly accessed by the caller, who hits ::SendOutboundData,
530
+ * which may or may not filter or encrypt the caller's data before
531
+ * sending it here.
532
+ */
533
+
534
+ // Highly naive and incomplete implementation.
535
+ // There's no throttle for runaways (which should abort only this connection
536
+ // and not the whole process), and no coalescing of small pages.
537
+ // (Well, not so bad, small pages are coalesced in ::Write)
538
+
539
+ if (IsCloseScheduled())
540
+ return 0;
541
+
542
+ // 25Mar10: Ignore 0 length packets as they are not meaningful in TCP (as opposed to UDP)
543
+ // and can cause the assert(nbytes>0) to fail when OutboundPages has a bunch of 0 length pages.
544
+ if (length == 0)
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
+ LastActivity = MyEventMachine->GetCurrentLoopTime();
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
+ LastActivity = MyEventMachine->GetCurrentLoopTime();
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 && (unsigned int)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 ((MyEventMachine->GetCurrentLoopTime() - CreatedAt) >= PendingConnectTimeout)
1202
+ ScheduleClose (false);
1203
+ //bCloseNow = true;
1204
+ }
1205
+ else {
1206
+ if (InactivityTimeout && ((MyEventMachine->GetCurrentLoopTime() - LastActivity) >= 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
+ close (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
+ {
1418
+ memset (&ReturnAddress, 0, sizeof(ReturnAddress));
1419
+
1420
+ /* Provisionally added 19Oct07. All datagram sockets support broadcasting.
1421
+ * Until now, sending to a broadcast address would give EACCES (permission denied)
1422
+ * on systems like Linux and BSD that require the SO_BROADCAST socket-option in order
1423
+ * to accept a packet to a broadcast address. Solaris doesn't require it. I think
1424
+ * Windows DOES require it but I'm not sure.
1425
+ *
1426
+ * Ruby does NOT do what we're doing here. In Ruby, you have to explicitly set SO_BROADCAST
1427
+ * on a UDP socket in order to enable broadcasting. The reason for requiring the option
1428
+ * in the first place is so that applications don't send broadcast datagrams by mistake.
1429
+ * I imagine that could happen if a user of an application typed in an address that happened
1430
+ * to be a broadcast address on that particular subnet.
1431
+ *
1432
+ * This is provisional because someone may eventually come up with a good reason not to
1433
+ * do it for all UDP sockets. If that happens, then we'll need to add a usercode-level API
1434
+ * to set the socket option, just like Ruby does. AND WE'LL ALSO BREAK CODE THAT DOESN'T
1435
+ * EXPLICITLY SET THE OPTION.
1436
+ */
1437
+
1438
+ int oval = 1;
1439
+ setsockopt (GetSocket(), SOL_SOCKET, SO_BROADCAST, (char*)&oval, sizeof(oval));
1440
+
1441
+ #ifdef HAVE_EPOLL
1442
+ EpollEvent.events = EPOLLIN;
1443
+ #endif
1444
+ #ifdef HAVE_KQUEUE
1445
+ MyEventMachine->ArmKqueueReader (this);
1446
+ #endif
1447
+ }
1448
+
1449
+
1450
+ /***************************************
1451
+ DatagramDescriptor::~DatagramDescriptor
1452
+ ***************************************/
1453
+
1454
+ DatagramDescriptor::~DatagramDescriptor()
1455
+ {
1456
+ // Run down any stranded outbound data.
1457
+ for (size_t i=0; i < OutboundPages.size(); i++)
1458
+ OutboundPages[i].Free();
1459
+ }
1460
+
1461
+
1462
+ /*****************************
1463
+ DatagramDescriptor::Heartbeat
1464
+ *****************************/
1465
+
1466
+ void DatagramDescriptor::Heartbeat()
1467
+ {
1468
+ // Close it if its inactivity timer has expired.
1469
+
1470
+ if (InactivityTimeout && ((MyEventMachine->GetCurrentLoopTime() - LastActivity) >= InactivityTimeout))
1471
+ ScheduleClose (false);
1472
+ //bCloseNow = true;
1473
+ }
1474
+
1475
+
1476
+ /************************
1477
+ DatagramDescriptor::Read
1478
+ ************************/
1479
+
1480
+ void DatagramDescriptor::Read()
1481
+ {
1482
+ int sd = GetSocket();
1483
+ assert (sd != INVALID_SOCKET);
1484
+ LastActivity = MyEventMachine->GetCurrentLoopTime();
1485
+
1486
+ // This is an extremely large read buffer.
1487
+ // In many cases you wouldn't expect to get any more than 4K.
1488
+ char readbuffer [16 * 1024];
1489
+
1490
+ for (int i=0; i < 10; i++) {
1491
+ // Don't read just one buffer and then move on. This is faster
1492
+ // if there is a lot of incoming.
1493
+ // But don't read indefinitely. Give other sockets a chance to run.
1494
+ // NOTICE, we're reading one less than the buffer size.
1495
+ // That's so we can put a guard byte at the end of what we send
1496
+ // to user code.
1497
+
1498
+ struct sockaddr_in sin;
1499
+ socklen_t slen = sizeof (sin);
1500
+ memset (&sin, 0, slen);
1501
+
1502
+ int r = recvfrom (sd, readbuffer, sizeof(readbuffer) - 1, 0, (struct sockaddr*)&sin, &slen);
1503
+ //cerr << "<R:" << r << ">";
1504
+
1505
+ // In UDP, a zero-length packet is perfectly legal.
1506
+ if (r >= 0) {
1507
+
1508
+ // Add a null-terminator at the the end of the buffer
1509
+ // that we will send to the callback.
1510
+ // DO NOT EVER CHANGE THIS. We want to explicitly allow users
1511
+ // to be able to depend on this behavior, so they will have
1512
+ // the option to do some things faster. Additionally it's
1513
+ // a security guard against buffer overflows.
1514
+ readbuffer [r] = 0;
1515
+
1516
+
1517
+ // Set up a "temporary" return address so that callers can "reply" to us
1518
+ // from within the callback we are about to invoke. That means that ordinary
1519
+ // calls to "send_data_to_connection" (which is of course misnamed in this
1520
+ // case) will result in packets being sent back to the same place that sent
1521
+ // us this one.
1522
+ // There is a different call (evma_send_datagram) for cases where the caller
1523
+ // actually wants to send a packet somewhere else.
1524
+
1525
+ memset (&ReturnAddress, 0, sizeof(ReturnAddress));
1526
+ memcpy (&ReturnAddress, &sin, slen);
1527
+
1528
+ _GenericInboundDispatch(readbuffer, r);
1529
+
1530
+ }
1531
+ else {
1532
+ // Basically a would-block, meaning we've read everything there is to read.
1533
+ break;
1534
+ }
1535
+
1536
+ }
1537
+
1538
+
1539
+ }
1540
+
1541
+
1542
+ /*************************
1543
+ DatagramDescriptor::Write
1544
+ *************************/
1545
+
1546
+ void DatagramDescriptor::Write()
1547
+ {
1548
+ /* It's possible for a socket to select writable and then no longer
1549
+ * be writable by the time we get around to writing. The kernel might
1550
+ * have used up its available output buffers between the select call
1551
+ * and when we get here. So this condition is not an error.
1552
+ * This code is very reminiscent of ConnectionDescriptor::_WriteOutboundData,
1553
+ * but differs in the that the outbound data pages (received from the
1554
+ * user) are _message-structured._ That is, we send each of them out
1555
+ * one message at a time.
1556
+ * TODO, we are currently suppressing the EMSGSIZE error!!!
1557
+ */
1558
+
1559
+ int sd = GetSocket();
1560
+ assert (sd != INVALID_SOCKET);
1561
+ LastActivity = MyEventMachine->GetCurrentLoopTime();
1562
+
1563
+ assert (OutboundPages.size() > 0);
1564
+
1565
+ // Send out up to 10 packets, then cycle the machine.
1566
+ for (int i = 0; i < 10; i++) {
1567
+ if (OutboundPages.size() <= 0)
1568
+ break;
1569
+ OutboundPage *op = &(OutboundPages[0]);
1570
+
1571
+ // The nasty cast to (char*) is needed because Windows is brain-dead.
1572
+ int s = sendto (sd, (char*)op->Buffer, op->Length, 0, (struct sockaddr*)&(op->From), sizeof(op->From));
1573
+ int e = errno;
1574
+
1575
+ OutboundDataSize -= op->Length;
1576
+ op->Free();
1577
+ OutboundPages.pop_front();
1578
+
1579
+ if (s == SOCKET_ERROR) {
1580
+ #ifdef OS_UNIX
1581
+ if ((e != EINPROGRESS) && (e != EWOULDBLOCK) && (e != EINTR)) {
1582
+ #endif
1583
+ #ifdef OS_WIN32
1584
+ if ((e != WSAEINPROGRESS) && (e != WSAEWOULDBLOCK)) {
1585
+ #endif
1586
+ Close();
1587
+ break;
1588
+ }
1589
+ }
1590
+ }
1591
+
1592
+ #ifdef HAVE_EPOLL
1593
+ EpollEvent.events = (EPOLLIN | (SelectForWrite() ? EPOLLOUT : 0));
1594
+ assert (MyEventMachine);
1595
+ MyEventMachine->Modify (this);
1596
+ #endif
1597
+ #ifdef HAVE_KQUEUE
1598
+ if (SelectForWrite())
1599
+ MyEventMachine->ArmKqueueWriter (this);
1600
+ #endif
1601
+ }
1602
+
1603
+
1604
+ /**********************************
1605
+ DatagramDescriptor::SelectForWrite
1606
+ **********************************/
1607
+
1608
+ bool DatagramDescriptor::SelectForWrite()
1609
+ {
1610
+ /* Changed 15Nov07, per bug report by Mark Zvillius.
1611
+ * The outbound data size will be zero if there are zero-length outbound packets,
1612
+ * so we now select writable in case the outbound page buffer is not empty.
1613
+ * Note that the superclass ShouldDelete method still checks for outbound data size,
1614
+ * which may be wrong.
1615
+ */
1616
+ //return (GetOutboundDataSize() > 0); (Original)
1617
+ return (OutboundPages.size() > 0);
1618
+ }
1619
+
1620
+
1621
+ /************************************
1622
+ DatagramDescriptor::SendOutboundData
1623
+ ************************************/
1624
+
1625
+ int DatagramDescriptor::SendOutboundData (const char *data, int length)
1626
+ {
1627
+ // This is almost an exact clone of ConnectionDescriptor::_SendRawOutboundData.
1628
+ // That means most of it could be factored to a common ancestor. Note that
1629
+ // empty datagrams are meaningful, which isn't the case for TCP streams.
1630
+
1631
+ if (IsCloseScheduled())
1632
+ return 0;
1633
+
1634
+ if (!data && (length > 0))
1635
+ throw std::runtime_error ("bad outbound data");
1636
+ char *buffer = (char *) malloc (length + 1);
1637
+ if (!buffer)
1638
+ throw std::runtime_error ("no allocation for outbound data");
1639
+ memcpy (buffer, data, length);
1640
+ buffer [length] = 0;
1641
+ OutboundPages.push_back (OutboundPage (buffer, length, ReturnAddress));
1642
+ OutboundDataSize += length;
1643
+
1644
+ #ifdef HAVE_EPOLL
1645
+ EpollEvent.events = (EPOLLIN | EPOLLOUT);
1646
+ assert (MyEventMachine);
1647
+ MyEventMachine->Modify (this);
1648
+ #endif
1649
+ #ifdef HAVE_KQUEUE
1650
+ MyEventMachine->ArmKqueueWriter (this);
1651
+ #endif
1652
+
1653
+ return length;
1654
+ }
1655
+
1656
+
1657
+ /****************************************
1658
+ DatagramDescriptor::SendOutboundDatagram
1659
+ ****************************************/
1660
+
1661
+ int DatagramDescriptor::SendOutboundDatagram (const char *data, int length, const char *address, int port)
1662
+ {
1663
+ // This is an exact clone of ConnectionDescriptor::SendOutboundData.
1664
+ // That means it needs to move to a common ancestor.
1665
+ // TODO: Refactor this so there's no overlap with SendOutboundData.
1666
+
1667
+ if (IsCloseScheduled())
1668
+ //if (bCloseNow || bCloseAfterWriting)
1669
+ return 0;
1670
+
1671
+ if (!address || !*address || !port)
1672
+ return 0;
1673
+
1674
+ sockaddr_in pin;
1675
+ unsigned long HostAddr;
1676
+
1677
+ HostAddr = inet_addr (address);
1678
+ if (HostAddr == INADDR_NONE) {
1679
+ // The nasty cast to (char*) is because Windows is brain-dead.
1680
+ hostent *hp = gethostbyname ((char*)address);
1681
+ if (!hp)
1682
+ return 0;
1683
+ HostAddr = ((in_addr*)(hp->h_addr))->s_addr;
1684
+ }
1685
+
1686
+ memset (&pin, 0, sizeof(pin));
1687
+ pin.sin_family = AF_INET;
1688
+ pin.sin_addr.s_addr = HostAddr;
1689
+ pin.sin_port = htons (port);
1690
+
1691
+
1692
+ if (!data && (length > 0))
1693
+ throw std::runtime_error ("bad outbound data");
1694
+ char *buffer = (char *) malloc (length + 1);
1695
+ if (!buffer)
1696
+ throw std::runtime_error ("no allocation for outbound data");
1697
+ memcpy (buffer, data, length);
1698
+ buffer [length] = 0;
1699
+ OutboundPages.push_back (OutboundPage (buffer, length, pin));
1700
+ OutboundDataSize += length;
1701
+
1702
+ #ifdef HAVE_EPOLL
1703
+ EpollEvent.events = (EPOLLIN | EPOLLOUT);
1704
+ assert (MyEventMachine);
1705
+ MyEventMachine->Modify (this);
1706
+ #endif
1707
+ #ifdef HAVE_KQUEUE
1708
+ MyEventMachine->ArmKqueueWriter (this);
1709
+ #endif
1710
+
1711
+ return length;
1712
+ }
1713
+
1714
+
1715
+ /*********************************
1716
+ ConnectionDescriptor::GetPeername
1717
+ *********************************/
1718
+
1719
+ bool ConnectionDescriptor::GetPeername (struct sockaddr *s)
1720
+ {
1721
+ bool ok = false;
1722
+ if (s) {
1723
+ socklen_t len = sizeof(*s);
1724
+ int gp = getpeername (GetSocket(), s, &len);
1725
+ if (gp == 0)
1726
+ ok = true;
1727
+ }
1728
+ return ok;
1729
+ }
1730
+
1731
+ /*********************************
1732
+ ConnectionDescriptor::GetSockname
1733
+ *********************************/
1734
+
1735
+ bool ConnectionDescriptor::GetSockname (struct sockaddr *s)
1736
+ {
1737
+ bool ok = false;
1738
+ if (s) {
1739
+ socklen_t len = sizeof(*s);
1740
+ int gp = getsockname (GetSocket(), s, &len);
1741
+ if (gp == 0)
1742
+ ok = true;
1743
+ }
1744
+ return ok;
1745
+ }
1746
+
1747
+
1748
+ /**********************************************
1749
+ ConnectionDescriptor::GetCommInactivityTimeout
1750
+ **********************************************/
1751
+
1752
+ uint64_t ConnectionDescriptor::GetCommInactivityTimeout()
1753
+ {
1754
+ return InactivityTimeout / 1000;
1755
+ }
1756
+
1757
+
1758
+ /**********************************************
1759
+ ConnectionDescriptor::SetCommInactivityTimeout
1760
+ **********************************************/
1761
+
1762
+ int ConnectionDescriptor::SetCommInactivityTimeout (uint64_t value)
1763
+ {
1764
+ InactivityTimeout = value * 1000;
1765
+ MyEventMachine->QueueHeartbeat(this);
1766
+ return 1;
1767
+ }
1768
+
1769
+ /*******************************
1770
+ DatagramDescriptor::GetPeername
1771
+ *******************************/
1772
+
1773
+ bool DatagramDescriptor::GetPeername (struct sockaddr *s)
1774
+ {
1775
+ bool ok = false;
1776
+ if (s) {
1777
+ memset (s, 0, sizeof(struct sockaddr));
1778
+ memcpy (s, &ReturnAddress, sizeof(ReturnAddress));
1779
+ ok = true;
1780
+ }
1781
+ return ok;
1782
+ }
1783
+
1784
+ /*******************************
1785
+ DatagramDescriptor::GetSockname
1786
+ *******************************/
1787
+
1788
+ bool DatagramDescriptor::GetSockname (struct sockaddr *s)
1789
+ {
1790
+ bool ok = false;
1791
+ if (s) {
1792
+ socklen_t len = sizeof(*s);
1793
+ int gp = getsockname (GetSocket(), s, &len);
1794
+ if (gp == 0)
1795
+ ok = true;
1796
+ }
1797
+ return ok;
1798
+ }
1799
+
1800
+
1801
+
1802
+ /********************************************
1803
+ DatagramDescriptor::GetCommInactivityTimeout
1804
+ ********************************************/
1805
+
1806
+ uint64_t DatagramDescriptor::GetCommInactivityTimeout()
1807
+ {
1808
+ return InactivityTimeout / 1000;
1809
+ }
1810
+
1811
+ /********************************************
1812
+ DatagramDescriptor::SetCommInactivityTimeout
1813
+ ********************************************/
1814
+
1815
+ int DatagramDescriptor::SetCommInactivityTimeout (uint64_t value)
1816
+ {
1817
+ if (value > 0) {
1818
+ InactivityTimeout = value * 1000;
1819
+ MyEventMachine->QueueHeartbeat(this);
1820
+ return 1;
1821
+ }
1822
+ return 0;
1823
+ }
1824
+
1825
+
1826
+ /************************************
1827
+ InotifyDescriptor::InotifyDescriptor
1828
+ *************************************/
1829
+
1830
+ InotifyDescriptor::InotifyDescriptor (EventMachine_t *em):
1831
+ EventableDescriptor(0, em)
1832
+ {
1833
+ bCallbackUnbind = false;
1834
+
1835
+ #ifndef HAVE_INOTIFY
1836
+ throw std::runtime_error("no inotify support on this system");
1837
+ #else
1838
+
1839
+ int fd = inotify_init();
1840
+ if (fd == -1) {
1841
+ char buf[200];
1842
+ snprintf (buf, sizeof(buf)-1, "unable to create inotify descriptor: %s", strerror(errno));
1843
+ throw std::runtime_error (buf);
1844
+ }
1845
+
1846
+ MySocket = fd;
1847
+ SetSocketNonblocking(MySocket);
1848
+ #ifdef HAVE_EPOLL
1849
+ EpollEvent.events = EPOLLIN;
1850
+ #endif
1851
+
1852
+ #endif
1853
+ }
1854
+
1855
+
1856
+ /*************************************
1857
+ InotifyDescriptor::~InotifyDescriptor
1858
+ **************************************/
1859
+
1860
+ InotifyDescriptor::~InotifyDescriptor()
1861
+ {
1862
+ close(MySocket);
1863
+ MySocket = INVALID_SOCKET;
1864
+ }
1865
+
1866
+ /***********************
1867
+ InotifyDescriptor::Read
1868
+ ************************/
1869
+
1870
+ void InotifyDescriptor::Read()
1871
+ {
1872
+ assert (MyEventMachine);
1873
+ MyEventMachine->_ReadInotifyEvents();
1874
+ }
1875
+
1876
+
1877
+ /************************
1878
+ InotifyDescriptor::Write
1879
+ *************************/
1880
+
1881
+ void InotifyDescriptor::Write()
1882
+ {
1883
+ throw std::runtime_error("bad code path in inotify");
1884
+ }