00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00048 #ifndef INETSTL_INCL_INETSTL_NETWORK_HPP_CONNECTION
00049 #define INETSTL_INCL_INETSTL_NETWORK_HPP_CONNECTION
00050
00051 #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
00052 # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_MAJOR 5
00053 # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_MINOR 1
00054 # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_REVISION 3
00055 # define INETSTL_VER_INETSTL_NETWORK_HPP_CONNECTION_EDIT 67
00056 #endif
00057
00058
00059
00060
00061
00062 #ifndef INETSTL_INCL_INETSTL_H_INETSTL
00063 # include <inetstl/inetstl.h>
00064 #endif
00065 #ifndef INETSTL_OS_IS_WINDOWS
00066 # error This file is currently compatible only with the Win32/Win64 API
00067 #endif
00068 #ifndef INETSTL_INCL_INETSTL_NETWORK_HPP_SESSION
00069 # include <inetstl/network/session.hpp>
00070 #endif
00071 #ifndef INETSTL_INCL_INETSTL_FILESYSTEM_HPP_FILESYSTEM_TRAITS
00072 # include <inetstl/filesystem/filesystem_traits.hpp>
00073 #endif
00074 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00075 # ifndef INETSTL_INCL_INETSTL_ERROR_HPP_EXCEPTIONS
00076 # include <inetstl/error/exceptions.hpp>
00077 # endif
00078 #else
00079 # ifndef STLSOFT_INCL_STLSOFT_ERROR_HPP_EXCEPTIONS
00080 # include <stlsoft/error/exceptions.hpp>
00081 # endif
00082 #endif
00083
00084
00085
00086
00087
00088 #ifndef _INETSTL_NO_NAMESPACE
00089 # if defined(_STLSOFT_NO_NAMESPACE) || \
00090 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00091
00092 namespace inetstl
00093 {
00094 # else
00095
00096
00097 namespace stlsoft
00098 {
00099
00100 namespace inetstl_project
00101 {
00102
00103 # endif
00104 #endif
00105
00106
00107
00108
00109
00115 template< ss_typename_param_k C
00116 #ifdef STLSOFT_CF_EXCEPTION_SUPPORT
00117 , ss_typename_param_k X = throw_internet_exception_policy
00118 #else
00119 , ss_typename_param_k X = stlsoft_ns_qual(null_exception_policy)
00120 #endif
00121 , ss_typename_param_k T = filesystem_traits<C>
00122 >
00123 class basic_connection
00124 {
00125 public:
00126 typedef C char_type;
00127 typedef X exception_policy_type;
00128 typedef ss_typename_param_k exception_policy_type::thrown_type thrown_type;
00129 typedef T traits_type;
00130 typedef basic_connection<C, X, T> class_type;
00131
00132 typedef HINTERNET resource_type;
00133
00134 public:
00138 basic_connection();
00139
00154 ss_explicit_k basic_connection( HINTERNET hsess
00155 , char_type const* server
00156 , INTERNET_PORT port
00157 , char_type const* userName
00158 , char_type const* password
00159 , is_dword_t service
00160 , is_dword_t flags
00161 , is_dword_t context = 0);
00163 ~basic_connection() stlsoft_throw_0();
00164
00165
00166 public:
00181 is_bool_t connect( HINTERNET hsess
00182 , char_type const* server
00183 , INTERNET_PORT port
00184 , char_type const* userName
00185 , char_type const* password
00186 , is_dword_t service
00187 , is_dword_t flags
00188 , is_dword_t context = 0);
00190 void close();
00193 HINTERNET detach();
00194
00195
00196 public:
00198 is_bool_t is_connected() const;
00200 is_dword_t last_error() const;
00201
00203 HINTERNET get() const;
00204
00208 operator HINTERNET ();
00209
00210
00211 private:
00212 static char_type const *null_string_();
00213
00214
00215 private:
00216 HINTERNET m_hConn;
00217 is_dword_t m_lastError;
00218 };
00219
00220
00221
00222
00223
00229 typedef basic_connection<is_char_a_t> connection_a;
00235 typedef basic_connection<is_char_w_t> connection_w;
00241 typedef basic_connection<TCHAR> connection;
00242
00243
00244
00245 template< ss_typename_param_k C
00246 , ss_typename_param_k X
00247 , ss_typename_param_k T
00248 >
00249 inline ss_typename_type_ret_k basic_connection<C, X, T>::char_type const* basic_connection<C, X, T>::null_string_()
00250 {
00251 static char_type s_null[1] = { 0 };
00252
00253 return s_null;
00254 }
00255
00256 template< ss_typename_param_k C
00257 , ss_typename_param_k X
00258 , ss_typename_param_k T
00259 >
00260 inline basic_connection<C, X, T>::basic_connection()
00261 : m_hConn(NULL)
00262 , m_lastError(ERROR_SUCCESS)
00263 {}
00264
00265 template< ss_typename_param_k C
00266 , ss_typename_param_k X
00267 , ss_typename_param_k T
00268 >
00269 inline basic_connection<C, X, T>::basic_connection( HINTERNET hsess
00270 , char_type const* server
00271 , INTERNET_PORT port
00272 , char_type const* userName
00273 , char_type const* password
00274 , is_dword_t service
00275 , is_dword_t flags
00276 , is_dword_t context )
00277 : m_hConn(traits_type::internet_connect(hsess, server, port, userName, password, service, flags, context))
00278 , m_lastError(::GetLastError())
00279 {
00280 if(NULL == m_hConn)
00281 {
00282 exception_policy_type()("Failed to open connection", m_lastError);
00283 }
00284 }
00285
00286 template< ss_typename_param_k C
00287 , ss_typename_param_k X
00288 , ss_typename_param_k T
00289 >
00290 inline basic_connection<C, X, T>::~basic_connection() stlsoft_throw_0()
00291 {
00292 if(NULL != m_hConn)
00293 {
00294 traits_type::close_connection(m_hConn);
00295 }
00296 }
00297
00298 template< ss_typename_param_k C
00299 , ss_typename_param_k X
00300 , ss_typename_param_k T
00301 >
00302 inline is_bool_t basic_connection<C, X, T>::connect(HINTERNET hsess
00303 , char_type const* server
00304 , INTERNET_PORT port
00305 , char_type const* userName
00306 , char_type const* password
00307 , is_dword_t service
00308 , is_dword_t flags
00309 , is_dword_t context )
00310 {
00311 is_bool_t bRet;
00312
00313 if(is_connected())
00314 {
00315 bRet = false;
00316 }
00317 else
00318 {
00319 m_hConn = traits_type::internet_connect(hsess, server, port, userName, password, service, flags, context);
00320 m_lastError = ::GetLastError();
00321
00322 if(NULL == m_hConn)
00323 {
00324 exception_policy_type()("Failed to open connection", m_lastError);
00325
00326 bRet = false;
00327 }
00328 else
00329 {
00330 bRet = true;
00331 }
00332 }
00333
00334 return bRet;
00335 }
00336
00337 template< ss_typename_param_k C
00338 , ss_typename_param_k X
00339 , ss_typename_param_k T
00340 >
00341 inline void basic_connection<C, X, T>::close()
00342 {
00343 if(NULL != m_hConn)
00344 {
00345 traits_type::close_connection(m_hConn);
00346
00347 m_hConn = NULL;
00348 }
00349 }
00350
00351 template< ss_typename_param_k C
00352 , ss_typename_param_k X
00353 , ss_typename_param_k T
00354 >
00355 inline HINTERNET basic_connection<C, X, T>::detach()
00356 {
00357 INETSTL_MESSAGE_ASSERT("Attempting to detach from an empty connection", is_connected());
00358
00359 HINTERNET hConn = m_hConn;
00360
00361 m_hConn = NULL;
00362
00363 return hConn;
00364 }
00365
00366 template< ss_typename_param_k C
00367 , ss_typename_param_k X
00368 , ss_typename_param_k T
00369 >
00370 inline is_bool_t basic_connection<C, X, T>::is_connected() const
00371 {
00372 return NULL != m_hConn;
00373 }
00374
00375 template< ss_typename_param_k C
00376 , ss_typename_param_k X
00377 , ss_typename_param_k T
00378 >
00379 inline is_dword_t basic_connection<C, X, T>::last_error() const
00380 {
00381 return m_lastError;
00382 }
00383
00384 template< ss_typename_param_k C
00385 , ss_typename_param_k X
00386 , ss_typename_param_k T
00387 >
00388 inline HINTERNET basic_connection<C, X, T>::get() const
00389 {
00390 return m_hConn;
00391 }
00392
00393 template< ss_typename_param_k C
00394 , ss_typename_param_k X
00395 , ss_typename_param_k T
00396 >
00397 inline basic_connection<C, X, T>::operator HINTERNET ()
00398 {
00399 return m_hConn;
00400 }
00401
00402
00403
00404 template< ss_typename_param_k C
00405 , ss_typename_param_k X
00406 , ss_typename_param_k T
00407 >
00408 inline HINTERNET get_handle(basic_connection<C, X, T> &s)
00409 {
00410 return s;
00411 }
00412
00413
00414
00415 #ifndef _INETSTL_NO_NAMESPACE
00416 # if defined(_STLSOFT_NO_NAMESPACE) || \
00417 defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
00418 }
00419 # else
00420 }
00421 }
00422 # endif
00423 #endif
00424
00425
00426
00427 #endif
00428
00429