eventmachine-le 1.1.0.beta.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +21 -0
- data/.yardopts +7 -0
- data/GNU +281 -0
- data/LICENSE +60 -0
- data/README.md +80 -0
- data/Rakefile +19 -0
- data/eventmachine-le.gemspec +42 -0
- data/ext/binder.cpp +124 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +841 -0
- data/ext/ed.cpp +1995 -0
- data/ext/ed.h +424 -0
- data/ext/em.cpp +2377 -0
- data/ext/em.h +243 -0
- data/ext/eventmachine.h +126 -0
- data/ext/extconf.rb +166 -0
- data/ext/fastfilereader/extconf.rb +94 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/kb.cpp +79 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +347 -0
- data/ext/project.h +155 -0
- data/ext/rubymain.cpp +1269 -0
- data/ext/ssl.cpp +468 -0
- data/ext/ssl.h +94 -0
- data/lib/em/buftok.rb +110 -0
- data/lib/em/callback.rb +58 -0
- data/lib/em/channel.rb +64 -0
- data/lib/em/completion.rb +304 -0
- data/lib/em/connection.rb +728 -0
- data/lib/em/deferrable.rb +210 -0
- data/lib/em/deferrable/pool.rb +2 -0
- data/lib/em/file_watch.rb +73 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/iterator.rb +313 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/pool.rb +151 -0
- data/lib/em/process_watch.rb +45 -0
- data/lib/em/processes.rb +123 -0
- data/lib/em/protocols.rb +37 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +279 -0
- data/lib/em/protocols/httpclient2.rb +600 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/line_protocol.rb +29 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +331 -0
- data/lib/em/protocols/object_protocol.rb +46 -0
- data/lib/em/protocols/postgres3.rb +246 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +365 -0
- data/lib/em/protocols/smtpserver.rb +663 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +202 -0
- data/lib/em/protocols/tcptest.rb +54 -0
- data/lib/em/queue.rb +71 -0
- data/lib/em/resolver.rb +195 -0
- data/lib/em/spawnable.rb +84 -0
- data/lib/em/streamer.rb +118 -0
- data/lib/em/threaded_resource.rb +90 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +106 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine-le.rb +10 -0
- data/lib/eventmachine.rb +1548 -0
- data/rakelib/cpp.rake_example +77 -0
- data/rakelib/package.rake +98 -0
- data/rakelib/test.rake +8 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/em_test_helper.rb +143 -0
- data/tests/test_attach.rb +148 -0
- data/tests/test_basic.rb +294 -0
- data/tests/test_channel.rb +62 -0
- data/tests/test_completion.rb +177 -0
- data/tests/test_connection_count.rb +33 -0
- data/tests/test_defer.rb +18 -0
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +134 -0
- data/tests/test_error_handler.rb +38 -0
- data/tests/test_exc.rb +28 -0
- data/tests/test_file_watch.rb +65 -0
- data/tests/test_futures.rb +170 -0
- data/tests/test_get_sock_opt.rb +37 -0
- data/tests/test_handler_check.rb +35 -0
- data/tests/test_hc.rb +155 -0
- data/tests/test_httpclient.rb +190 -0
- data/tests/test_httpclient2.rb +128 -0
- data/tests/test_inactivity_timeout.rb +54 -0
- data/tests/test_ipv4.rb +125 -0
- data/tests/test_ipv6.rb +131 -0
- data/tests/test_iterator.rb +110 -0
- data/tests/test_kb.rb +34 -0
- data/tests/test_line_protocol.rb +33 -0
- data/tests/test_ltp.rb +138 -0
- data/tests/test_ltp2.rb +288 -0
- data/tests/test_next_tick.rb +104 -0
- data/tests/test_object_protocol.rb +36 -0
- data/tests/test_pause.rb +78 -0
- data/tests/test_pending_connect_timeout.rb +52 -0
- data/tests/test_pool.rb +196 -0
- data/tests/test_process_watch.rb +48 -0
- data/tests/test_processes.rb +133 -0
- data/tests/test_proxy_connection.rb +168 -0
- data/tests/test_pure.rb +88 -0
- data/tests/test_queue.rb +50 -0
- data/tests/test_resolver.rb +55 -0
- data/tests/test_running.rb +14 -0
- data/tests/test_sasl.rb +47 -0
- data/tests/test_send_file.rb +217 -0
- data/tests/test_servers.rb +33 -0
- data/tests/test_set_sock_opt.rb +41 -0
- data/tests/test_shutdown_hooks.rb +23 -0
- data/tests/test_smtpclient.rb +55 -0
- data/tests/test_smtpserver.rb +120 -0
- data/tests/test_spawn.rb +293 -0
- data/tests/test_ssl_args.rb +78 -0
- data/tests/test_ssl_methods.rb +48 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_threaded_resource.rb +55 -0
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +180 -0
- data/tests/test_ud.rb +8 -0
- data/tests/test_udp46.rb +53 -0
- data/tests/test_unbind_reason.rb +48 -0
- metadata +390 -0
data/ext/kb.cpp
ADDED
@@ -0,0 +1,79 @@
|
|
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
|
+
{
|
31
|
+
#ifdef HAVE_EPOLL
|
32
|
+
EpollEvent.events = EPOLLIN;
|
33
|
+
#endif
|
34
|
+
#ifdef HAVE_KQUEUE
|
35
|
+
MyEventMachine->ArmKqueueReader (this);
|
36
|
+
#endif
|
37
|
+
}
|
38
|
+
|
39
|
+
|
40
|
+
/***************************************
|
41
|
+
KeyboardDescriptor::~KeyboardDescriptor
|
42
|
+
***************************************/
|
43
|
+
|
44
|
+
KeyboardDescriptor::~KeyboardDescriptor()
|
45
|
+
{
|
46
|
+
}
|
47
|
+
|
48
|
+
|
49
|
+
/*************************
|
50
|
+
KeyboardDescriptor::Write
|
51
|
+
*************************/
|
52
|
+
|
53
|
+
void KeyboardDescriptor::Write()
|
54
|
+
{
|
55
|
+
// Why are we here?
|
56
|
+
throw std::runtime_error ("bad code path in keyboard handler");
|
57
|
+
}
|
58
|
+
|
59
|
+
|
60
|
+
/*****************************
|
61
|
+
KeyboardDescriptor::Heartbeat
|
62
|
+
*****************************/
|
63
|
+
|
64
|
+
void KeyboardDescriptor::Heartbeat()
|
65
|
+
{
|
66
|
+
// no-op
|
67
|
+
}
|
68
|
+
|
69
|
+
|
70
|
+
/************************
|
71
|
+
KeyboardDescriptor::Read
|
72
|
+
************************/
|
73
|
+
|
74
|
+
void KeyboardDescriptor::Read()
|
75
|
+
{
|
76
|
+
char c;
|
77
|
+
read (GetSocket(), &c, 1);
|
78
|
+
_GenericInboundDispatch(&c, 1);
|
79
|
+
}
|
data/ext/page.cpp
ADDED
@@ -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
|
+
|
data/ext/page.h
ADDED
@@ -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_
|
data/ext/pipe.cpp
ADDED
@@ -0,0 +1,347 @@
|
|
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
|
+
OutboundDataSize (0),
|
34
|
+
SubprocessPid (subpid)
|
35
|
+
{
|
36
|
+
#ifdef HAVE_EPOLL
|
37
|
+
EpollEvent.events = EPOLLIN;
|
38
|
+
#endif
|
39
|
+
#ifdef HAVE_KQUEUE
|
40
|
+
MyEventMachine->ArmKqueueReader (this);
|
41
|
+
#endif
|
42
|
+
}
|
43
|
+
|
44
|
+
|
45
|
+
/*******************************
|
46
|
+
PipeDescriptor::~PipeDescriptor
|
47
|
+
*******************************/
|
48
|
+
|
49
|
+
PipeDescriptor::~PipeDescriptor()
|
50
|
+
{
|
51
|
+
// Run down any stranded outbound data.
|
52
|
+
for (size_t i=0; i < OutboundPages.size(); i++)
|
53
|
+
OutboundPages[i].Free();
|
54
|
+
|
55
|
+
/* As a virtual destructor, we come here before the base-class
|
56
|
+
* destructor that closes our file-descriptor.
|
57
|
+
* We have to make sure the subprocess goes down (if it's not
|
58
|
+
* already down) and we have to reap the zombie.
|
59
|
+
*
|
60
|
+
* This implementation is PROVISIONAL and will surely be improved.
|
61
|
+
* The intention here is that we never block, hence the highly
|
62
|
+
* undesirable sleeps. But if we can't reap the subprocess even
|
63
|
+
* after sending it SIGKILL, then something is wrong and we
|
64
|
+
* throw a fatal exception, which is also not something we should
|
65
|
+
* be doing.
|
66
|
+
*
|
67
|
+
* Eventually the right thing to do will be to have the reactor
|
68
|
+
* core respond to SIGCHLD by chaining a handler on top of the
|
69
|
+
* one Ruby may have installed, and dealing with a list of dead
|
70
|
+
* children that are pending cleanup.
|
71
|
+
*
|
72
|
+
* Since we want to have a signal processor integrated into the
|
73
|
+
* client-visible API, let's wait until that is done before cleaning
|
74
|
+
* this up.
|
75
|
+
*
|
76
|
+
* Added a very ugly hack to support passing the subprocess's exit
|
77
|
+
* status to the user. It only makes logical sense for user code to access
|
78
|
+
* the subprocess exit status in the unbind callback. But unbind is called
|
79
|
+
* back during the EventableDescriptor destructor. So by that time there's
|
80
|
+
* no way to call back this object through an object binding, because it's
|
81
|
+
* already been cleaned up. We might have added a parameter to the unbind
|
82
|
+
* callback, but that would probably break a huge amount of existing code.
|
83
|
+
* So the hack-solution is to define an instance variable in the EventMachine
|
84
|
+
* object and stick the exit status in there, where it can easily be accessed
|
85
|
+
* with an accessor visible to user code.
|
86
|
+
* User code should ONLY access the exit status from within the unbind callback.
|
87
|
+
* Otherwise there's no guarantee it'll be valid.
|
88
|
+
* This hack won't make it impossible to run multiple EventMachines in a single
|
89
|
+
* process, but it will make it impossible to reliably nest unbind calls
|
90
|
+
* within other unbind calls. (Not sure if that's even possible.)
|
91
|
+
*/
|
92
|
+
|
93
|
+
assert (MyEventMachine);
|
94
|
+
|
95
|
+
/* Another hack to make the SubprocessPid available to get_subprocess_status */
|
96
|
+
MyEventMachine->SubprocessPid = SubprocessPid;
|
97
|
+
|
98
|
+
/* 01Mar09: Updated to use a small nanosleep in a loop. When nanosleep is interrupted by SIGCHLD,
|
99
|
+
* it resumes the system call after processing the signal (resulting in unnecessary latency).
|
100
|
+
* Calling nanosleep in a loop avoids this problem.
|
101
|
+
*/
|
102
|
+
struct timespec req = {0, 50000000}; // 0.05s
|
103
|
+
int n;
|
104
|
+
|
105
|
+
// wait 0.5s for the process to die
|
106
|
+
for (n=0; n<10; n++) {
|
107
|
+
if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) != 0) return;
|
108
|
+
nanosleep (&req, NULL);
|
109
|
+
}
|
110
|
+
|
111
|
+
// send SIGTERM and wait another 1s
|
112
|
+
kill (SubprocessPid, SIGTERM);
|
113
|
+
for (n=0; n<20; n++) {
|
114
|
+
nanosleep (&req, NULL);
|
115
|
+
if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) != 0) return;
|
116
|
+
}
|
117
|
+
|
118
|
+
// send SIGKILL and wait another 5s
|
119
|
+
kill (SubprocessPid, SIGKILL);
|
120
|
+
for (n=0; n<100; n++) {
|
121
|
+
nanosleep (&req, NULL);
|
122
|
+
if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) != 0) return;
|
123
|
+
}
|
124
|
+
|
125
|
+
// still not dead, give up!
|
126
|
+
throw std::runtime_error ("unable to reap subprocess");
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
/********************
|
132
|
+
PipeDescriptor::Read
|
133
|
+
********************/
|
134
|
+
|
135
|
+
void PipeDescriptor::Read()
|
136
|
+
{
|
137
|
+
int sd = GetSocket();
|
138
|
+
if (sd == INVALID_SOCKET) {
|
139
|
+
assert (!bReadAttemptedAfterClose);
|
140
|
+
bReadAttemptedAfterClose = true;
|
141
|
+
return;
|
142
|
+
}
|
143
|
+
|
144
|
+
LastActivity = MyEventMachine->GetCurrentLoopTime();
|
145
|
+
|
146
|
+
int total_bytes_read = 0;
|
147
|
+
char readbuffer [16 * 1024];
|
148
|
+
|
149
|
+
for (int i=0; i < 10; i++) {
|
150
|
+
// Don't read just one buffer and then move on. This is faster
|
151
|
+
// if there is a lot of incoming.
|
152
|
+
// But don't read indefinitely. Give other sockets a chance to run.
|
153
|
+
// NOTICE, we're reading one less than the buffer size.
|
154
|
+
// That's so we can put a guard byte at the end of what we send
|
155
|
+
// to user code.
|
156
|
+
// Use read instead of recv, which on Linux gives a "socket operation
|
157
|
+
// on nonsocket" error.
|
158
|
+
|
159
|
+
|
160
|
+
int r = read (sd, readbuffer, sizeof(readbuffer) - 1);
|
161
|
+
//cerr << "<R:" << r << ">";
|
162
|
+
|
163
|
+
if (r > 0) {
|
164
|
+
total_bytes_read += r;
|
165
|
+
|
166
|
+
// Add a null-terminator at the the end of the buffer
|
167
|
+
// that we will send to the callback.
|
168
|
+
// DO NOT EVER CHANGE THIS. We want to explicitly allow users
|
169
|
+
// to be able to depend on this behavior, so they will have
|
170
|
+
// the option to do some things faster. Additionally it's
|
171
|
+
// a security guard against buffer overflows.
|
172
|
+
readbuffer [r] = 0;
|
173
|
+
_GenericInboundDispatch(readbuffer, r);
|
174
|
+
}
|
175
|
+
else if (r == 0) {
|
176
|
+
break;
|
177
|
+
}
|
178
|
+
else {
|
179
|
+
// Basically a would-block, meaning we've read everything there is to read.
|
180
|
+
break;
|
181
|
+
}
|
182
|
+
|
183
|
+
}
|
184
|
+
|
185
|
+
|
186
|
+
if (total_bytes_read == 0) {
|
187
|
+
// If we read no data on a socket that selected readable,
|
188
|
+
// it generally means the other end closed the connection gracefully.
|
189
|
+
ScheduleClose (false);
|
190
|
+
//bCloseNow = true;
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
/*********************
|
196
|
+
PipeDescriptor::Write
|
197
|
+
*********************/
|
198
|
+
|
199
|
+
void PipeDescriptor::Write()
|
200
|
+
{
|
201
|
+
int sd = GetSocket();
|
202
|
+
assert (sd != INVALID_SOCKET);
|
203
|
+
|
204
|
+
LastActivity = MyEventMachine->GetCurrentLoopTime();
|
205
|
+
char output_buffer [16 * 1024];
|
206
|
+
size_t nbytes = 0;
|
207
|
+
|
208
|
+
while ((OutboundPages.size() > 0) && (nbytes < sizeof(output_buffer))) {
|
209
|
+
OutboundPage *op = &(OutboundPages[0]);
|
210
|
+
if ((nbytes + op->Length - op->Offset) < sizeof (output_buffer)) {
|
211
|
+
memcpy (output_buffer + nbytes, op->Buffer + op->Offset, op->Length - op->Offset);
|
212
|
+
nbytes += (op->Length - op->Offset);
|
213
|
+
op->Free();
|
214
|
+
OutboundPages.pop_front();
|
215
|
+
}
|
216
|
+
else {
|
217
|
+
int len = sizeof(output_buffer) - nbytes;
|
218
|
+
memcpy (output_buffer + nbytes, op->Buffer + op->Offset, len);
|
219
|
+
op->Offset += len;
|
220
|
+
nbytes += len;
|
221
|
+
}
|
222
|
+
}
|
223
|
+
|
224
|
+
// We should never have gotten here if there were no data to write,
|
225
|
+
// so assert that as a sanity check.
|
226
|
+
// Don't bother to make sure nbytes is less than output_buffer because
|
227
|
+
// if it were we probably would have crashed already.
|
228
|
+
assert (nbytes > 0);
|
229
|
+
|
230
|
+
assert (GetSocket() != INVALID_SOCKET);
|
231
|
+
int bytes_written = write (GetSocket(), output_buffer, nbytes);
|
232
|
+
|
233
|
+
if (bytes_written > 0) {
|
234
|
+
OutboundDataSize -= bytes_written;
|
235
|
+
if ((size_t)bytes_written < nbytes) {
|
236
|
+
int len = nbytes - bytes_written;
|
237
|
+
char *buffer = (char*) malloc (len + 1);
|
238
|
+
if (!buffer)
|
239
|
+
throw std::runtime_error ("bad alloc throwing back data");
|
240
|
+
memcpy (buffer, output_buffer + bytes_written, len);
|
241
|
+
buffer [len] = 0;
|
242
|
+
OutboundPages.push_front (OutboundPage (buffer, len));
|
243
|
+
}
|
244
|
+
#ifdef HAVE_EPOLL
|
245
|
+
EpollEvent.events = (EPOLLIN | (SelectForWrite() ? EPOLLOUT : 0));
|
246
|
+
assert (MyEventMachine);
|
247
|
+
MyEventMachine->Modify (this);
|
248
|
+
#endif
|
249
|
+
}
|
250
|
+
else {
|
251
|
+
#ifdef OS_UNIX
|
252
|
+
if ((errno != EINPROGRESS) && (errno != EWOULDBLOCK) && (errno != EINTR))
|
253
|
+
#endif
|
254
|
+
#ifdef OS_WIN32
|
255
|
+
if ((errno != WSAEINPROGRESS) && (errno != WSAEWOULDBLOCK))
|
256
|
+
#endif
|
257
|
+
Close();
|
258
|
+
}
|
259
|
+
}
|
260
|
+
|
261
|
+
|
262
|
+
/*************************
|
263
|
+
PipeDescriptor::Heartbeat
|
264
|
+
*************************/
|
265
|
+
|
266
|
+
void PipeDescriptor::Heartbeat()
|
267
|
+
{
|
268
|
+
// If an inactivity timeout is defined, then check for it.
|
269
|
+
if (InactivityTimeout && ((MyEventMachine->GetCurrentLoopTime() - LastActivity) >= InactivityTimeout))
|
270
|
+
ScheduleClose (false);
|
271
|
+
//bCloseNow = true;
|
272
|
+
}
|
273
|
+
|
274
|
+
|
275
|
+
/*****************************
|
276
|
+
PipeDescriptor::SelectForRead
|
277
|
+
*****************************/
|
278
|
+
|
279
|
+
bool PipeDescriptor::SelectForRead()
|
280
|
+
{
|
281
|
+
/* Pipe descriptors, being local by definition, don't have
|
282
|
+
* a pending state, so this is simpler than for the
|
283
|
+
* ConnectionDescriptor object.
|
284
|
+
*/
|
285
|
+
return bPaused ? false : true;
|
286
|
+
}
|
287
|
+
|
288
|
+
/******************************
|
289
|
+
PipeDescriptor::SelectForWrite
|
290
|
+
******************************/
|
291
|
+
|
292
|
+
bool PipeDescriptor::SelectForWrite()
|
293
|
+
{
|
294
|
+
/* Pipe descriptors, being local by definition, don't have
|
295
|
+
* a pending state, so this is simpler than for the
|
296
|
+
* ConnectionDescriptor object.
|
297
|
+
*/
|
298
|
+
return (GetOutboundDataSize() > 0) && !bPaused ? true : false;
|
299
|
+
}
|
300
|
+
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
/********************************
|
305
|
+
PipeDescriptor::SendOutboundData
|
306
|
+
********************************/
|
307
|
+
|
308
|
+
int PipeDescriptor::SendOutboundData (const char *data, int length)
|
309
|
+
{
|
310
|
+
//if (bCloseNow || bCloseAfterWriting)
|
311
|
+
if (IsCloseScheduled())
|
312
|
+
return 0;
|
313
|
+
|
314
|
+
if (!data && (length > 0))
|
315
|
+
throw std::runtime_error ("bad outbound data");
|
316
|
+
char *buffer = (char *) malloc (length + 1);
|
317
|
+
if (!buffer)
|
318
|
+
throw std::runtime_error ("no allocation for outbound data");
|
319
|
+
memcpy (buffer, data, length);
|
320
|
+
buffer [length] = 0;
|
321
|
+
OutboundPages.push_back (OutboundPage (buffer, length));
|
322
|
+
OutboundDataSize += length;
|
323
|
+
#ifdef HAVE_EPOLL
|
324
|
+
EpollEvent.events = (EPOLLIN | EPOLLOUT);
|
325
|
+
assert (MyEventMachine);
|
326
|
+
MyEventMachine->Modify (this);
|
327
|
+
#endif
|
328
|
+
return length;
|
329
|
+
}
|
330
|
+
|
331
|
+
/********************************
|
332
|
+
PipeDescriptor::GetSubprocessPid
|
333
|
+
********************************/
|
334
|
+
|
335
|
+
bool PipeDescriptor::GetSubprocessPid (pid_t *pid)
|
336
|
+
{
|
337
|
+
bool ok = false;
|
338
|
+
if (pid && (SubprocessPid > 0)) {
|
339
|
+
*pid = SubprocessPid;
|
340
|
+
ok = true;
|
341
|
+
}
|
342
|
+
return ok;
|
343
|
+
}
|
344
|
+
|
345
|
+
|
346
|
+
#endif // OS_UNIX
|
347
|
+
|