1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
/*
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
#include "qpid/sys/AsynchIO.h"
#include "check.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <boost/bind.hpp>
using namespace qpid::sys;
namespace {
/*
* Make file descriptor non-blocking
*/
void nonblocking(int fd) {
QPID_POSIX_CHECK(::fcntl(fd, F_SETFL, O_NONBLOCK));
}
}
/*
* Asynch Acceptor
*/
AsynchAcceptor::AsynchAcceptor(int fd, Callback callback) :
acceptedCallback(callback),
handle(fd, boost::bind(&AsynchAcceptor::readable, this, _1), 0) {
nonblocking(fd);
}
void AsynchAcceptor::start(Poller::shared_ptr poller) {
handle.watch(poller);
}
/*
* We keep on accepting as long as there is something to accept
*/
void AsynchAcceptor::readable(DispatchHandle& h) {
int afd;
do {
errno = 0;
// TODO: Currently we ignore the peers address, perhaps we should
// log it or use it for connection acceptance.
afd = ::accept(h.getFD(), 0, 0);
if (afd >= 0) {
acceptedCallback(afd);
} else if (errno == EAGAIN) {
break;
} else {
QPID_POSIX_CHECK(afd);
}
} while (true);
h.rewatch();
}
/*
* Asynch reader/writer
*/
AsynchIO::AsynchIO(int fd, ReadCallback rCb, EofCallback eofCb, BuffersEmptyCallback eCb, IdleCallback iCb) :
readCallback(rCb),
eofCallback(eofCb),
emptyCallback(eCb),
idleCallback(iCb),
handle(fd, boost::bind(&AsynchIO::readable, this, _1), boost::bind(&AsynchIO::writeable, this, _1)) {
nonblocking(fd);
}
void AsynchIO::start(Poller::shared_ptr poller) {
handle.watch(poller);
}
void AsynchIO::QueueReadBuffer(const Buffer& buff) {
bufferQueue.push_front(buff);
handle.rewatchRead();
}
void AsynchIO::QueueWrite(const Buffer& buff) {
writeQueue.push_front(buff);
handle.rewatchWrite();
}
/*
* We keep on reading as long as we have something to read and a buffer to put
* it in
*/
void AsynchIO::readable(DispatchHandle& h) {
do {
// (Try to) get a buffer
if (!bufferQueue.empty()) {
// Read into buffer
Buffer buff = bufferQueue.back();
bufferQueue.pop_back();
errno = 0;
int rc = ::read(h.getFD(), buff.bytes, buff.byteCount);
if (rc == 0) {
eofCallback();
} else if (rc > 0) {
readCallback(buff, rc);
} else {
// Put buffer back
bufferQueue.push_back(buff);
if (errno == EAGAIN) {
// We must have just put a buffer back so we know
// we can do this
h.rewatchRead();
return;
} else {
QPID_POSIX_CHECK(rc);
}
}
} else {
// Something to read but no buffer
if (emptyCallback) {
emptyCallback();
}
// If we still have no buffers we can't do anything more
if (bufferQueue.empty()) {
return;
}
}
} while (true);
}
/*
* We carry on writing whilst we have data to write and we can write
*/
void AsynchIO::writeable(DispatchHandle& h) {
do {
// See if we've got something to write
if (!writeQueue.empty()) {
// Write buffer
Buffer buff = writeQueue.back();
writeQueue.pop_back();
errno = 0;
int rc = ::write(h.getFD(), buff.bytes, buff.byteCount);
if (rc >= 0) {
// Recycle the buffer
QueueReadBuffer(buff);
} else {
// Put buffer back
writeQueue.push_back(buff);
if (errno == EAGAIN) {
// We have just put a buffer back so we know
// we can do this
h.rewatchWrite();
return;
} else {
QPID_POSIX_CHECK(rc);
}
}
} else {
// Something to read but no buffer
if (idleCallback) {
idleCallback(h.getFD());
}
// If we still have no buffers to write we can't do anything more
if (writeQueue.empty()) {
return;
}
}
} while (true);
}
|