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 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 7 | #include <glib.h> |
| 8 | #include <stdio.h> |
| 9 | #include <sys/socket.h> |
| 10 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 11 | #include "shill/logging.h" |
| 12 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 13 | using base::Callback; |
| 14 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 15 | namespace shill { |
| 16 | |
| 17 | static gboolean DispatchIOHandler(GIOChannel *chan, |
| 18 | GIOCondition cond, |
| 19 | gpointer data) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 20 | GlibIOReadyHandler *handler = reinterpret_cast<GlibIOReadyHandler *>(data); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 21 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 22 | handler->callback().Run(g_io_channel_unix_get_fd(chan)); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 23 | |
| 24 | if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) |
| 25 | return FALSE; |
| 26 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 27 | return TRUE; |
| 28 | } |
| 29 | |
| 30 | GlibIOReadyHandler::GlibIOReadyHandler(int fd, |
| 31 | IOHandler::ReadyMode mode, |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 32 | const Callback<void(int)> &callback) |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 33 | : channel_(NULL), |
| 34 | callback_(callback), |
| 35 | source_id_(G_MAXUINT) { |
| 36 | if (mode == kModeInput) { |
| 37 | condition_ = static_cast<GIOCondition>( |
| 38 | G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR); |
| 39 | } else if (mode == kModeOutput) { |
| 40 | condition_ = static_cast<GIOCondition>( |
| 41 | G_IO_OUT | G_IO_NVAL | G_IO_HUP | G_IO_ERR); |
| 42 | } else { |
| 43 | NOTREACHED() << "Unknown IO ready mode: " << mode; |
| 44 | } |
| 45 | |
| 46 | channel_ = g_io_channel_unix_new(fd); |
| 47 | g_io_channel_set_close_on_unref(channel_, FALSE); |
| 48 | } |
| 49 | |
| 50 | GlibIOReadyHandler::~GlibIOReadyHandler() { |
Darin Petkov | 78f6326 | 2012-03-26 01:30:24 +0200 | [diff] [blame] | 51 | GlibIOReadyHandler::Stop(); |
Paul Stewart | c2350ee | 2011-10-19 12:28:40 -0700 | [diff] [blame] | 52 | // 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] | 53 | g_io_channel_unref(channel_); |
| 54 | } |
| 55 | |
| 56 | void GlibIOReadyHandler::Start() { |
| 57 | if (source_id_ == G_MAXUINT) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 58 | source_id_ = g_io_add_watch(channel_, condition_, DispatchIOHandler, this); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | void GlibIOReadyHandler::Stop() { |
| 63 | if (source_id_ != G_MAXUINT) { |
| 64 | g_source_remove(source_id_); |
| 65 | source_id_ = G_MAXUINT; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | } // namespace shill |