Ticket #4787: boost-ext.hpp

File boost-ext.hpp, 2.8 KB (added by saleyn@…, 12 years ago)

Implementation of timestamp supporting socket options.

Line 
1//----------------------------------------------------------------------------
2/// \file boost-ext.hpp
3//----------------------------------------------------------------------------
4/// \brief This file contains implementation of a timestamp supporting
5/// socket options.
6//----------------------------------------------------------------------------
7// Author: Serge Aleynikov <saleyn@gmail.com>
8// Created: 2010-09-30
9// License: The same open-source licensing terms apply as in boost::asio.
10//----------------------------------------------------------------------------
11#ifndef _BOOST_ASIO_EXT_HPP_
12#define _BOOST_ASIO_EXT_HPP_
13
14#include <time.h>
15#include <boost/asio/detail/socket_option.hpp>
16
17namespace boost {
18namespace asio {
19
20namespace ip {
21namespace unicast {
22
23#if defined(GENERATING_DOCUMENTATION)
24 typedef implementation_defined timestamp;
25#else
26 typedef boost::asio::detail::socket_option::boolean<
27 SOL_SOCKET, SO_TIMESTAMP> timestamp;
28#endif
29
30#if defined(GENERATING_DOCUMENTATION)
31 typedef implementation_defined timestampns;
32#else
33 typedef boost::asio::detail::socket_option::boolean<
34 SOL_SOCKET, SO_TIMESTAMPNS> timestampns;
35#endif
36
37} // namespace unicast
38} // namespace ip
39
40namespace detail {
41namespace io_control {
42
43// Helper template for implementing SIOCGSTAMP option.
44class siocgstamp {
45public:
46 // Default constructor.
47 siocgstamp() {
48 m_value.tv_sec = 0;
49 m_value.tv_usec = 0;
50 }
51
52 siocgstamp(struct timeval* tv) { set(*tv); }
53
54 const struct timeval* get() const { return &m_value; }
55
56 void set(const struct timeval& tv) {
57 m_value.tv_sec = tv.tv_sec;
58 m_value.tv_usec = tv.tv_usec;
59 }
60
61 //const struct timeval& get() const { return m_value; }
62 //struct timeval& get() { return m_value; }
63
64 const struct timeval* operator*() const { return &m_value; }
65 struct timeval* operator*() { return &m_value; }
66
67 // Get the value for the siocgstamp timeout.
68 double value() const {
69 return static_cast<double>(m_value.tv_sec)
70 + static_cast<double>(m_value.tv_usec) / 1000000.0;
71 }
72
73 long diff_usec(struct timeval& other) {
74 return (m_value.tv_sec - other.tv_sec)*1000000
75 + (m_value.tv_usec - other.tv_usec);
76 }
77
78 // Get the name of the socket option.
79 int name() const { return SIOCGSTAMP; }
80
81 // Get the address of the siocgstamp data.
82 detail::ioctl_arg_type* data() {
83 return reinterpret_cast<detail::ioctl_arg_type*>(&m_value);
84 }
85
86 // Get the address of the siocgstamp data.
87 const detail::ioctl_arg_type* data() const {
88 return reinterpret_cast<const detail::ioctl_arg_type*>(&m_value);
89 }
90private:
91 struct timeval m_value;
92};
93
94} // namespace socket_option
95} // namespace detail
96} // namespace asio
97} // namespace boost
98
99#endif // _BOOST_ASIO_EXT_HPP_