| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/message_pump_libevent.h" | 5 #include "base/message_pump_libevent.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <unistd.h> | 9 #include <unistd.h> |
| 10 | 10 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 // Return 0 on success | 51 // Return 0 on success |
| 52 // Too small a function to bother putting in a library? | 52 // Too small a function to bother putting in a library? |
| 53 static int SetNonBlocking(int fd) { | 53 static int SetNonBlocking(int fd) { |
| 54 int flags = fcntl(fd, F_GETFL, 0); | 54 int flags = fcntl(fd, F_GETFL, 0); |
| 55 if (flags == -1) | 55 if (flags == -1) |
| 56 flags = 0; | 56 flags = 0; |
| 57 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); | 57 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 58 } | 58 } |
| 59 | 59 |
| 60 MessagePumpLibevent::FileDescriptorWatcher::FileDescriptorWatcher() | 60 MessagePumpLibevent::FileDescriptorWatcher::FileDescriptorWatcher() |
| 61 : is_persistent_(false), | 61 : event_(NULL), |
| 62 event_(NULL), | |
| 63 pump_(NULL), | 62 pump_(NULL), |
| 64 watcher_(NULL), | 63 watcher_(NULL), |
| 65 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { | 64 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) { |
| 66 } | 65 } |
| 67 | 66 |
| 68 MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() { | 67 MessagePumpLibevent::FileDescriptorWatcher::~FileDescriptorWatcher() { |
| 69 if (event_) { | 68 if (event_) { |
| 70 StopWatchingFileDescriptor(); | 69 StopWatchingFileDescriptor(); |
| 71 } | 70 } |
| 72 } | 71 } |
| 73 | 72 |
| 74 bool MessagePumpLibevent::FileDescriptorWatcher::StopWatchingFileDescriptor() { | 73 bool MessagePumpLibevent::FileDescriptorWatcher::StopWatchingFileDescriptor() { |
| 75 event* e = ReleaseEvent(); | 74 event* e = ReleaseEvent(); |
| 76 if (e == NULL) | 75 if (e == NULL) |
| 77 return true; | 76 return true; |
| 78 | 77 |
| 79 // event_del() is a no-op if the event isn't active. | 78 // event_del() is a no-op if the event isn't active. |
| 80 int rv = event_del(e); | 79 int rv = event_del(e); |
| 81 delete e; | 80 delete e; |
| 82 pump_ = NULL; | 81 pump_ = NULL; |
| 83 watcher_ = NULL; | 82 watcher_ = NULL; |
| 84 return (rv == 0); | 83 return (rv == 0); |
| 85 } | 84 } |
| 86 | 85 |
| 87 void MessagePumpLibevent::FileDescriptorWatcher::Init(event *e, | 86 void MessagePumpLibevent::FileDescriptorWatcher::Init(event *e) { |
| 88 bool is_persistent) { | |
| 89 DCHECK(e); | 87 DCHECK(e); |
| 90 DCHECK(!event_); | 88 DCHECK(!event_); |
| 91 | 89 |
| 92 is_persistent_ = is_persistent; | |
| 93 event_ = e; | 90 event_ = e; |
| 94 } | 91 } |
| 95 | 92 |
| 96 event *MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() { | 93 event *MessagePumpLibevent::FileDescriptorWatcher::ReleaseEvent() { |
| 97 struct event *e = event_; | 94 struct event *e = event_; |
| 98 event_ = NULL; | 95 event_ = NULL; |
| 99 return e; | 96 return e; |
| 100 } | 97 } |
| 101 | 98 |
| 102 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( | 99 void MessagePumpLibevent::FileDescriptorWatcher::OnFileCanReadWithoutBlocking( |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 if (event_base_set(event_base_, evt.get()) != 0) { | 193 if (event_base_set(event_base_, evt.get()) != 0) { |
| 197 return false; | 194 return false; |
| 198 } | 195 } |
| 199 | 196 |
| 200 // Add this socket to the list of monitored sockets. | 197 // Add this socket to the list of monitored sockets. |
| 201 if (event_add(evt.get(), NULL) != 0) { | 198 if (event_add(evt.get(), NULL) != 0) { |
| 202 return false; | 199 return false; |
| 203 } | 200 } |
| 204 | 201 |
| 205 // Transfer ownership of evt to controller. | 202 // Transfer ownership of evt to controller. |
| 206 controller->Init(evt.release(), persistent); | 203 controller->Init(evt.release()); |
| 207 | 204 |
| 208 controller->set_watcher(delegate); | 205 controller->set_watcher(delegate); |
| 209 controller->set_pump(this); | 206 controller->set_pump(this); |
| 210 | 207 |
| 211 return true; | 208 return true; |
| 212 } | 209 } |
| 213 | 210 |
| 214 void MessagePumpLibevent::AddIOObserver(IOObserver *obs) { | 211 void MessagePumpLibevent::AddIOObserver(IOObserver *obs) { |
| 215 io_observers_.AddObserver(obs); | 212 io_observers_.AddObserver(obs); |
| 216 } | 213 } |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 377 // Remove and discard the wakeup byte. | 374 // Remove and discard the wakeup byte. |
| 378 char buf; | 375 char buf; |
| 379 int nread = HANDLE_EINTR(read(socket, &buf, 1)); | 376 int nread = HANDLE_EINTR(read(socket, &buf, 1)); |
| 380 DCHECK_EQ(nread, 1); | 377 DCHECK_EQ(nread, 1); |
| 381 that->processed_io_events_ = true; | 378 that->processed_io_events_ = true; |
| 382 // Tell libevent to break out of inner loop. | 379 // Tell libevent to break out of inner loop. |
| 383 event_base_loopbreak(that->event_base_); | 380 event_base_loopbreak(that->event_base_); |
| 384 } | 381 } |
| 385 | 382 |
| 386 } // namespace base | 383 } // namespace base |
| OLD | NEW |