iosockstream_abstract.h 6.49 KB
Newer Older
Davis King's avatar
Davis King committed
1
2
// Copyright (C) 2012  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
3
4
#undef DLIB_IOSOCKSTrEAM_ABSTRACT_Hh_
#ifdef DLIB_IOSOCKSTrEAM_ABSTRACT_Hh_
Davis King's avatar
Davis King committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

#include "../sockstreambuf/sockstreambuf_abstract.h"

#include <iostream>

namespace dlib
{

// ---------------------------------------------------------------------------------------- 

    class iosockstream : public std::iostream
    {
        /*!
            WHAT THIS OBJECT REPRESENTS
                This is an iostream object that reads/writes from a TCP network connection.

21
22
23
                Note that any attempt to read from this stream will automatically flush the
                stream's output buffers.  

Davis King's avatar
Davis King committed
24
            THREAD SAFETY
25
26
27
28
                It is not safe for multiple threads to make concurrent accesses to the same
                instance of this object (except for calls to shutdown() which are always
                threadsafe).  Therefore, you should mutex lock an instance of this object
                if you need to touch it from multiple threads. 
Davis King's avatar
Davis King committed
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
        !*/

    public:

        iosockstream(
        );
        /*!
            ensures
                - #good() == false
        !*/

        iosockstream( 
            const network_address& addr
        );
        /*!
            ensures
                - Attempts to connect to the given network address. 
                - Calling this constructor is equivalent to calling the default constructor
                  and then invoking open(addr).
                - #good() == true 
            throws
                - dlib::socket_error
                    This exception is thrown if there is some problem that prevents us from
                    creating the connection.  
        !*/

        iosockstream( 
            const network_address& addr,
            unsigned long timeout
        ); 
        /*!
            ensures
                - Attempts to connect to the given network address. 
                - Calling this constructor is equivalent to calling the default constructor
                  and then invoking open(addr, timeout).
                - #good() == true 
            throws
                - dlib::socket_error
                    This exception is thrown if there is some problem that prevents us from
                    creating the connection or if timeout milliseconds elapses before the
                    connect is successful.
        !*/

        ~iosockstream(
        );
        /*!
            ensures
                - Invokes close() before destructing the stream.  Therefore, any open
                  connection will be gracefully closed using the default timeout time.
                  This also means any data in the stream will be flushed to the connection.
        !*/

        void open (
            const network_address& addr
        );
        /*!
            ensures
                - This object will attempt to create a TCP connection with the remote host
                  indicated by addr.  
                - Any previous connection in this iosockstream is closed by calling close()
                  before we make any new connection.
                - #good() == true
                  (i.e. the error flags are reset by calling open())
            throws
                - dlib::socket_error
                    This exception is thrown if there is some problem that prevents us from
                    creating the connection.  
        !*/

        void open (
            const network_address& addr,
            unsigned long timeout 
        );
        /*!
            ensures
                - This object will attempt to create a TCP connection with the remote host
                  indicated by addr.  
                - Any previous connection in this iosockstream is closed by calling close()
                  before we make any new connection.
                - #good() == true
                  (i.e. the error flags are reset by calling open())
            throws
                - dlib::socket_error
                    This exception is thrown if there is some problem that prevents us from
                    creating the connection or if timeout milliseconds elapses before the
                    connect is successful.
        !*/

        void close(
            unsigned long timeout = 10000
        );
        /*!
            ensures
                - #good() == false 
                - if (there is an active TCP connection) then
                    - Flushes any data buffered in the output part of the stream
                      to the connection.  
                    - Performs a proper graceful close (i.e. like dlib::close_gracefully()).
                    - Will only wait timeout milliseconds for the buffer flush and graceful
                      close to finish before the connection is terminated forcefully.
                      Therefore, close() will only block for at most timeout milliseconds.
        !*/

        void terminate_connection_after_timeout (
            unsigned long timeout
        );
        /*!
            ensures
                - if (there is an active TCP connection) then
                    - Any operations on this TCP connection will return error or
                      end-of-file once timeout milliseconds have elapsed from this call to
                      terminate_connection_after_timeout().  This is true unless another
                      call to terminate_connection_after_timeout() is made which gives a
                      new time.  In this case, the previous call is forgotten and the
                      timeout is reset.
                    - This timeout only applies to the current TCP connection.  That is, if
                      the iosockstream is closed and a new connection is established, any
                      previous timeouts setup by terminate_connection_after_timeout() do
                      not apply. 
                - else
                    - This function has no effect on this object.
        !*/

152
153
154
155
156
157
158
159
160
        void shutdown (
        );
        /*!
            ensures
                - Immediately closes the TCP connection and causes all I/O operations on
                  this object to return an error.  
                - It is safe to call this function from any thread, therefore, you can use
                  it to signal when you want a connection to terminate from another thread.
        !*/
Davis King's avatar
Davis King committed
161
162
163
164
165
166
167
    };

// ---------------------------------------------------------------------------------------- 

}


168
#endif // DLIB_IOSOCKSTrEAM_ABSTRACT_Hh_
Davis King's avatar
Davis King committed
169
170
171