MCPcopy Create free account
hub / github.com/apache/thrift / peek

Method peek

lib/cpp/src/thrift/transport/TSocket.cpp:214–267  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

212}
213
214bool TSocket::peek() {
215 if (!isOpen()) {
216 return false;
217 }
218 if (interruptListener_) {
219 for (int retries = 0;;) {
220 struct THRIFT_POLLFD fds[2];
221 std::memset(fds, 0, sizeof(fds));
222 fds[0].fd = socket_;
223 fds[0].events = THRIFT_POLLIN;
224 fds[1].fd = *(interruptListener_.get());
225 fds[1].events = THRIFT_POLLIN;
226 int ret = THRIFT_POLL(fds, 2, (recvTimeout_ == 0) ? -1 : recvTimeout_);
227 int errno_copy = THRIFT_GET_SOCKET_ERROR;
228 if (ret < 0) {
229 // error cases
230 if (errno_copy == THRIFT_EINTR && (retries++ < maxRecvRetries_)) {
231 continue;
232 }
233 TOutput::instance().perror("TSocket::peek() THRIFT_POLL() ", errno_copy);
234 throw TTransportException(TTransportException::UNKNOWN, "Unknown", errno_copy);
235 } else if (ret > 0) {
236 // Check the interruptListener
237 if (fds[1].revents & THRIFT_POLLIN) {
238 return false;
239 }
240 // There must be data or a disconnection, fall through to the PEEK
241 break;
242 } else {
243 // timeout
244 return false;
245 }
246 }
247 }
248
249 // Check to see if data is available or if the remote side closed
250 uint8_t buf;
251 int r = static_cast<int>(recv(socket_, cast_sockopt(&buf), 1, MSG_PEEK));
252 if (r == -1) {
253 int errno_copy = THRIFT_GET_SOCKET_ERROR;
254#if defined __FreeBSD__ || defined __MACH__
255 /* shigin:
256 * freebsd returns -1 and THRIFT_ECONNRESET if socket was closed by
257 * the other side
258 */
259 if (errno_copy == THRIFT_ECONNRESET) {
260 return false;
261 }
262#endif
263 TOutput::instance().perror("TSocket::peek() recv() " + getSocketInfo(), errno_copy);
264 throw TTransportException(TTransportException::UNKNOWN, "recv()", errno_copy);
265 }
266 return (r > 0);
267}
268
269void TSocket::openConnection(struct addrinfo* res) {
270

Callers

nothing calls this directly

Calls 4

TTransportExceptionClass · 0.70
cast_sockoptFunction · 0.70
getMethod · 0.45
perrorMethod · 0.45

Tested by

no test coverage detected