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
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #ifndef INETSTL_INCL_H_INETSTL
00060 # include "inetstl.h"
00061 #endif
00062 #ifndef INETSTL_INCL_H_INETSTL_SESSION
00063 # include "inetstl_session.h"
00064 #endif
00065 #ifndef INETSTL_INCL_H_INETSTL_FILESYSTEM_TRAITS
00066 # include "inetstl_filesystem_traits.h"
00067 #endif
00068 #ifdef __STLSOFT_CF_EXCEPTION_SUPPORT
00069 # ifndef INETSTL_INCL_H_INETSTL_EXCEPTIONS
00070 # include "inetstl_exceptions.h"
00071 # endif
00072 #else
00073 # ifndef STLSOFT_INCL_H_STLSOFT_EXCEPTIONS
00074 # include "stlsoft_exceptions.h"
00075 # endif
00076 #endif
00077
00078
00079
00080
00081
00082 #ifndef _INETSTL_NO_NAMESPACE
00083 # if defined(_STLSOFT_NO_NAMESPACE) || \
00084 defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00085
00086 namespace inetstl
00087 {
00088 # else
00089
00090
00091 namespace stlsoft
00092 {
00093
00094 namespace inetstl_project
00095 {
00096
00097 # endif
00098 #endif
00099
00100
00101
00104
00108
00113
00114
00115
00116
00117
00119 template< ss_typename_param_k C
00120 #ifdef __STLSOFT_CF_EXCEPTION_SUPPORT
00121 , ss_typename_param_k X = throw_internet_exception_policy
00122 #else
00123 , ss_typename_param_k X = stlsoft_ns_qual(null_exception_policy)
00124 #endif
00125 , ss_typename_param_k T = filesystem_traits<C>
00126 >
00127 class basic_connection
00128 {
00129 public:
00130 typedef C char_type;
00131 typedef X exception_policy_type;
00132 typedef ss_typename_param_k exception_policy_type::thrown_type thrown_type;
00133 typedef T traits_type;
00134 typedef basic_connection<C, X, T> class_type;
00135
00136
00137 public:
00141 basic_connection();
00142
00157 ss_explicit_k basic_connection( HINTERNET hsess
00158 , char_type const *server
00159 , INTERNET_PORT port
00160 , char_type const *userName
00161 , char_type const *password
00162 , is_dword_t service
00163 , is_dword_t flags
00164 , is_dword_t context = 0);
00166 ~basic_connection();
00167
00168
00169 public:
00184 is_bool_t connect( HINTERNET hsess
00185 , char_type const *server
00186 , INTERNET_PORT port
00187 , char_type const *userName
00188 , char_type const *password
00189 , is_dword_t service
00190 , is_dword_t flags
00191 , is_dword_t context = 0);
00193 void close();
00196 HINTERNET detach();
00197
00198
00199 public:
00201 is_bool_t is_connected() const;
00203 is_dword_t last_error() const;
00204
00206 operator HINTERNET ();
00207
00208
00209 private:
00210 static char_type const *null_string_();
00211
00212
00213 private:
00214 HINTERNET m_hConn;
00215 is_dword_t m_lastError;
00216 };
00217
00218
00219
00220
00221
00223 typedef basic_connection<is_char_a_t> connection_a;
00225 typedef basic_connection<is_char_w_t> connection_w;
00227 typedef basic_connection<TCHAR> connection;
00228
00229
00230
00231
00232
00233
00234
00235 ss_typename_type_k basic_connection<C, X, T>::char_type const *basic_connection<C, X, T>::null_string_()
00236 {
00237 static char_type s_null[1] = { 0 };
00238
00239 return s_null;
00240 }
00241
00242 template< ss_typename_param_k C
00243 , ss_typename_param_k X
00244 , ss_typename_param_k T
00245 >
00246 inline basic_connection<C, X, T>::basic_connection()
00247 : m_hConn(NULL)
00248 , m_lastError(ERROR_SUCCESS)
00249 {}
00250
00251 template< ss_typename_param_k C
00252 , ss_typename_param_k X
00253 , ss_typename_param_k T
00254 >
00255 inline basic_connection<C, X, T>::basic_connection( HINTERNET hsess
00256 , char_type const *server
00257 , INTERNET_PORT port
00258 , char_type const *userName
00259 , char_type const *password
00260 , is_dword_t service
00261 , is_dword_t flags
00262 , is_dword_t context )
00263 : m_hConn(traits_type::internet_connect(hsess, server, port, userName, password, service, flags, context))
00264 , m_lastError(::GetLastError())
00265 {
00266 if(NULL == m_hConn)
00267 {
00268 exception_policy_type()(m_lastError);
00269 }
00270 }
00271
00272 template< ss_typename_param_k C
00273 , ss_typename_param_k X
00274 , ss_typename_param_k T
00275 >
00276 inline basic_connection<C, X, T>::~basic_connection()
00277 {
00278 if(m_hConn != NULL)
00279 {
00280 traits_type::close_connection(m_hConn);
00281 }
00282 }
00283
00284 template< ss_typename_param_k C
00285 , ss_typename_param_k X
00286 , ss_typename_param_k T
00287 >
00288 inline is_bool_t basic_connection<C, X, T>::connect(HINTERNET hsess
00289 , char_type const *server
00290 , INTERNET_PORT port
00291 , char_type const *userName
00292 , char_type const *password
00293 , is_dword_t service
00294 , is_dword_t flags
00295 , is_dword_t context )
00296 {
00297 is_bool_t bRet;
00298
00299 if(is_connected())
00300 {
00301 bRet = false;
00302 }
00303 else
00304 {
00305 m_hConn = traits_type::internet_connect(hsess, server, port, userName, password, service, flags, context);
00306 m_lastError = ::GetLastError();
00307
00308 if(NULL == m_hConn)
00309 {
00310 exception_policy_type()(m_lastError);
00311
00312 bRet = false;
00313 }
00314 else
00315 {
00316 bRet = true;
00317 }
00318 }
00319
00320 return bRet;
00321 }
00322
00323 template< ss_typename_param_k C
00324 , ss_typename_param_k X
00325 , ss_typename_param_k T
00326 >
00327 inline void basic_connection<C, X, T>::close()
00328 {
00329 if(m_hConn != NULL)
00330 {
00331 traits_type::close_connection(m_hConn);
00332
00333 m_hConn = NULL;
00334 }
00335 }
00336
00337 template< ss_typename_param_k C
00338 , ss_typename_param_k X
00339 , ss_typename_param_k T
00340 >
00341 inline HINTERNET basic_connection<C, X, T>::detach()
00342 {
00343 inetstl_message_assert("Attempting to detach from an empty connection", is_connected());
00344
00345 HINTERNET hConn = m_hConn;
00346
00347 m_hConn = NULL;
00348
00349 return hConn;
00350 }
00351
00352 template< ss_typename_param_k C
00353 , ss_typename_param_k X
00354 , ss_typename_param_k T
00355 >
00356 inline is_bool_t basic_connection<C, X, T>::is_connected() const
00357 {
00358 return m_hConn != NULL;
00359 }
00360
00361 template< ss_typename_param_k C
00362 , ss_typename_param_k X
00363 , ss_typename_param_k T
00364 >
00365 inline is_dword_t basic_connection<C, X, T>::last_error() const
00366 {
00367 return m_lastError;
00368 }
00369
00370 template< ss_typename_param_k C
00371 , ss_typename_param_k X
00372 , ss_typename_param_k T
00373 >
00374 inline basic_connection<C, X, T>::operator HINTERNET ()
00375 {
00376 return m_hConn;
00377 }
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404 #endif
00405
00406
00407
00408
00409
00410