Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -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 | |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 5 | #include "shill/glib_io_input_handler.h" |
Paul Stewart | 26b327e | 2011-10-19 11:38:09 -0700 | [diff] [blame] | 6 | |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 7 | #include <stdio.h> |
| 8 | #include <glib.h> |
| 9 | |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 10 | #include "shill/logging.h" |
| 11 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 12 | using base::Callback; |
| 13 | |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 14 | namespace shill { |
| 15 | |
| 16 | static gboolean DispatchIOHandler(GIOChannel *chan, |
| 17 | GIOCondition cond, |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 18 | gpointer data) { |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 19 | GlibIOInputHandler *handler = reinterpret_cast<GlibIOInputHandler *>(data); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 20 | unsigned char buf[4096]; |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 21 | gsize len = 0; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 22 | gboolean ret = TRUE; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 23 | |
| 24 | if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) |
| 25 | return FALSE; |
| 26 | |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 27 | GIOStatus status = g_io_channel_read_chars( |
| 28 | chan, reinterpret_cast<gchar *>(buf), sizeof(buf), &len, NULL); |
| 29 | if (status != G_IO_STATUS_NORMAL) { |
| 30 | if (status == G_IO_STATUS_AGAIN) |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 31 | return TRUE; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 32 | len = 0; |
| 33 | ret = FALSE; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 34 | } |
| 35 | |
Darin Petkov | 633ac6f | 2011-07-08 13:56:13 -0700 | [diff] [blame] | 36 | InputData input_data(buf, len); |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 37 | handler->callback().Run(&input_data); |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 38 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 39 | return ret; |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 42 | GlibIOInputHandler::GlibIOInputHandler( |
| 43 | int fd, const Callback<void(InputData *)> &callback) |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 44 | : channel_(g_io_channel_unix_new(fd)), |
| 45 | callback_(callback), |
| 46 | source_id_(G_MAXUINT) { |
Ben Chan | b6a7553 | 2012-09-07 20:30:49 -0700 | [diff] [blame] | 47 | // To avoid blocking in g_io_channel_read_chars() due to its internal buffer, |
| 48 | // set the channel to unbuffered, which in turns requires encoding to be NULL. |
| 49 | // This assumes raw binary data are read from |fd| via the channel. |
| 50 | CHECK_EQ(G_IO_STATUS_NORMAL, g_io_channel_set_encoding(channel_, NULL, NULL)); |
| 51 | g_io_channel_set_buffered(channel_, FALSE); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 52 | g_io_channel_set_close_on_unref(channel_, TRUE); |
| 53 | } |
| 54 | |
| 55 | GlibIOInputHandler::~GlibIOInputHandler() { |
| 56 | g_source_remove(source_id_); |
| 57 | g_io_channel_shutdown(channel_, TRUE, NULL); |
| 58 | g_io_channel_unref(channel_); |
| 59 | } |
| 60 | |
| 61 | void GlibIOInputHandler::Start() { |
| 62 | if (source_id_ == G_MAXUINT) { |
| 63 | source_id_ = g_io_add_watch(channel_, |
| 64 | static_cast<GIOCondition>( |
| 65 | G_IO_IN | G_IO_NVAL | G_IO_HUP | G_IO_ERR), |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 66 | DispatchIOHandler, this); |
Paul Stewart | f0aae10 | 2011-10-19 12:11:44 -0700 | [diff] [blame] | 67 | } |
| 68 | } |
| 69 | |
| 70 | void GlibIOInputHandler::Stop() { |
| 71 | if (source_id_ != G_MAXUINT) { |
| 72 | g_source_remove(source_id_); |
| 73 | source_id_ = G_MAXUINT; |
| 74 | } |
| 75 | } |
| 76 | |
Paul Stewart | 25379f1 | 2011-05-26 06:41:38 -0700 | [diff] [blame] | 77 | } // namespace shill |