~ubuntu-branches/ubuntu/maverick/libtorrent-rasterbar/maverick

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/detail/pipe_select_interrupter.hpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Sauthier
  • Date: 2010-08-10 12:59:37 UTC
  • mfrom: (1.3.7 upstream)
  • Revision ID: james.westby@ubuntu.com-20100810125937-jbcmmf17y8yo9hgz
Tags: 0.15.0-0ubuntu1
* New upstream version.
* debian/patches/100_fix_html_docs.patch: refreshed.
* debian/control: bump up standards-version to 3.9.1 (no changes).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
//
2
 
// pipe_select_interrupter.hpp
3
 
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~
4
 
//
5
 
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6
 
//
7
 
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
 
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9
 
//
10
 
 
11
 
#ifndef ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
12
 
#define ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP
13
 
 
14
 
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
15
 
# pragma once
16
 
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
 
 
18
 
#include "asio/detail/push_options.hpp"
19
 
 
20
 
#include "asio/detail/push_options.hpp"
21
 
#include <boost/config.hpp>
22
 
#include <boost/throw_exception.hpp>
23
 
#include "asio/detail/pop_options.hpp"
24
 
 
25
 
#if !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
26
 
 
27
 
#include "asio/detail/push_options.hpp"
28
 
#include <fcntl.h>
29
 
#include "asio/detail/pop_options.hpp"
30
 
 
31
 
#include "asio/error.hpp"
32
 
#include "asio/system_error.hpp"
33
 
#include "asio/detail/socket_types.hpp"
34
 
 
35
 
namespace asio {
36
 
namespace detail {
37
 
 
38
 
class pipe_select_interrupter
39
 
{
40
 
public:
41
 
  // Constructor.
42
 
  pipe_select_interrupter()
43
 
  {
44
 
    int pipe_fds[2];
45
 
    if (pipe(pipe_fds) == 0)
46
 
    {
47
 
      read_descriptor_ = pipe_fds[0];
48
 
      ::fcntl(read_descriptor_, F_SETFL, O_NONBLOCK);
49
 
      write_descriptor_ = pipe_fds[1];
50
 
      ::fcntl(write_descriptor_, F_SETFL, O_NONBLOCK);
51
 
    }
52
 
    else
53
 
    {
54
 
      asio::error_code ec(errno,
55
 
          asio::error::get_system_category());
56
 
      asio::system_error e(ec, "pipe_select_interrupter");
57
 
      boost::throw_exception(e);
58
 
    }
59
 
  }
60
 
 
61
 
  // Destructor.
62
 
  ~pipe_select_interrupter()
63
 
  {
64
 
    if (read_descriptor_ != -1)
65
 
      ::close(read_descriptor_);
66
 
    if (write_descriptor_ != -1)
67
 
      ::close(write_descriptor_);
68
 
  }
69
 
 
70
 
  // Interrupt the select call.
71
 
  void interrupt()
72
 
  {
73
 
    char byte = 0;
74
 
    ::write(write_descriptor_, &byte, 1);
75
 
  }
76
 
 
77
 
  // Reset the select interrupt. Returns true if the call was interrupted.
78
 
  bool reset()
79
 
  {
80
 
    char data[1024];
81
 
    int bytes_read = ::read(read_descriptor_, data, sizeof(data));
82
 
    bool was_interrupted = (bytes_read > 0);
83
 
    while (bytes_read == sizeof(data))
84
 
      bytes_read = ::read(read_descriptor_, data, sizeof(data));
85
 
    return was_interrupted;
86
 
  }
87
 
 
88
 
  // Get the read descriptor to be passed to select.
89
 
  int read_descriptor() const
90
 
  {
91
 
    return read_descriptor_;
92
 
  }
93
 
 
94
 
private:
95
 
  // The read end of a connection used to interrupt the select call. This file
96
 
  // descriptor is passed to select such that when it is time to stop, a single
97
 
  // byte will be written on the other end of the connection and this
98
 
  // descriptor will become readable.
99
 
  int read_descriptor_;
100
 
 
101
 
  // The write end of a connection used to interrupt the select call. A single
102
 
  // byte may be written to this to wake up the select which is waiting for the
103
 
  // other end to become readable.
104
 
  int write_descriptor_;
105
 
};
106
 
 
107
 
} // namespace detail
108
 
} // namespace asio
109
 
 
110
 
#endif // !defined(BOOST_WINDOWS) && !defined(__CYGWIN__)
111
 
 
112
 
#include "asio/detail/pop_options.hpp"
113
 
 
114
 
#endif // ASIO_DETAIL_PIPE_SELECT_INTERRUPTER_HPP