Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_ | 5 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_ |
| 6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_ | 6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_ |
| 7 | 7 |
| 8 #include "base/message_pump.h" | 8 #include "base/message_pump.h" |
| 9 #include "base/scoped_ptr.h" | |
| 9 #include "base/time.h" | 10 #include "base/time.h" |
| 10 | 11 |
| 11 // Declare structs we need from libevent.h rather than including it | 12 // Declare structs we need from libevent.h rather than including it |
| 12 struct event_base; | 13 struct event_base; |
| 13 struct event; | 14 struct event; |
| 14 | 15 |
| 15 namespace base { | 16 namespace base { |
| 16 | 17 |
| 17 // Class to monitor sockets and issue callbacks when sockets are ready for I/O | 18 // Class to monitor sockets and issue callbacks when sockets are ready for I/O |
| 18 // TODO(dkegel): add support for background file IO somehow | 19 // TODO(dkegel): add support for background file IO somehow |
| 19 class MessagePumpLibevent : public MessagePump { | 20 class MessagePumpLibevent : public MessagePump { |
| 20 public: | 21 public: |
| 21 // Used with WatchSocket to asynchronously monitor the I/O readiness of a | 22 |
| 22 // socket. | 23 // Object returned by WatchFileDescriptor to manage further watching. |
| 24 class FileDescriptorWatcher { | |
| 25 public: | |
| 26 FileDescriptorWatcher(); | |
| 27 ~FileDescriptorWatcher(); // Implicitly calls StopWatching. | |
|
Mark Mentovai
2008/12/15 21:43:40
Implicitly calls StopWatchingFileDescriptor?
| |
| 28 | |
| 29 // NOTE: These methods aren't called StartWatching()/StopWatching() to | |
| 30 // avoid confusion with the win32 ObjectWatcher class. | |
| 31 | |
| 32 // Stop watching the FD, always safe to call. No-op if there's nothing | |
| 33 // to do. | |
| 34 bool StopWatchingFileDescriptor(); | |
| 35 | |
| 36 private: | |
| 37 // Called by MessagePumpLibevent, ownership of |e| is transferred to this | |
| 38 // object. | |
| 39 void Init(event* e, bool is_persistent); | |
| 40 | |
| 41 // Used by MessagePumpLibevent to take ownership of event_. | |
| 42 event *ReleaseEvent(); | |
| 43 friend class MessagePumpLibevent; | |
| 44 | |
| 45 private: | |
| 46 bool is_persistent_; // false if this event is one-shot. | |
| 47 scoped_ptr<event> event_; | |
| 48 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); | |
| 49 }; | |
| 50 | |
| 51 // Used with WatchFileDescptor to asynchronously monitor the I/O readiness of | |
| 52 // a File Descriptor. | |
| 23 class Watcher { | 53 class Watcher { |
| 24 public: | 54 public: |
| 25 virtual ~Watcher() {} | 55 virtual ~Watcher() {} |
| 26 // Called from MessageLoop::Run when a ready socket is detected. | 56 // Called from MessageLoop::Run when an FD can be read from/written to |
| 27 virtual void OnSocketReady(short eventmask) = 0; | 57 // without blocking |
| 28 }; | 58 virtual void OnFileCanReadWithoutBlocking(int fd) = 0; |
| 29 | 59 virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; |
| 30 // Used with WatchFileHandle to monitor I/O readiness for a File Handle. | |
| 31 class FileWatcher { | |
| 32 public: | |
| 33 virtual ~FileWatcher() {} | |
| 34 // Called from MessageLoop::Run when a non-blocking read/write can be made. | |
| 35 virtual void OnFileReadReady(int fd) = 0; | |
| 36 virtual void OnFileWriteReady(int fd) = 0; | |
| 37 }; | 60 }; |
| 38 | 61 |
| 39 MessagePumpLibevent(); | 62 MessagePumpLibevent(); |
| 40 virtual ~MessagePumpLibevent(); | 63 virtual ~MessagePumpLibevent(); |
| 41 | 64 |
| 42 // Have the current thread's message loop watch for a ready socket. | 65 enum Mode { |
| 43 // Caller must provide a struct event for this socket for libevent's use. | 66 WATCH_READ = 1 << 0, |
| 44 // The event and interest_mask fields are defined in libevent. | 67 WATCH_WRITE = 1 << 1, |
| 68 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE | |
| 69 }; | |
| 70 | |
| 71 // Have the current thread's message loop watch for a a situation in which | |
| 72 // reading/writing to the FD can be performed without Blocking. | |
| 73 // Callers must provide a preallocated FileDescriptorWatcher object which | |
| 74 // can later be used to manage the Lifetime of this event. | |
| 75 // If a FileDescriptorWatcher is passed in which is already attached to | |
| 76 // an event, then the effect is cumulative i.e. after the call |controller| | |
| 77 // will watch both the previous event and the new one. | |
| 78 // If an error occurs while calling this method in a cumulative fashion, the | |
| 79 // event previously attached to |controller| is aborted. | |
| 45 // Returns true on success. | 80 // Returns true on success. |
| 46 // TODO(dkegel): hide libevent better; abstraction still too leaky | |
| 47 // TODO(dkegel): better error handing | |
| 48 // TODO(dkegel): switch to edge-triggered readiness notification | 81 // TODO(dkegel): switch to edge-triggered readiness notification |
| 49 void WatchSocket(int socket, short interest_mask, event* e, Watcher*); | 82 bool WatchFileDescriptor(int fd, |
| 50 | 83 bool persistent, |
| 51 // TODO(playmobil): Merge this with WatchSocket(). | 84 Mode mode, |
| 52 void WatchFileHandle(int fd, short interest_mask, event* e, FileWatcher*); | 85 FileDescriptorWatcher *controller, |
| 53 | 86 Watcher *delegate); |
| 54 // Stop watching a socket. | |
| 55 // Event was previously initialized by WatchSocket. | |
| 56 void UnwatchSocket(event* e); | |
| 57 | |
| 58 // Stop watching a File Handle. | |
| 59 // Event was previously initialized by WatchFileHandle. | |
| 60 void UnwatchFileHandle(event* e); | |
| 61 | 87 |
| 62 // MessagePump methods: | 88 // MessagePump methods: |
| 63 virtual void Run(Delegate* delegate); | 89 virtual void Run(Delegate* delegate); |
| 64 virtual void Quit(); | 90 virtual void Quit(); |
| 65 virtual void ScheduleWork(); | 91 virtual void ScheduleWork(); |
| 66 virtual void ScheduleDelayedWork(const Time& delayed_work_time); | 92 virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
| 67 | 93 |
| 68 private: | 94 private: |
| 69 | 95 |
| 70 // Risky part of constructor. Returns true on success. | 96 // Risky part of constructor. Returns true on success. |
| 71 bool Init(); | 97 bool Init(); |
| 72 | 98 |
| 73 // This flag is set to false when Run should return. | 99 // This flag is set to false when Run should return. |
| 74 bool keep_running_; | 100 bool keep_running_; |
| 75 | 101 |
| 76 // This flag is set when inside Run. | 102 // This flag is set when inside Run. |
| 77 bool in_run_; | 103 bool in_run_; |
| 78 | 104 |
| 79 // The time at which we should call DoDelayedWork. | 105 // The time at which we should call DoDelayedWork. |
| 80 Time delayed_work_time_; | 106 Time delayed_work_time_; |
| 81 | 107 |
| 82 // Libevent dispatcher. Watches all sockets registered with it, and sends | 108 // Libevent dispatcher. Watches all sockets registered with it, and sends |
| 83 // readiness callbacks when a socket is ready for I/O. | 109 // readiness callbacks when a socket is ready for I/O. |
| 84 event_base* event_base_; | 110 event_base* event_base_; |
| 85 | 111 |
| 86 // Called by libevent to tell us a registered socket is ready | 112 // Called by libevent to tell us a registered FD can be read/written to. |
| 87 static void OnReadinessNotification(int socket, short flags, void* context); | 113 static void OnLibeventNotification(int fd, short flags, |
| 88 | 114 void* context); |
| 89 // Called by libevent to tell us a registered fd is ready. | |
| 90 static void OnFileReadReadinessNotification(int fd, short flags, | |
| 91 void* context); | |
| 92 static void OnFileWriteReadinessNotification(int fd, short flags, | |
| 93 void* context); | |
| 94 | 115 |
| 95 // Unix pipe used to implement ScheduleWork() | 116 // Unix pipe used to implement ScheduleWork() |
| 96 // ... callback; called by libevent inside Run() when pipe is ready to read | 117 // ... callback; called by libevent inside Run() when pipe is ready to read |
| 97 static void OnWakeup(int socket, short flags, void* context); | 118 static void OnWakeup(int socket, short flags, void* context); |
| 98 // ... write end; ScheduleWork() writes a single byte to it | 119 // ... write end; ScheduleWork() writes a single byte to it |
| 99 int wakeup_pipe_in_; | 120 int wakeup_pipe_in_; |
| 100 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep | 121 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep |
| 101 int wakeup_pipe_out_; | 122 int wakeup_pipe_out_; |
| 102 // ... libevent wrapper for read end | 123 // ... libevent wrapper for read end |
| 103 event* wakeup_event_; | 124 event* wakeup_event_; |
| 104 | 125 |
| 105 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); | 126 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); |
| 106 }; | 127 }; |
| 107 | 128 |
| 108 } // namespace base | 129 } // namespace base |
| 109 | 130 |
| 110 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_ | 131 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_ |
| OLD | NEW |