| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include <errno.h> | 5 #include <errno.h> |
| 6 #include <poll.h> | 6 #include <poll.h> |
| 7 #include <pthread.h> | 7 #include <pthread.h> |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <string.h> | 9 #include <string.h> |
| 10 #include <sys/time.h> | 10 #include <sys/time.h> |
| (...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 // Prioritize data events over close and error events. | 236 // Prioritize data events over close and error events. |
| 237 if ((pollfd->revents & POLLIN) != 0) { | 237 if ((pollfd->revents & POLLIN) != 0) { |
| 238 if (FDUtils::AvailableBytes(pollfd->fd) != 0) { | 238 if (FDUtils::AvailableBytes(pollfd->fd) != 0) { |
| 239 event_mask = (1 << kInEvent); | 239 event_mask = (1 << kInEvent); |
| 240 } else if (((pollfd->revents & POLLHUP) != 0)) { | 240 } else if (((pollfd->revents & POLLHUP) != 0)) { |
| 241 event_mask = (1 << kCloseEvent); | 241 event_mask = (1 << kCloseEvent); |
| 242 sd->MarkClosedRead(); | 242 sd->MarkClosedRead(); |
| 243 } else if ((pollfd->revents & POLLERR) != 0) { | 243 } else if ((pollfd->revents & POLLERR) != 0) { |
| 244 event_mask = (1 << kErrorEvent); | 244 event_mask = (1 << kErrorEvent); |
| 245 } else { | 245 } else { |
| 246 // If POLLIN is set with no available data and no POLLHUP use | 246 if (sd->IsPipe()) { |
| 247 // recv to peek for whether the other end of the socket | 247 // For stdin when reading from a terminal treat POLLIN with 0 |
| 248 // actually closed. | 248 // available bytes as end-of-file. |
| 249 char buffer; | 249 if (sd->fd() == STDIN_FILENO && isatty(sd->fd())) { |
| 250 ssize_t bytesPeeked = recv(sd->fd(), &buffer, 1, MSG_PEEK); | 250 event_mask = (1 << kCloseEvent); |
| 251 if (bytesPeeked == 0) { | 251 sd->MarkClosedRead(); |
| 252 event_mask = (1 << kCloseEvent); | 252 } |
| 253 sd->MarkClosedRead(); | 253 } else { |
| 254 } else if (errno != EAGAIN) { | 254 // If POLLIN is set with no available data and no POLLHUP use |
| 255 fprintf(stderr, "Error recv: %s\n", strerror(errno)); | 255 // recv to peek for whether the other end of the socket |
| 256 // actually closed. |
| 257 char buffer; |
| 258 ssize_t bytesPeeked = recv(sd->fd(), &buffer, 1, MSG_PEEK); |
| 259 if (bytesPeeked == 0) { |
| 260 event_mask = (1 << kCloseEvent); |
| 261 sd->MarkClosedRead(); |
| 262 } else if (errno != EAGAIN) { |
| 263 fprintf(stderr, "Error recv: %s\n", strerror(errno)); |
| 264 } |
| 256 } | 265 } |
| 257 } | 266 } |
| 258 } | 267 } |
| 259 | 268 |
| 260 // On pipes POLLHUP is reported without POLLIN. | 269 // On pipes POLLHUP is reported without POLLIN. |
| 261 if (((pollfd->revents & POLLIN) == 0) && | 270 if (sd->IsPipe()) { |
| 262 ((pollfd->revents & POLLHUP) != 0)) { | 271 if (((pollfd->revents & POLLIN) == 0) && |
| 263 event_mask = (1 << kCloseEvent); | 272 ((pollfd->revents & POLLHUP) != 0)) { |
| 264 sd->MarkClosedRead(); | 273 event_mask = (1 << kCloseEvent); |
| 274 sd->MarkClosedRead(); |
| 275 } |
| 265 } | 276 } |
| 266 | 277 |
| 267 if ((pollfd->revents & POLLOUT) != 0) event_mask |= (1 << kOutEvent); | 278 if ((pollfd->revents & POLLOUT) != 0) event_mask |= (1 << kOutEvent); |
| 268 } | 279 } |
| 269 | 280 |
| 270 return event_mask; | 281 return event_mask; |
| 271 } | 282 } |
| 272 | 283 |
| 273 | 284 |
| 274 void EventHandlerImplementation::HandleEvents(struct pollfd* pollfds, | 285 void EventHandlerImplementation::HandleEvents(struct pollfd* pollfds, |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 FATAL("Create start event handler thread"); | 361 FATAL("Create start event handler thread"); |
| 351 } | 362 } |
| 352 } | 363 } |
| 353 | 364 |
| 354 | 365 |
| 355 void EventHandlerImplementation::SendData(intptr_t id, | 366 void EventHandlerImplementation::SendData(intptr_t id, |
| 356 Dart_Port dart_port, | 367 Dart_Port dart_port, |
| 357 intptr_t data) { | 368 intptr_t data) { |
| 358 WakeupHandler(id, dart_port, data); | 369 WakeupHandler(id, dart_port, data); |
| 359 } | 370 } |
| OLD | NEW |