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