MCPcopy Create free account
hub / github.com/3rdparty/libprocess / recv

Method recv

src/socket.cpp:145–193  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

143
144
145Future<string> SocketImpl::recv(const Option<ssize_t>& size)
146{
147 // Extend lifetime by holding onto a reference to ourself!
148 auto self = shared_from_this();
149
150 // Default chunk size to attempt to receive when nothing is
151 // specified represents roughly 16 pages.
152 static const size_t DEFAULT_CHUNK = 16 * os::pagesize();
153
154 const size_t chunk = (size.isNone() || size.get() < 0)
155 ? DEFAULT_CHUNK
156 : size.get();
157
158 boost::shared_array<char> data(new char[chunk]);
159 string buffer;
160
161 return loop(
162 None(),
163 [=]() {
164 return self->recv(data.get(), chunk);
165 },
166 [=](size_t length) mutable -> ControlFlow<string> {
167 if (length == 0) { // EOF.
168 // Return everything we've received thus far, a subsequent
169 // receive will return an empty string.
170 return Break(std::move(buffer));
171 }
172
173 buffer.append(data.get(), length);
174
175 if (size.isNone()) {
176 // We've been asked just to return any data that we receive!
177 return Break(std::move(buffer));
178 } else if (size.get() < 0) {
179 // We've been asked to receive until EOF so keep receiving
180 // since according to the 'length == 0' check above we
181 // haven't reached EOF yet.
182 return Continue();
183 } else if (
184 static_cast<string::size_type>(size.get()) > buffer.size()) {
185 // We've been asked to receive a particular amount of data and we
186 // haven't yet received that much data so keep receiving.
187 return Continue();
188 }
189
190 // We've received as much data as requested, so return that data!
191 return Break(std::move(buffer));
192 });
193}
194
195
196Future<Nothing> SocketImpl::send(const string& data)

Callers 6

readMethod · 0.45
receiveFunction · 0.45
receiveFunction · 0.45
ignore_recv_dataFunction · 0.45
synchronizedFunction · 0.45
send_connectMethod · 0.45

Calls 5

loopFunction · 0.85
BreakFunction · 0.50
ContinueClass · 0.50
getMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected