OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef BASE_MESSAGE_PUMP_LIBEVENT_H_ |
| 6 #define BASE_MESSAGE_PUMP_LIBEVENT_H_ |
| 7 |
| 8 #include "base/message_pump.h" |
| 9 #include "base/time.h" |
| 10 |
| 11 // Declare structs we need from libevent.h rather than including it |
| 12 struct event_base; |
| 13 struct event; |
| 14 |
| 15 namespace base { |
| 16 |
| 17 // 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 class MessagePumpLibevent : public MessagePump { |
| 20 public: |
| 21 // Used with WatchObject to asynchronously monitor the I/O readiness of a |
| 22 // socket. |
| 23 class Watcher { |
| 24 public: |
| 25 virtual ~Watcher() {} |
| 26 // Called from MessageLoop::Run when a ready socket is detected. |
| 27 virtual void OnSocketReady(short eventmask) = 0; |
| 28 }; |
| 29 |
| 30 MessagePumpLibevent(); |
| 31 virtual ~MessagePumpLibevent(); |
| 32 |
| 33 // Have the current thread's message loop watch for a ready socket. |
| 34 // Caller must provide a struct event for this socket for libevent's use. |
| 35 // The event and interest_mask fields are defined in libevent. |
| 36 // Returns true on success. |
| 37 // TODO(dkegel): hide libevent better; abstraction still too leaky |
| 38 // TODO(dkegel): better error handing |
| 39 // TODO(dkegel): switch to edge-triggered readiness notification |
| 40 void WatchSocket(int socket, short interest_mask, event* e, Watcher*); |
| 41 |
| 42 // Stop watching a socket. |
| 43 // Event was previously initialized by WatchSocket. |
| 44 void UnwatchSocket(event* e); |
| 45 |
| 46 // MessagePump methods: |
| 47 virtual void Run(Delegate* delegate); |
| 48 virtual void Quit(); |
| 49 virtual void ScheduleWork(); |
| 50 virtual void ScheduleDelayedWork(const Time& delayed_work_time); |
| 51 |
| 52 private: |
| 53 |
| 54 // Risky part of constructor. Returns true on success. |
| 55 bool Init(); |
| 56 |
| 57 // This flag is set to false when Run should return. |
| 58 bool keep_running_; |
| 59 |
| 60 // This flag is set when inside Run. |
| 61 bool in_run_; |
| 62 |
| 63 // The time at which we should call DoDelayedWork. |
| 64 Time delayed_work_time_; |
| 65 |
| 66 // Libevent dispatcher. Watches all sockets registered with it, and sends |
| 67 // readiness callbacks when a socket is ready for I/O. |
| 68 event_base* event_base_; |
| 69 |
| 70 // Called by libevent to tell us a registered socket is ready |
| 71 static void OnReadinessNotification(int socket, short flags, void* context); |
| 72 |
| 73 // Unix pipe used to implement ScheduleWork() |
| 74 // ... callback; called by libevent inside Run() when pipe is ready to read |
| 75 static void OnWakeup(int socket, short flags, void* context); |
| 76 // ... write end; ScheduleWork() writes a single byte to it |
| 77 int wakeup_pipe_in_; |
| 78 // ... read end; OnWakeup reads it and then breaks Run() out of its sleep |
| 79 int wakeup_pipe_out_; |
| 80 // ... libevent wrapper for read end |
| 81 event* wakeup_event_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(MessagePumpLibevent); |
| 84 }; |
| 85 |
| 86 } // namespace base |
| 87 |
| 88 #endif // BASE_MESSAGE_PUMP_LIBEVENT_H_ |
| 89 |
OLD | NEW |