eventmachine 0.12.6-x86-mswin32-60

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (136) hide show
  1. data/.gitignore +13 -0
  2. data/Rakefile +254 -0
  3. data/docs/COPYING +60 -0
  4. data/docs/ChangeLog +211 -0
  5. data/docs/DEFERRABLES +138 -0
  6. data/docs/EPOLL +141 -0
  7. data/docs/GNU +281 -0
  8. data/docs/INSTALL +15 -0
  9. data/docs/KEYBOARD +38 -0
  10. data/docs/LEGAL +25 -0
  11. data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
  12. data/docs/PURE_RUBY +77 -0
  13. data/docs/README +74 -0
  14. data/docs/RELEASE_NOTES +96 -0
  15. data/docs/SMTP +9 -0
  16. data/docs/SPAWNED_PROCESSES +93 -0
  17. data/docs/TODO +10 -0
  18. data/eventmachine.gemspec +32 -0
  19. data/ext/binder.cpp +126 -0
  20. data/ext/binder.h +48 -0
  21. data/ext/cmain.cpp +586 -0
  22. data/ext/cplusplus.cpp +193 -0
  23. data/ext/ed.cpp +1522 -0
  24. data/ext/ed.h +380 -0
  25. data/ext/em.cpp +1937 -0
  26. data/ext/em.h +186 -0
  27. data/ext/emwin.cpp +300 -0
  28. data/ext/emwin.h +94 -0
  29. data/ext/epoll.cpp +26 -0
  30. data/ext/epoll.h +25 -0
  31. data/ext/eventmachine.h +98 -0
  32. data/ext/eventmachine_cpp.h +95 -0
  33. data/ext/extconf.rb +129 -0
  34. data/ext/fastfilereader/extconf.rb +77 -0
  35. data/ext/fastfilereader/mapper.cpp +214 -0
  36. data/ext/fastfilereader/mapper.h +59 -0
  37. data/ext/fastfilereader/rubymain.cpp +127 -0
  38. data/ext/files.cpp +94 -0
  39. data/ext/files.h +65 -0
  40. data/ext/kb.cpp +82 -0
  41. data/ext/page.cpp +107 -0
  42. data/ext/page.h +51 -0
  43. data/ext/pipe.cpp +351 -0
  44. data/ext/project.h +119 -0
  45. data/ext/rubymain.cpp +847 -0
  46. data/ext/sigs.cpp +89 -0
  47. data/ext/sigs.h +32 -0
  48. data/ext/ssl.cpp +423 -0
  49. data/ext/ssl.h +90 -0
  50. data/java/.classpath +8 -0
  51. data/java/.project +17 -0
  52. data/java/src/com/rubyeventmachine/Application.java +196 -0
  53. data/java/src/com/rubyeventmachine/Connection.java +74 -0
  54. data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
  55. data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
  61. data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
  62. data/java/src/com/rubyeventmachine/Timer.java +54 -0
  63. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
  64. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
  65. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  66. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  67. data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
  68. data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
  69. data/lib/em/deferrable.rb +208 -0
  70. data/lib/em/eventable.rb +39 -0
  71. data/lib/em/future.rb +62 -0
  72. data/lib/em/messages.rb +66 -0
  73. data/lib/em/processes.rb +113 -0
  74. data/lib/em/spawnable.rb +88 -0
  75. data/lib/em/streamer.rb +112 -0
  76. data/lib/eventmachine.rb +1926 -0
  77. data/lib/eventmachine_version.rb +31 -0
  78. data/lib/evma.rb +32 -0
  79. data/lib/evma/callback.rb +32 -0
  80. data/lib/evma/container.rb +75 -0
  81. data/lib/evma/factory.rb +77 -0
  82. data/lib/evma/protocol.rb +87 -0
  83. data/lib/evma/reactor.rb +48 -0
  84. data/lib/jeventmachine.rb +137 -0
  85. data/lib/pr_eventmachine.rb +1011 -0
  86. data/lib/protocols/buftok.rb +127 -0
  87. data/lib/protocols/header_and_content.rb +129 -0
  88. data/lib/protocols/httpcli2.rb +803 -0
  89. data/lib/protocols/httpclient.rb +270 -0
  90. data/lib/protocols/line_and_text.rb +126 -0
  91. data/lib/protocols/linetext2.rb +161 -0
  92. data/lib/protocols/memcache.rb +293 -0
  93. data/lib/protocols/postgres.rb +261 -0
  94. data/lib/protocols/saslauth.rb +179 -0
  95. data/lib/protocols/smtpclient.rb +308 -0
  96. data/lib/protocols/smtpserver.rb +556 -0
  97. data/lib/protocols/stomp.rb +153 -0
  98. data/lib/protocols/tcptest.rb +57 -0
  99. data/setup.rb +1585 -0
  100. data/tasks/cpp.rake +77 -0
  101. data/tasks/project.rake +78 -0
  102. data/tasks/tests.rake +193 -0
  103. data/tests/test_attach.rb +83 -0
  104. data/tests/test_basic.rb +231 -0
  105. data/tests/test_connection_count.rb +45 -0
  106. data/tests/test_defer.rb +47 -0
  107. data/tests/test_epoll.rb +163 -0
  108. data/tests/test_error_handler.rb +35 -0
  109. data/tests/test_errors.rb +82 -0
  110. data/tests/test_eventables.rb +77 -0
  111. data/tests/test_exc.rb +58 -0
  112. data/tests/test_futures.rb +214 -0
  113. data/tests/test_handler_check.rb +37 -0
  114. data/tests/test_hc.rb +218 -0
  115. data/tests/test_httpclient.rb +215 -0
  116. data/tests/test_httpclient2.rb +155 -0
  117. data/tests/test_kb.rb +61 -0
  118. data/tests/test_ltp.rb +188 -0
  119. data/tests/test_ltp2.rb +320 -0
  120. data/tests/test_next_tick.rb +109 -0
  121. data/tests/test_processes.rb +95 -0
  122. data/tests/test_pure.rb +129 -0
  123. data/tests/test_running.rb +47 -0
  124. data/tests/test_sasl.rb +74 -0
  125. data/tests/test_send_file.rb +243 -0
  126. data/tests/test_servers.rb +80 -0
  127. data/tests/test_smtpclient.rb +83 -0
  128. data/tests/test_smtpserver.rb +93 -0
  129. data/tests/test_spawn.rb +329 -0
  130. data/tests/test_ssl_args.rb +68 -0
  131. data/tests/test_ssl_methods.rb +50 -0
  132. data/tests/test_timers.rb +148 -0
  133. data/tests/test_ud.rb +43 -0
  134. data/tests/testem.rb +31 -0
  135. data/web/whatis +7 -0
  136. metadata +207 -0
@@ -0,0 +1,94 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: files.cpp
6
+ Date: 26Aug06
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
+ FileStreamDescriptor::FileStreamDescriptor
25
+ ******************************************/
26
+
27
+ FileStreamDescriptor::FileStreamDescriptor (int fd, EventMachine_t *em):
28
+ EventableDescriptor (fd, em),
29
+ OutboundDataSize (0)
30
+ {
31
+ cerr << "#####";
32
+ }
33
+
34
+
35
+ /*******************************************
36
+ FileStreamDescriptor::~FileStreamDescriptor
37
+ *******************************************/
38
+
39
+ FileStreamDescriptor::~FileStreamDescriptor()
40
+ {
41
+ // Run down any stranded outbound data.
42
+ for (size_t i=0; i < OutboundPages.size(); i++)
43
+ OutboundPages[i].Free();
44
+ }
45
+
46
+
47
+ /**************************
48
+ FileStreamDescriptor::Read
49
+ **************************/
50
+
51
+ void FileStreamDescriptor::Read()
52
+ {
53
+ }
54
+
55
+ /***************************
56
+ FileStreamDescriptor::Write
57
+ ***************************/
58
+
59
+ void FileStreamDescriptor::Write()
60
+ {
61
+ }
62
+
63
+
64
+ /*******************************
65
+ FileStreamDescriptor::Heartbeat
66
+ *******************************/
67
+
68
+ void FileStreamDescriptor::Heartbeat()
69
+ {
70
+ }
71
+
72
+
73
+ /***********************************
74
+ FileStreamDescriptor::SelectForRead
75
+ ***********************************/
76
+
77
+ bool FileStreamDescriptor::SelectForRead()
78
+ {
79
+ cerr << "R?";
80
+ return false;
81
+ }
82
+
83
+
84
+ /************************************
85
+ FileStreamDescriptor::SelectForWrite
86
+ ************************************/
87
+
88
+ bool FileStreamDescriptor::SelectForWrite()
89
+ {
90
+ cerr << "W?";
91
+ return false;
92
+ }
93
+
94
+
@@ -0,0 +1,65 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: files.h
6
+ Date: 26Aug06
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
+ #ifndef __FileStreamDescriptor__H_
22
+ #define __FileStreamDescriptor__H_
23
+
24
+
25
+
26
+ /**************************
27
+ class FileStreamDescriptor
28
+ **************************/
29
+
30
+ class FileStreamDescriptor: public EventableDescriptor
31
+ {
32
+ public:
33
+ FileStreamDescriptor (int, EventMachine_t*);
34
+ virtual ~FileStreamDescriptor();
35
+
36
+ virtual void Read();
37
+ virtual void Write();
38
+ virtual void Heartbeat();
39
+
40
+ virtual bool SelectForRead();
41
+ virtual bool SelectForWrite();
42
+
43
+ // Do we have any data to write? This is used by ShouldDelete.
44
+ virtual int GetOutboundDataSize() {return OutboundDataSize;}
45
+
46
+ protected:
47
+ struct OutboundPage {
48
+ OutboundPage (const char *b, int l, int o=0): Buffer(b), Length(l), Offset(o) {}
49
+ void Free() {if (Buffer) free ((char*)Buffer); }
50
+ const char *Buffer;
51
+ int Length;
52
+ int Offset;
53
+ };
54
+
55
+ protected:
56
+ deque<OutboundPage> OutboundPages;
57
+ int OutboundDataSize;
58
+
59
+ private:
60
+
61
+ };
62
+
63
+
64
+ #endif // __FileStreamDescriptor__H_
65
+
@@ -0,0 +1,82 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: kb.cpp
6
+ Date: 24Aug07
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
+ KeyboardDescriptor::KeyboardDescriptor
25
+ **************************************/
26
+
27
+ KeyboardDescriptor::KeyboardDescriptor (EventMachine_t *parent_em):
28
+ EventableDescriptor (0, parent_em),
29
+ bReadAttemptedAfterClose (false),
30
+ LastIo (gCurrentLoopTime),
31
+ InactivityTimeout (0)
32
+ {
33
+ #ifdef HAVE_EPOLL
34
+ EpollEvent.events = EPOLLIN;
35
+ #endif
36
+ #ifdef HAVE_KQUEUE
37
+ MyEventMachine->ArmKqueueReader (this);
38
+ #endif
39
+ }
40
+
41
+
42
+ /***************************************
43
+ KeyboardDescriptor::~KeyboardDescriptor
44
+ ***************************************/
45
+
46
+ KeyboardDescriptor::~KeyboardDescriptor()
47
+ {
48
+ }
49
+
50
+
51
+ /*************************
52
+ KeyboardDescriptor::Write
53
+ *************************/
54
+
55
+ void KeyboardDescriptor::Write()
56
+ {
57
+ // Why are we here?
58
+ throw std::runtime_error ("bad code path in keyboard handler");
59
+ }
60
+
61
+
62
+ /*****************************
63
+ KeyboardDescriptor::Heartbeat
64
+ *****************************/
65
+
66
+ void KeyboardDescriptor::Heartbeat()
67
+ {
68
+ // no-op
69
+ }
70
+
71
+
72
+ /************************
73
+ KeyboardDescriptor::Read
74
+ ************************/
75
+
76
+ void KeyboardDescriptor::Read()
77
+ {
78
+ char c;
79
+ read (GetSocket(), &c, 1);
80
+ if (EventCallback)
81
+ (*EventCallback)(GetBinding().c_str(), EM_CONNECTION_READ, &c, 1);
82
+ }
@@ -0,0 +1,107 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: page.cpp
6
+ Date: 30Apr06
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
+ /******************
25
+ PageList::PageList
26
+ ******************/
27
+
28
+ PageList::PageList()
29
+ {
30
+ }
31
+
32
+
33
+ /*******************
34
+ PageList::~PageList
35
+ *******************/
36
+
37
+ PageList::~PageList()
38
+ {
39
+ while (HasPages())
40
+ PopFront();
41
+ }
42
+
43
+
44
+ /***************
45
+ PageList::Front
46
+ ***************/
47
+
48
+ void PageList::Front (const char **page, int *length)
49
+ {
50
+ assert (page && length);
51
+
52
+ if (HasPages()) {
53
+ Page p = Pages.front();
54
+ *page = p.Buffer;
55
+ *length = p.Size;
56
+ }
57
+ else {
58
+ *page = NULL;
59
+ *length = 0;
60
+ }
61
+ }
62
+
63
+
64
+ /******************
65
+ PageList::PopFront
66
+ ******************/
67
+
68
+ void PageList::PopFront()
69
+ {
70
+ if (HasPages()) {
71
+ Page p = Pages.front();
72
+ Pages.pop_front();
73
+ if (p.Buffer)
74
+ free ((void*)p.Buffer);
75
+ }
76
+ }
77
+
78
+
79
+ /******************
80
+ PageList::HasPages
81
+ ******************/
82
+
83
+ bool PageList::HasPages()
84
+ {
85
+ return (Pages.size() > 0) ? true : false;
86
+ }
87
+
88
+
89
+ /**************
90
+ PageList::Push
91
+ **************/
92
+
93
+ void PageList::Push (const char *buf, int size)
94
+ {
95
+ if (buf && (size > 0)) {
96
+ char *copy = (char*) malloc (size);
97
+ if (!copy)
98
+ throw runtime_error ("no memory in pagelist");
99
+ memcpy (copy, buf, size);
100
+ Pages.push_back (Page (copy, size));
101
+ }
102
+ }
103
+
104
+
105
+
106
+
107
+
@@ -0,0 +1,51 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: page.h
6
+ Date: 30Apr06
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
+ #ifndef __PageManager__H_
22
+ #define __PageManager__H_
23
+
24
+
25
+ /**************
26
+ class PageList
27
+ **************/
28
+
29
+ class PageList
30
+ {
31
+ struct Page {
32
+ Page (const char *b, size_t s): Buffer(b), Size(s) {}
33
+ const char *Buffer;
34
+ size_t Size;
35
+ };
36
+
37
+ public:
38
+ PageList();
39
+ virtual ~PageList();
40
+
41
+ void Push (const char*, int);
42
+ bool HasPages();
43
+ void Front (const char**, int*);
44
+ void PopFront();
45
+
46
+ private:
47
+ deque<Page> Pages;
48
+ };
49
+
50
+
51
+ #endif // __PageManager__H_
@@ -0,0 +1,351 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: pipe.cpp
6
+ Date: 30May07
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
+ #ifdef OS_UNIX
24
+ // THIS ENTIRE FILE IS ONLY COMPILED ON UNIX-LIKE SYSTEMS.
25
+
26
+ /******************************
27
+ PipeDescriptor::PipeDescriptor
28
+ ******************************/
29
+
30
+ PipeDescriptor::PipeDescriptor (int fd, pid_t subpid, EventMachine_t *parent_em):
31
+ EventableDescriptor (fd, parent_em),
32
+ bReadAttemptedAfterClose (false),
33
+ LastIo (gCurrentLoopTime),
34
+ InactivityTimeout (0),
35
+ OutboundDataSize (0),
36
+ SubprocessPid (subpid)
37
+ {
38
+ #ifdef HAVE_EPOLL
39
+ EpollEvent.events = EPOLLIN;
40
+ #endif
41
+ #ifdef HAVE_KQUEUE
42
+ MyEventMachine->ArmKqueueReader (this);
43
+ #endif
44
+ }
45
+
46
+
47
+ /*******************************
48
+ PipeDescriptor::~PipeDescriptor
49
+ *******************************/
50
+
51
+ PipeDescriptor::~PipeDescriptor()
52
+ {
53
+ // Run down any stranded outbound data.
54
+ for (size_t i=0; i < OutboundPages.size(); i++)
55
+ OutboundPages[i].Free();
56
+
57
+ /* As a virtual destructor, we come here before the base-class
58
+ * destructor that closes our file-descriptor.
59
+ * We have to make sure the subprocess goes down (if it's not
60
+ * already down) and we have to reap the zombie.
61
+ *
62
+ * This implementation is PROVISIONAL and will surely be improved.
63
+ * The intention here is that we never block, hence the highly
64
+ * undesirable sleeps. But if we can't reap the subprocess even
65
+ * after sending it SIGKILL, then something is wrong and we
66
+ * throw a fatal exception, which is also not something we should
67
+ * be doing.
68
+ *
69
+ * Eventually the right thing to do will be to have the reactor
70
+ * core respond to SIGCHLD by chaining a handler on top of the
71
+ * one Ruby may have installed, and dealing with a list of dead
72
+ * children that are pending cleanup.
73
+ *
74
+ * Since we want to have a signal processor integrated into the
75
+ * client-visible API, let's wait until that is done before cleaning
76
+ * this up.
77
+ *
78
+ * Added a very ugly hack to support passing the subprocess's exit
79
+ * status to the user. It only makes logical sense for user code to access
80
+ * the subprocess exit status in the unbind callback. But unbind is called
81
+ * back during the EventableDescriptor destructor. So by that time there's
82
+ * no way to call back this object through an object binding, because it's
83
+ * already been cleaned up. We might have added a parameter to the unbind
84
+ * callback, but that would probably break a huge amount of existing code.
85
+ * So the hack-solution is to define an instance variable in the EventMachine
86
+ * object and stick the exit status in there, where it can easily be accessed
87
+ * with an accessor visible to user code.
88
+ * User code should ONLY access the exit status from within the unbind callback.
89
+ * Otherwise there's no guarantee it'll be valid.
90
+ * This hack won't make it impossible to run multiple EventMachines in a single
91
+ * process, but it will make it impossible to reliably nest unbind calls
92
+ * within other unbind calls. (Not sure if that's even possible.)
93
+ */
94
+
95
+ assert (MyEventMachine);
96
+
97
+ /* Another hack to make the SubprocessPid available to get_subprocess_status */
98
+ MyEventMachine->SubprocessPid = SubprocessPid;
99
+
100
+ /* 01Mar09: Updated to use a small nanosleep in a loop. When nanosleep is interrupted by SIGCHLD,
101
+ * it resumes the system call after processing the signal (resulting in unnecessary latency).
102
+ * Calling nanosleep in a loop avoids this problem.
103
+ */
104
+ struct timespec req = {0, 50000000}; // 0.05s
105
+ int n;
106
+
107
+ // wait 0.25s for the process to die
108
+ for (n=0; n<5; n++) {
109
+ if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) != 0) return;
110
+ nanosleep (&req, NULL);
111
+ }
112
+
113
+ // send SIGTERM and wait another 0.5s
114
+ kill (SubprocessPid, SIGTERM);
115
+ for (n=0; n<10; n++) {
116
+ nanosleep (&req, NULL);
117
+ if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) != 0) return;
118
+ }
119
+
120
+ // send SIGKILL and wait another 1s
121
+ kill (SubprocessPid, SIGKILL);
122
+ for (n=0; n<20; n++) {
123
+ nanosleep (&req, NULL);
124
+ if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) != 0) return;
125
+ }
126
+
127
+ // still not dead, give up!
128
+ throw std::runtime_error ("unable to reap subprocess");
129
+ }
130
+
131
+
132
+
133
+ /********************
134
+ PipeDescriptor::Read
135
+ ********************/
136
+
137
+ void PipeDescriptor::Read()
138
+ {
139
+ int sd = GetSocket();
140
+ if (sd == INVALID_SOCKET) {
141
+ assert (!bReadAttemptedAfterClose);
142
+ bReadAttemptedAfterClose = true;
143
+ return;
144
+ }
145
+
146
+ LastIo = gCurrentLoopTime;
147
+
148
+ int total_bytes_read = 0;
149
+ char readbuffer [16 * 1024];
150
+
151
+ for (int i=0; i < 10; i++) {
152
+ // Don't read just one buffer and then move on. This is faster
153
+ // if there is a lot of incoming.
154
+ // But don't read indefinitely. Give other sockets a chance to run.
155
+ // NOTICE, we're reading one less than the buffer size.
156
+ // That's so we can put a guard byte at the end of what we send
157
+ // to user code.
158
+ // Use read instead of recv, which on Linux gives a "socket operation
159
+ // on nonsocket" error.
160
+
161
+
162
+ int r = read (sd, readbuffer, sizeof(readbuffer) - 1);
163
+ //cerr << "<R:" << r << ">";
164
+
165
+ if (r > 0) {
166
+ total_bytes_read += r;
167
+ LastRead = gCurrentLoopTime;
168
+
169
+ // Add a null-terminator at the the end of the buffer
170
+ // that we will send to the callback.
171
+ // DO NOT EVER CHANGE THIS. We want to explicitly allow users
172
+ // to be able to depend on this behavior, so they will have
173
+ // the option to do some things faster. Additionally it's
174
+ // a security guard against buffer overflows.
175
+ readbuffer [r] = 0;
176
+ if (EventCallback)
177
+ (*EventCallback)(GetBinding().c_str(), EM_CONNECTION_READ, readbuffer, r);
178
+ }
179
+ else if (r == 0) {
180
+ break;
181
+ }
182
+ else {
183
+ // Basically a would-block, meaning we've read everything there is to read.
184
+ break;
185
+ }
186
+
187
+ }
188
+
189
+
190
+ if (total_bytes_read == 0) {
191
+ // If we read no data on a socket that selected readable,
192
+ // it generally means the other end closed the connection gracefully.
193
+ ScheduleClose (false);
194
+ //bCloseNow = true;
195
+ }
196
+
197
+ }
198
+
199
+ /*********************
200
+ PipeDescriptor::Write
201
+ *********************/
202
+
203
+ void PipeDescriptor::Write()
204
+ {
205
+ int sd = GetSocket();
206
+ assert (sd != INVALID_SOCKET);
207
+
208
+ LastIo = gCurrentLoopTime;
209
+ char output_buffer [16 * 1024];
210
+ size_t nbytes = 0;
211
+
212
+ while ((OutboundPages.size() > 0) && (nbytes < sizeof(output_buffer))) {
213
+ OutboundPage *op = &(OutboundPages[0]);
214
+ if ((nbytes + op->Length - op->Offset) < sizeof (output_buffer)) {
215
+ memcpy (output_buffer + nbytes, op->Buffer + op->Offset, op->Length - op->Offset);
216
+ nbytes += (op->Length - op->Offset);
217
+ op->Free();
218
+ OutboundPages.pop_front();
219
+ }
220
+ else {
221
+ int len = sizeof(output_buffer) - nbytes;
222
+ memcpy (output_buffer + nbytes, op->Buffer + op->Offset, len);
223
+ op->Offset += len;
224
+ nbytes += len;
225
+ }
226
+ }
227
+
228
+ // We should never have gotten here if there were no data to write,
229
+ // so assert that as a sanity check.
230
+ // Don't bother to make sure nbytes is less than output_buffer because
231
+ // if it were we probably would have crashed already.
232
+ assert (nbytes > 0);
233
+
234
+ assert (GetSocket() != INVALID_SOCKET);
235
+ int bytes_written = write (GetSocket(), output_buffer, nbytes);
236
+
237
+ if (bytes_written > 0) {
238
+ OutboundDataSize -= bytes_written;
239
+ if ((size_t)bytes_written < nbytes) {
240
+ int len = nbytes - bytes_written;
241
+ char *buffer = (char*) malloc (len + 1);
242
+ if (!buffer)
243
+ throw std::runtime_error ("bad alloc throwing back data");
244
+ memcpy (buffer, output_buffer + bytes_written, len);
245
+ buffer [len] = 0;
246
+ OutboundPages.push_front (OutboundPage (buffer, len));
247
+ }
248
+ #ifdef HAVE_EPOLL
249
+ EpollEvent.events = (EPOLLIN | (SelectForWrite() ? EPOLLOUT : 0));
250
+ assert (MyEventMachine);
251
+ MyEventMachine->Modify (this);
252
+ #endif
253
+ }
254
+ else {
255
+ #ifdef OS_UNIX
256
+ if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK) && (errno != EINTR))
257
+ #endif
258
+ #ifdef OS_WIN32
259
+ if ((errno != WSAEINPROGRESS) && (errno != WSAEWOULDBLOCK))
260
+ #endif
261
+ Close();
262
+ }
263
+ }
264
+
265
+
266
+ /*************************
267
+ PipeDescriptor::Heartbeat
268
+ *************************/
269
+
270
+ void PipeDescriptor::Heartbeat()
271
+ {
272
+ // If an inactivity timeout is defined, then check for it.
273
+ if (InactivityTimeout && ((gCurrentLoopTime - LastIo) >= InactivityTimeout))
274
+ ScheduleClose (false);
275
+ //bCloseNow = true;
276
+ }
277
+
278
+
279
+ /*****************************
280
+ PipeDescriptor::SelectForRead
281
+ *****************************/
282
+
283
+ bool PipeDescriptor::SelectForRead()
284
+ {
285
+ /* Pipe descriptors, being local by definition, don't have
286
+ * a pending state, so this is simpler than for the
287
+ * ConnectionDescriptor object.
288
+ */
289
+ return true;
290
+ }
291
+
292
+ /******************************
293
+ PipeDescriptor::SelectForWrite
294
+ ******************************/
295
+
296
+ bool PipeDescriptor::SelectForWrite()
297
+ {
298
+ /* Pipe descriptors, being local by definition, don't have
299
+ * a pending state, so this is simpler than for the
300
+ * ConnectionDescriptor object.
301
+ */
302
+ return (GetOutboundDataSize() > 0);
303
+ }
304
+
305
+
306
+
307
+
308
+ /********************************
309
+ PipeDescriptor::SendOutboundData
310
+ ********************************/
311
+
312
+ int PipeDescriptor::SendOutboundData (const char *data, int length)
313
+ {
314
+ //if (bCloseNow || bCloseAfterWriting)
315
+ if (IsCloseScheduled())
316
+ return 0;
317
+
318
+ if (!data && (length > 0))
319
+ throw std::runtime_error ("bad outbound data");
320
+ char *buffer = (char *) malloc (length + 1);
321
+ if (!buffer)
322
+ throw std::runtime_error ("no allocation for outbound data");
323
+ memcpy (buffer, data, length);
324
+ buffer [length] = 0;
325
+ OutboundPages.push_back (OutboundPage (buffer, length));
326
+ OutboundDataSize += length;
327
+ #ifdef HAVE_EPOLL
328
+ EpollEvent.events = (EPOLLIN | EPOLLOUT);
329
+ assert (MyEventMachine);
330
+ MyEventMachine->Modify (this);
331
+ #endif
332
+ return length;
333
+ }
334
+
335
+ /********************************
336
+ PipeDescriptor::GetSubprocessPid
337
+ ********************************/
338
+
339
+ bool PipeDescriptor::GetSubprocessPid (pid_t *pid)
340
+ {
341
+ bool ok = false;
342
+ if (pid && (SubprocessPid > 0)) {
343
+ *pid = SubprocessPid;
344
+ ok = true;
345
+ }
346
+ return ok;
347
+ }
348
+
349
+
350
+ #endif // OS_UNIX
351
+