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

« back to all changes in this revision

Viewing changes to include/libtorrent/asio/impl/write.ipp

  • Committer: Bazaar Package Importer
  • Author(s): Cristian Greco
  • Date: 2008-07-02 10:46:21 UTC
  • Revision ID: james.westby@ubuntu.com-20080702104621-jzx3pfke9lkcxfxn
Tags: upstream-0.13.1
ImportĀ upstreamĀ versionĀ 0.13.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// write.ipp
 
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_WRITE_IPP
 
12
#define ASIO_WRITE_IPP
 
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/buffer.hpp"
 
21
#include "asio/completion_condition.hpp"
 
22
#include "asio/detail/bind_handler.hpp"
 
23
#include "asio/detail/consuming_buffers.hpp"
 
24
#include "asio/detail/handler_alloc_helpers.hpp"
 
25
#include "asio/detail/handler_invoke_helpers.hpp"
 
26
#include "asio/detail/throw_error.hpp"
 
27
 
 
28
namespace asio {
 
29
 
 
30
template <typename SyncWriteStream, typename ConstBufferSequence,
 
31
    typename CompletionCondition>
 
32
std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
 
33
    CompletionCondition completion_condition, asio::error_code& ec)
 
34
{
 
35
  asio::detail::consuming_buffers<
 
36
    const_buffer, ConstBufferSequence> tmp(buffers);
 
37
  std::size_t total_transferred = 0;
 
38
  while (tmp.begin() != tmp.end())
 
39
  {
 
40
    std::size_t bytes_transferred = s.write_some(tmp, ec);
 
41
    tmp.consume(bytes_transferred);
 
42
    total_transferred += bytes_transferred;
 
43
    if (completion_condition(ec, total_transferred))
 
44
      return total_transferred;
 
45
  }
 
46
  ec = asio::error_code();
 
47
  return total_transferred;
 
48
}
 
49
 
 
50
template <typename SyncWriteStream, typename ConstBufferSequence>
 
51
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers)
 
52
{
 
53
  asio::error_code ec;
 
54
  std::size_t bytes_transferred = write(s, buffers, transfer_all(), ec);
 
55
  asio::detail::throw_error(ec);
 
56
  return bytes_transferred;
 
57
}
 
58
 
 
59
template <typename SyncWriteStream, typename ConstBufferSequence,
 
60
    typename CompletionCondition>
 
61
inline std::size_t write(SyncWriteStream& s, const ConstBufferSequence& buffers,
 
62
    CompletionCondition completion_condition)
 
63
{
 
64
  asio::error_code ec;
 
65
  std::size_t bytes_transferred = write(s, buffers, completion_condition, ec);
 
66
  asio::detail::throw_error(ec);
 
67
  return bytes_transferred;
 
68
}
 
69
 
 
70
template <typename SyncWriteStream, typename Allocator,
 
71
    typename CompletionCondition>
 
72
std::size_t write(SyncWriteStream& s,
 
73
    asio::basic_streambuf<Allocator>& b,
 
74
    CompletionCondition completion_condition, asio::error_code& ec)
 
75
{
 
76
  std::size_t bytes_transferred = write(s, b.data(), completion_condition, ec);
 
77
  b.consume(bytes_transferred);
 
78
  return bytes_transferred;
 
79
}
 
80
 
 
81
template <typename SyncWriteStream, typename Allocator>
 
82
inline std::size_t write(SyncWriteStream& s,
 
83
    asio::basic_streambuf<Allocator>& b)
 
84
{
 
85
  asio::error_code ec;
 
86
  std::size_t bytes_transferred = write(s, b, transfer_all(), ec);
 
87
  asio::detail::throw_error(ec);
 
88
  return bytes_transferred;
 
89
}
 
90
 
 
91
template <typename SyncWriteStream, typename Allocator,
 
92
    typename CompletionCondition>
 
93
inline std::size_t write(SyncWriteStream& s,
 
94
    asio::basic_streambuf<Allocator>& b,
 
95
    CompletionCondition completion_condition)
 
96
{
 
97
  asio::error_code ec;
 
98
  std::size_t bytes_transferred = write(s, b, completion_condition, ec);
 
99
  asio::detail::throw_error(ec);
 
100
  return bytes_transferred;
 
101
}
 
102
 
 
103
namespace detail
 
104
{
 
105
  template <typename AsyncWriteStream, typename ConstBufferSequence,
 
106
      typename CompletionCondition, typename WriteHandler>
 
107
  class write_handler
 
108
  {
 
109
  public:
 
110
    typedef asio::detail::consuming_buffers<
 
111
      const_buffer, ConstBufferSequence> buffers_type;
 
112
 
 
113
    write_handler(AsyncWriteStream& stream, const buffers_type& buffers,
 
114
        CompletionCondition completion_condition, WriteHandler handler)
 
115
      : stream_(stream),
 
116
        buffers_(buffers),
 
117
        total_transferred_(0),
 
118
        completion_condition_(completion_condition),
 
119
        handler_(handler)
 
120
    {
 
121
    }
 
122
 
 
123
    void operator()(const asio::error_code& ec,
 
124
        std::size_t bytes_transferred)
 
125
    {
 
126
      total_transferred_ += bytes_transferred;
 
127
      buffers_.consume(bytes_transferred);
 
128
      if (completion_condition_(ec, total_transferred_)
 
129
          || buffers_.begin() == buffers_.end())
 
130
      {
 
131
        handler_(ec, total_transferred_);
 
132
      }
 
133
      else
 
134
      {
 
135
        stream_.async_write_some(buffers_, *this);
 
136
      }
 
137
    }
 
138
 
 
139
  //private:
 
140
    AsyncWriteStream& stream_;
 
141
    buffers_type buffers_;
 
142
    std::size_t total_transferred_;
 
143
    CompletionCondition completion_condition_;
 
144
    WriteHandler handler_;
 
145
  };
 
146
 
 
147
  template <typename AsyncWriteStream, typename ConstBufferSequence,
 
148
      typename CompletionCondition, typename WriteHandler>
 
149
  inline void* asio_handler_allocate(std::size_t size,
 
150
      write_handler<AsyncWriteStream, ConstBufferSequence,
 
151
        CompletionCondition, WriteHandler>* this_handler)
 
152
  {
 
153
    return asio_handler_alloc_helpers::allocate(
 
154
        size, &this_handler->handler_);
 
155
  }
 
156
 
 
157
  template <typename AsyncWriteStream, typename ConstBufferSequence,
 
158
      typename CompletionCondition, typename WriteHandler>
 
159
  inline void asio_handler_deallocate(void* pointer, std::size_t size,
 
160
      write_handler<AsyncWriteStream, ConstBufferSequence,
 
161
        CompletionCondition, WriteHandler>* this_handler)
 
162
  {
 
163
    asio_handler_alloc_helpers::deallocate(
 
164
        pointer, size, &this_handler->handler_);
 
165
  }
 
166
 
 
167
  template <typename Function, typename AsyncWriteStream,
 
168
      typename ConstBufferSequence, typename CompletionCondition,
 
169
      typename WriteHandler>
 
170
  inline void asio_handler_invoke(const Function& function,
 
171
      write_handler<AsyncWriteStream, ConstBufferSequence,
 
172
        CompletionCondition, WriteHandler>* this_handler)
 
173
  {
 
174
    asio_handler_invoke_helpers::invoke(
 
175
        function, &this_handler->handler_);
 
176
  }
 
177
} // namespace detail
 
178
 
 
179
template <typename AsyncWriteStream, typename ConstBufferSequence,
 
180
  typename CompletionCondition, typename WriteHandler>
 
181
inline void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
 
182
    CompletionCondition completion_condition, WriteHandler handler)
 
183
{
 
184
  asio::detail::consuming_buffers<
 
185
    const_buffer, ConstBufferSequence> tmp(buffers);
 
186
  s.async_write_some(tmp,
 
187
      detail::write_handler<AsyncWriteStream, ConstBufferSequence,
 
188
        CompletionCondition, WriteHandler>(
 
189
          s, tmp, completion_condition, handler));
 
190
}
 
191
 
 
192
template <typename AsyncWriteStream, typename ConstBufferSequence,
 
193
    typename WriteHandler>
 
194
inline void async_write(AsyncWriteStream& s, const ConstBufferSequence& buffers,
 
195
    WriteHandler handler)
 
196
{
 
197
  async_write(s, buffers, transfer_all(), handler);
 
198
}
 
199
 
 
200
namespace detail
 
201
{
 
202
  template <typename AsyncWriteStream, typename Allocator,
 
203
      typename WriteHandler>
 
204
  class write_streambuf_handler
 
205
  {
 
206
  public:
 
207
    write_streambuf_handler(asio::basic_streambuf<Allocator>& streambuf,
 
208
        WriteHandler handler)
 
209
      : streambuf_(streambuf),
 
210
        handler_(handler)
 
211
    {
 
212
    }
 
213
 
 
214
    void operator()(const asio::error_code& ec,
 
215
        std::size_t bytes_transferred)
 
216
    {
 
217
      streambuf_.consume(bytes_transferred);
 
218
      handler_(ec, bytes_transferred);
 
219
    }
 
220
 
 
221
  //private:
 
222
    asio::basic_streambuf<Allocator>& streambuf_;
 
223
    WriteHandler handler_;
 
224
  };
 
225
 
 
226
  template <typename AsyncWriteStream, typename Allocator,
 
227
      typename WriteHandler>
 
228
  inline void* asio_handler_allocate(std::size_t size,
 
229
      write_streambuf_handler<AsyncWriteStream,
 
230
        Allocator, WriteHandler>* this_handler)
 
231
  {
 
232
    return asio_handler_alloc_helpers::allocate(
 
233
        size, &this_handler->handler_);
 
234
  }
 
235
 
 
236
  template <typename AsyncWriteStream, typename Allocator,
 
237
      typename WriteHandler>
 
238
  inline void asio_handler_deallocate(void* pointer, std::size_t size,
 
239
      write_streambuf_handler<AsyncWriteStream,
 
240
        Allocator, WriteHandler>* this_handler)
 
241
  {
 
242
    asio_handler_alloc_helpers::deallocate(
 
243
        pointer, size, &this_handler->handler_);
 
244
  }
 
245
 
 
246
  template <typename Function, typename AsyncWriteStream, typename Allocator,
 
247
      typename WriteHandler>
 
248
  inline void asio_handler_invoke(const Function& function,
 
249
      write_streambuf_handler<AsyncWriteStream,
 
250
        Allocator, WriteHandler>* this_handler)
 
251
  {
 
252
    asio_handler_invoke_helpers::invoke(
 
253
        function, &this_handler->handler_);
 
254
  }
 
255
} // namespace detail
 
256
 
 
257
template <typename AsyncWriteStream, typename Allocator,
 
258
    typename CompletionCondition, typename WriteHandler>
 
259
inline void async_write(AsyncWriteStream& s,
 
260
    asio::basic_streambuf<Allocator>& b,
 
261
    CompletionCondition completion_condition, WriteHandler handler)
 
262
{
 
263
  async_write(s, b.data(), completion_condition,
 
264
      detail::write_streambuf_handler<
 
265
        AsyncWriteStream, Allocator, WriteHandler>(b, handler));
 
266
}
 
267
 
 
268
template <typename AsyncWriteStream, typename Allocator, typename WriteHandler>
 
269
inline void async_write(AsyncWriteStream& s,
 
270
    asio::basic_streambuf<Allocator>& b, WriteHandler handler)
 
271
{
 
272
  async_write(s, b, transfer_all(), handler);
 
273
}
 
274
 
 
275
} // namespace asio
 
276
 
 
277
#include "asio/detail/pop_options.hpp"
 
278
 
 
279
#endif // ASIO_WRITE_IPP