1 //
2 // basic_socket_streambuf.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 BOOST_ASIO_BASIC_SOCKET_STREAMBUF_HPP
12 #define BOOST_ASIO_BASIC_SOCKET_STREAMBUF_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include <boost/asio/detail/config.hpp>
19 
20 #if !defined(BOOST_ASIO_NO_IOSTREAM)
21 
22 #include <streambuf>
23 #include <vector>
24 #include <boost/asio/basic_socket.hpp>
25 #include <boost/asio/basic_stream_socket.hpp>
26 #include <boost/asio/detail/buffer_sequence_adapter.hpp>
27 #include <boost/asio/detail/memory.hpp>
28 #include <boost/asio/detail/throw_error.hpp>
29 #include <boost/asio/io_context.hpp>
30 
31 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
32 # include <boost/asio/stream_socket_service.hpp>
33 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
34 
35 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
36 # if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
37 #  include <boost/asio/deadline_timer_service.hpp>
38 # else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
39 #  include <boost/asio/detail/deadline_timer_service.hpp>
40 # endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
41 #else
42 # include <boost/asio/steady_timer.hpp>
first_finderFboost::algorithm::detail::first_finderF43 #endif
44 
45 #if !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
46 
47 # include <boost/asio/detail/variadic_templates.hpp>
48 
49 // A macro that should expand to:
50 //   template <typename T1, ..., typename Tn>
51 //   basic_socket_streambuf* connect(T1 x1, ..., Tn xn)
52 //   {
53 //     init_buffers();
54 //     typedef typename Protocol::resolver resolver_type;
55 //     resolver_type resolver(socket().get_executor().context());
56 //     connect_to_endpoints(
57 //         resolver.resolve(x1, ..., xn, ec_));
58 //     return !ec_ ? this : 0;
59 //   }
60 // This macro should only persist within this file.
61 
62 # define BOOST_ASIO_PRIVATE_CONNECT_DEF(n) \
63   template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
64   basic_socket_streambuf* connect(BOOST_ASIO_VARIADIC_BYVAL_PARAMS(n)) \
65   { \
66     init_buffers(); \
67     typedef typename Protocol::resolver resolver_type; \
68     resolver_type resolver(socket().get_executor().context()); \
69     connect_to_endpoints( \
70         resolver.resolve(BOOST_ASIO_VARIADIC_BYVAL_ARGS(n), ec_)); \
71     return !ec_ ? this : 0; \
72   } \
73   /**/
74 
75 #endif // !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
76 
77 #if !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
78 # define BOOST_ASIO_SVC_T1 detail::deadline_timer_service<traits_helper>
79 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
80 
81 #include <boost/asio/detail/push_options.hpp>
82 
83 namespace boost {
84 namespace asio {
85 namespace detail {
86 
87 // A separate base class is used to ensure that the io_context member is
88 // initialised prior to the basic_socket_streambuf's basic_socket base class.
89 class socket_streambuf_io_context
90 {
91 protected:
92   socket_streambuf_io_context(io_context* ctx)
93     : default_io_context_(ctx)
94   {
95   }
96 
97   shared_ptr<io_context> default_io_context_;
98 };
99 
100 // A separate base class is used to ensure that the dynamically allocated
101 // buffers are constructed prior to the basic_socket_streambuf's basic_socket
102 // base class. This makes moving the socket is the last potentially throwing
103 // step in the streambuf's move constructor, giving the constructor a strong
104 // exception safety guarantee.
105 class socket_streambuf_buffers
106 {
107 protected:
108   socket_streambuf_buffers()
109     : get_buffer_(buffer_size),
last_finderFboost::algorithm::detail::last_finderF110       put_buffer_(buffer_size)
111   {
112   }
113 
114   enum { buffer_size = 512 };
115   std::vector<char> get_buffer_;
116   std::vector<char> put_buffer_;
117 };
118 
119 } // namespace detail
120 
operator ()boost::algorithm::detail::last_finderF121 #if !defined(BOOST_ASIO_BASIC_SOCKET_STREAMBUF_FWD_DECL)
122 #define BOOST_ASIO_BASIC_SOCKET_STREAMBUF_FWD_DECL
123 
124 // Forward declaration with defaulted arguments.
125 template <typename Protocol
126     BOOST_ASIO_SVC_TPARAM_DEF1(= stream_socket_service<Protocol>),
127 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
128     typename Clock = boost::posix_time::ptime,
129     typename WaitTraits = time_traits<Clock>
130     BOOST_ASIO_SVC_TPARAM1_DEF2(= deadline_timer_service<Clock, WaitTraits>)>
131 #else
132     typename Clock = chrono::steady_clock,
133     typename WaitTraits = wait_traits<Clock>
134     BOOST_ASIO_SVC_TPARAM1_DEF1(= steady_timer::service_type)>
135 #endif
136 class basic_socket_streambuf;
137 
138 #endif // !defined(BOOST_ASIO_BASIC_SOCKET_STREAMBUF_FWD_DECL)
139 
finditboost::algorithm::detail::last_finderF140 /// Iostream streambuf for a socket.
141 #if defined(GENERATING_DOCUMENTATION)
142 template <typename Protocol,
143     typename Clock = chrono::steady_clock,
144     typename WaitTraits = wait_traits<Clock> >
145 #else // defined(GENERATING_DOCUMENTATION)
146 template <typename Protocol BOOST_ASIO_SVC_TPARAM,
147     typename Clock, typename WaitTraits BOOST_ASIO_SVC_TPARAM1>
148 #endif // defined(GENERATING_DOCUMENTATION)
149 class basic_socket_streambuf
150   : public std::streambuf,
151     private detail::socket_streambuf_io_context,
152     private detail::socket_streambuf_buffers,
153 #if defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
154     private basic_socket<Protocol BOOST_ASIO_SVC_TARG>
155 #else // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
156     public basic_socket<Protocol BOOST_ASIO_SVC_TARG>
157 #endif // defined(BOOST_ASIO_NO_DEPRECATED) || defined(GENERATING_DOCUMENTATION)
158 {
159 private:
160   // These typedefs are intended keep this class's implementation independent
161   // of whether it's using Boost.DateClock, Boost.Chrono or std::chrono.
162 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
163   typedef WaitTraits traits_helper;
164 #else
165   typedef detail::chrono_time_traits<Clock, WaitTraits> traits_helper;
166 #endif
167 
168 public:
169   /// The protocol type.
170   typedef Protocol protocol_type;
171 
172   /// The endpoint type.
173   typedef typename Protocol::endpoint endpoint_type;
174 
175   /// The clock type.
176   typedef Clock clock_type;
177 
178 #if defined(GENERATING_DOCUMENTATION)
179   /// (Deprecated: Use time_point.) The time type.
180   typedef typename WaitTraits::time_type time_type;
181 
182   /// The time type.
183   typedef typename WaitTraits::time_point time_point;
184 
185   /// (Deprecated: Use duration.) The duration type.
186   typedef typename WaitTraits::duration_type duration_type;
187 
188   /// The duration type.
189   typedef typename WaitTraits::duration duration;
190 #else
191 # if !defined(BOOST_ASIO_NO_DEPRECATED)
192   typedef typename traits_helper::time_type time_type;
193   typedef typename traits_helper::duration_type duration_type;
194 # endif // !defined(BOOST_ASIO_NO_DEPRECATED)
195   typedef typename traits_helper::time_type time_point;
196   typedef typename traits_helper::duration_type duration;
197 #endif
198 
199   /// Construct a basic_socket_streambuf without establishing a connection.
200   basic_socket_streambuf()
201     : detail::socket_streambuf_io_context(new io_context),
202       basic_socket<Protocol BOOST_ASIO_SVC_TARG>(*default_io_context_),
203       expiry_time_(max_expiry_time())
204   {
205     init_buffers();
206   }
207 
208 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
209   /// Construct a basic_socket_streambuf from the supplied socket.
210   explicit basic_socket_streambuf(basic_stream_socket<protocol_type> s)
211     : detail::socket_streambuf_io_context(0),
212       basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(s)),
213       expiry_time_(max_expiry_time())
214   {
215     init_buffers();
216   }
217 
218   /// Move-construct a basic_socket_streambuf from another.
219   basic_socket_streambuf(basic_socket_streambuf&& other)
220     : detail::socket_streambuf_io_context(other),
221       basic_socket<Protocol BOOST_ASIO_SVC_TARG>(std::move(other.socket())),
222       ec_(other.ec_),
223       expiry_time_(other.expiry_time_)
224   {
225     get_buffer_.swap(other.get_buffer_);
226     put_buffer_.swap(other.put_buffer_);
227     setg(other.eback(), other.gptr(), other.egptr());
228     setp(other.pptr(), other.epptr());
229     other.ec_ = boost::system::error_code();
230     other.expiry_time_ = max_expiry_time();
231     other.init_buffers();
232   }
233 
234   /// Move-assign a basic_socket_streambuf from another.
235   basic_socket_streambuf& operator=(basic_socket_streambuf&& other)
236   {
237     this->close();
238     socket() = std::move(other.socket());
239     detail::socket_streambuf_io_context::operator=(other);
240     ec_ = other.ec_;
241     expiry_time_ = other.expiry_time_;
242     get_buffer_.swap(other.get_buffer_);
243     put_buffer_.swap(other.put_buffer_);
244     setg(other.eback(), other.gptr(), other.egptr());
245     setp(other.pptr(), other.epptr());
246     other.ec_ = boost::system::error_code();
247     other.expiry_time_ = max_expiry_time();
248     other.put_buffer_.resize(buffer_size);
249     other.init_buffers();
250     return *this;
251   }
252 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
253 
254   /// Destructor flushes buffered data.
255   virtual ~basic_socket_streambuf()
256   {
257     if (pptr() != pbase())
258       overflow(traits_type::eof());
259   }
260 
261   /// Establish a connection.
262   /**
263    * This function establishes a connection to the specified endpoint.
264    *
265    * @return \c this if a connection was successfully established, a null
266    * pointer otherwise.
267    */
268   basic_socket_streambuf* connect(const endpoint_type& endpoint)
269   {
270     init_buffers();
271     ec_ = boost::system::error_code();
272     this->connect_to_endpoints(&endpoint, &endpoint + 1);
273     return !ec_ ? this : 0;
274   }
275 
276 #if defined(GENERATING_DOCUMENTATION)
277   /// Establish a connection.
278   /**
279    * This function automatically establishes a connection based on the supplied
280    * resolver query parameters. The arguments are used to construct a resolver
281    * query object.
282    *
283    * @return \c this if a connection was successfully established, a null
284    * pointer otherwise.
285    */
286   template <typename T1, ..., typename TN>
287   basic_socket_streambuf* connect(T1 t1, ..., TN tn);
288 #elif defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
289   template <typename... T>
290   basic_socket_streambuf* connect(T... x)
291   {
292     init_buffers();
293     typedef typename Protocol::resolver resolver_type;
294     resolver_type resolver(socket().get_executor().context());
295     connect_to_endpoints(resolver.resolve(x..., ec_));
296     return !ec_ ? this : 0;
297   }
298 #else
299   BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_CONNECT_DEF)
300 #endif
301 
302   /// Close the connection.
303   /**
304    * @return \c this if a connection was successfully established, a null
305    * pointer otherwise.
306    */
307   basic_socket_streambuf* close()
308   {
309     sync();
310     socket().close(ec_);
311     if (!ec_)
312       init_buffers();
313     return !ec_ ? this : 0;
314   }
315 
316   /// Get a reference to the underlying socket.
317   basic_socket<Protocol BOOST_ASIO_SVC_TARG>& socket()
318   {
319     return *this;
320   }
321 
322   /// Get the last error associated with the stream buffer.
323   /**
324    * @return An \c error_code corresponding to the last error from the stream
325    * buffer.
326    */
327   const boost::system::error_code& error() const
328   {
329     return ec_;
330   }
331 
332 #if !defined(BOOST_ASIO_NO_DEPRECATED)
333   /// (Deprecated: Use error().) Get the last error associated with the stream
334   /// buffer.
335   /**
336    * @return An \c error_code corresponding to the last error from the stream
337    * buffer.
338    */
339   const boost::system::error_code& puberror() const
340   {
341     return error();
342   }
343 
344   /// (Deprecated: Use expiry().) Get the stream buffer's expiry time as an
345   /// absolute time.
346   /**
347    * @return An absolute time value representing the stream buffer's expiry
348    * time.
349    */
350   time_point expires_at() const
351   {
352     return expiry_time_;
353   }
354 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
355 
356   /// Get the stream buffer's expiry time as an absolute time.
357   /**
358    * @return An absolute time value representing the stream buffer's expiry
359    * time.
360    */
361   time_point expiry() const
362   {
363     return expiry_time_;
364   }
365 
366   /// Set the stream buffer's expiry time as an absolute time.
367   /**
368    * This function sets the expiry time associated with the stream. Stream
369    * operations performed after this time (where the operations cannot be
370    * completed using the internal buffers) will fail with the error
371    * boost::asio::error::operation_aborted.
372    *
373    * @param expiry_time The expiry time to be used for the stream.
374    */
375   void expires_at(const time_point& expiry_time)
376   {
377     expiry_time_ = expiry_time;
378   }
379 
380   /// Set the stream buffer's expiry time relative to now.
381   /**
382    * This function sets the expiry time associated with the stream. Stream
383    * operations performed after this time (where the operations cannot be
384    * completed using the internal buffers) will fail with the error
385    * boost::asio::error::operation_aborted.
386    *
387    * @param expiry_time The expiry time to be used for the timer.
388    */
389   void expires_after(const duration& expiry_time)
390   {
391     expiry_time_ = traits_helper::add(traits_helper::now(), expiry_time);
392   }
393 
394 #if !defined(BOOST_ASIO_NO_DEPRECATED)
395   /// (Deprecated: Use expiry().) Get the stream buffer's expiry time relative
396   /// to now.
397   /**
398    * @return A relative time value representing the stream buffer's expiry time.
399    */
400   duration expires_from_now() const
401   {
402     return traits_helper::subtract(expires_at(), traits_helper::now());
403   }
404 
405   /// (Deprecated: Use expires_after().) Set the stream buffer's expiry time
406   /// relative to now.
407   /**
408    * This function sets the expiry time associated with the stream. Stream
409    * operations performed after this time (where the operations cannot be
410    * completed using the internal buffers) will fail with the error
411    * boost::asio::error::operation_aborted.
412    *
413    * @param expiry_time The expiry time to be used for the timer.
414    */
415   void expires_from_now(const duration& expiry_time)
416   {
417     expiry_time_ = traits_helper::add(traits_helper::now(), expiry_time);
418   }
419 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
420 
421 protected:
422   int_type underflow()
423   {
424 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
425     ec_ = boost::asio::error::operation_not_supported;
426     return traits_type::eof();
427 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
428     if (gptr() != egptr())
429       return traits_type::eof();
430 
431     for (;;)
432     {
433       // Check if we are past the expiry time.
434       if (traits_helper::less_than(expiry_time_, traits_helper::now()))
435       {
436         ec_ = boost::asio::error::timed_out;
437         return traits_type::eof();
438       }
439 
440       // Try to complete the operation without blocking.
441       if (!socket().native_non_blocking())
442         socket().native_non_blocking(true, ec_);
443       detail::buffer_sequence_adapter<mutable_buffer, mutable_buffer>
444         bufs(boost::asio::buffer(get_buffer_) + putback_max);
445       detail::signed_size_type bytes = detail::socket_ops::recv(
446           socket().native_handle(), bufs.buffers(), bufs.count(), 0, ec_);
447 
448       // Check if operation succeeded.
449       if (bytes > 0)
450       {
451         setg(&get_buffer_[0], &get_buffer_[0] + putback_max,
452             &get_buffer_[0] + putback_max + bytes);
453         return traits_type::to_int_type(*gptr());
454       }
455 
456       // Check for EOF.
457       if (bytes == 0)
458       {
459         ec_ = boost::asio::error::eof;
460         return traits_type::eof();
461       }
462 
463       // Operation failed.
464       if (ec_ != boost::asio::error::would_block
465           && ec_ != boost::asio::error::try_again)
466         return traits_type::eof();
467 
468       // Wait for socket to become ready.
469       if (detail::socket_ops::poll_read(
470             socket().native_handle(), 0, timeout(), ec_) < 0)
471         return traits_type::eof();
472     }
473 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
474   }
475 
476   int_type overflow(int_type c)
477   {
478 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
479     ec_ = boost::asio::error::operation_not_supported;
480     return traits_type::eof();
481 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
482     char_type ch = traits_type::to_char_type(c);
483 
484     // Determine what needs to be sent.
485     const_buffer output_buffer;
486     if (put_buffer_.empty())
487     {
488       if (traits_type::eq_int_type(c, traits_type::eof()))
489         return traits_type::not_eof(c); // Nothing to do.
490       output_buffer = boost::asio::buffer(&ch, sizeof(char_type));
491     }
492     else
493     {
494       output_buffer = boost::asio::buffer(pbase(),
495           (pptr() - pbase()) * sizeof(char_type));
496     }
497 
498     while (output_buffer.size() > 0)
499     {
500       // Check if we are past the expiry time.
501       if (traits_helper::less_than(expiry_time_, traits_helper::now()))
502       {
503         ec_ = boost::asio::error::timed_out;
504         return traits_type::eof();
505       }
506 
507       // Try to complete the operation without blocking.
508       if (!socket().native_non_blocking())
509         socket().native_non_blocking(true, ec_);
510       detail::buffer_sequence_adapter<
511         const_buffer, const_buffer> bufs(output_buffer);
512       detail::signed_size_type bytes = detail::socket_ops::send(
513           socket().native_handle(), bufs.buffers(), bufs.count(), 0, ec_);
514 
515       // Check if operation succeeded.
516       if (bytes > 0)
517       {
518         output_buffer += static_cast<std::size_t>(bytes);
519         continue;
520       }
521 
522       // Operation failed.
523       if (ec_ != boost::asio::error::would_block
524           && ec_ != boost::asio::error::try_again)
525         return traits_type::eof();
526 
527       // Wait for socket to become ready.
528       if (detail::socket_ops::poll_write(
529             socket().native_handle(), 0, timeout(), ec_) < 0)
530         return traits_type::eof();
531     }
532 
533     if (!put_buffer_.empty())
534     {
535       setp(&put_buffer_[0], &put_buffer_[0] + put_buffer_.size());
536 
537       // If the new character is eof then our work here is done.
538       if (traits_type::eq_int_type(c, traits_type::eof()))
539         return traits_type::not_eof(c);
540 
541       // Add the new character to the output buffer.
542       *pptr() = ch;
543       pbump(1);
544     }
545 
546     return c;
547 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
548   }
549 
550   int sync()
551   {
552     return overflow(traits_type::eof());
553   }
554 
555   std::streambuf* setbuf(char_type* s, std::streamsize n)
556   {
557     if (pptr() == pbase() && s == 0 && n == 0)
558     {
559       put_buffer_.clear();
560       setp(0, 0);
561       sync();
562       return this;
563     }
564 
565     return 0;
566   }
567 
568 private:
569   // Disallow copying and assignment.
570   basic_socket_streambuf(const basic_socket_streambuf&) BOOST_ASIO_DELETED;
571   basic_socket_streambuf& operator=(
572       const basic_socket_streambuf&) BOOST_ASIO_DELETED;
573 
574   void init_buffers()
575   {
576     setg(&get_buffer_[0],
577         &get_buffer_[0] + putback_max,
578         &get_buffer_[0] + putback_max);
579 
580     if (put_buffer_.empty())
581       setp(0, 0);
582     else
583       setp(&put_buffer_[0], &put_buffer_[0] + put_buffer_.size());
584   }
585 
586   int timeout() const
587   {
588     int64_t msec = traits_helper::to_posix_duration(
589         traits_helper::subtract(expiry_time_,
590           traits_helper::now())).total_milliseconds();
591     if (msec > (std::numeric_limits<int>::max)())
592       msec = (std::numeric_limits<int>::max)();
593     else if (msec < 0)
594       msec = 0;
595     return static_cast<int>(msec);
596   }
597 
598   template <typename EndpointSequence>
599   void connect_to_endpoints(const EndpointSequence& endpoints)
600   {
601     this->connect_to_endpoints(endpoints.begin(), endpoints.end());
602   }
603 
604   template <typename EndpointIterator>
605   void connect_to_endpoints(EndpointIterator begin, EndpointIterator end)
606   {
607 #if defined(BOOST_ASIO_WINDOWS_RUNTIME)
608     ec_ = boost::asio::error::operation_not_supported;
609 #else // defined(BOOST_ASIO_WINDOWS_RUNTIME)
610     if (ec_)
611       return;
612 
613     ec_ = boost::asio::error::not_found;
614     for (EndpointIterator i = begin; i != end; ++i)
615     {
616       // Check if we are past the expiry time.
617       if (traits_helper::less_than(expiry_time_, traits_helper::now()))
618       {
619         ec_ = boost::asio::error::timed_out;
620         return;
621       }
622 
623       // Close and reopen the socket.
624       typename Protocol::endpoint ep(*i);
625       socket().close(ec_);
626       socket().open(ep.protocol(), ec_);
627       if (ec_)
628         continue;
629 
630       // Try to complete the operation without blocking.
631       if (!socket().native_non_blocking())
632         socket().native_non_blocking(true, ec_);
633       detail::socket_ops::connect(socket().native_handle(),
634           ep.data(), ep.size(), ec_);
635 
636       // Check if operation succeeded.
637       if (!ec_)
638         return;
639 
640       // Operation failed.
641       if (ec_ != boost::asio::error::in_progress
642           && ec_ != boost::asio::error::would_block)
643         continue;
644 
645       // Wait for socket to become ready.
646       if (detail::socket_ops::poll_connect(
647             socket().native_handle(), timeout(), ec_) < 0)
648         continue;
649 
650       // Get the error code from the connect operation.
651       int connect_error = 0;
652       size_t connect_error_len = sizeof(connect_error);
653       if (detail::socket_ops::getsockopt(socket().native_handle(), 0,
654             SOL_SOCKET, SO_ERROR, &connect_error, &connect_error_len, ec_)
655           == detail::socket_error_retval)
656         return;
657 
658       // Check the result of the connect operation.
659       ec_ = boost::system::error_code(connect_error,
660           boost::asio::error::get_system_category());
661       if (!ec_)
662         return;
663     }
664 #endif // defined(BOOST_ASIO_WINDOWS_RUNTIME)
665   }
666 
667   // Helper function to get the maximum expiry time.
668   static time_point max_expiry_time()
669   {
670 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
671     return boost::posix_time::pos_infin;
672 #else // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
673     return (time_point::max)();
674 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
675   }
676 
677   enum { putback_max = 8 };
678   boost::system::error_code ec_;
679   time_point expiry_time_;
680 };
681 
682 } // namespace asio
683 } // namespace boost
684 
685 #include <boost/asio/detail/pop_options.hpp>
686 
687 #if !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
688 # undef BOOST_ASIO_SVC_T1
689 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
690 
691 #if !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
692 # undef BOOST_ASIO_PRIVATE_CONNECT_DEF
693 #endif // !defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
694 
695 #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
696 
697 #endif // BOOST_ASIO_BASIC_SOCKET_STREAMBUF_HPP
698