Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/glib_io_ready_handler.h" |
| 6 | |
| 7 | #include <base/logging.h> |
| 8 | #include <glib.h> |
| 9 | #include <stdio.h> |
| 10 | #include <sys/socket.h> |
| 11 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | using base::Callback; |
| 13 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 14 | namespace shill { |
| 15 | |
| 16 | static gboolean DispatchIOHandler(GIOChannel *chan, |
| 17 | GIOCondition cond, |
| 18 | gpointer data) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 19 | GlibIOReadyHandler *handler = reinterpret_cast<GlibIOReadyHandler *>(data); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 20 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 21 | handler->callback().Run(g_io_channel_unix_get_fd(chan)); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 22 | |
| 23 | if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) |
| 24 | return FALSE; |
| 25 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 26 | return TRUE; |
| 27 | } |
| 28 | |
| 29 | GlibIOReadyHandler::GlibIOReadyHandler(int fd, |
| 30 | IOHandler::ReadyMode mode, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 31 | const Callback<void(int)> &callback) |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 32 | : channel_(NULL), |
| 33 | callback_(callback), |
| 34 | source_id_(G_MAXUINT) { |
| 35 | if (mode == kModeInput) { |
| 36 | condition_ = static_cast<GIOCondition>( |
| 37 | G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR); |
| 38 | } else if (mode == kModeOutput) { |
| 39 | condition_ = static_cast<GIOCondition>( |
| 40 | G_IO_OUT | G_IO_NVAL | G_IO_HUP | G_IO_ERR); |
| 41 | } else { |
| 42 | NOTREACHED() << "Unknown IO ready mode: " << mode; |
| 43 | } |
| 44 | |
| 45 | channel_ = g_io_channel_unix_new(fd); |
| 46 | g_io_channel_set_close_on_unref(channel_, FALSE); |
| 47 | } |
| 48 | |
| 49 | GlibIOReadyHandler::~GlibIOReadyHandler() { |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 50 | GlibIOReadyHandler::Stop(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 51 | // NB: We don't shut down the channel since we don't own it |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 52 | g_io_channel_unref(channel_); |
| 53 | } |
| 54 | |
| 55 | void GlibIOReadyHandler::Start() { |
| 56 | if (source_id_ == G_MAXUINT) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 57 | source_id_ = g_io_add_watch(channel_, condition_, DispatchIOHandler, this); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | void GlibIOReadyHandler::Stop() { |
| 62 | if (source_id_ != G_MAXUINT) { |
| 63 | g_source_remove(source_id_); |
| 64 | source_id_ = G_MAXUINT; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | } // namespace shill |