STLSoft - ... Robust, Lightweight, Cross-platform, Template Software ... ATLSTL - Template Software for the Active Template Library COMSTL - The Standard Template Library meets the Component Object Model .netSTL - Standard Template Library meets the Microsoft.NET Common Language Runtime InetSTL - The Standard Template Library meets WinInet MFCSTL - Template Software for the Microsoft Foundation Classes UNIXSTL - Template Software for the UNIX Operating System WinSTL - where the Standard Template Library meets the Win32 API

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

inetstl_session.h

Go to the documentation of this file.
00001 /* 
00002  * File:        inetstl_session.h (formerly MIntSess.h)
00003  *
00004  * Purpose:     Contains the basic_session class.
00005  *
00006  * Created:     30th April 1999
00007  * Updated:     11th September 2004
00008  *
00009  * Home:        http://stlsoft.org/
00010  *
00011  * Copyright (c) 1999-2004, Matthew Wilson and Synesis Software
00012  * All rights reserved.
00013  *
00014  * Redistribution and use in source and binary forms, with or without
00015  * modification, are permitted provided that the following conditions are met:
00016  *
00017  * - Redistributions of source code must retain the above copyright notice, this
00018  *   list of conditions and the following disclaimer.
00019  * - Redistributions in binary form must reproduce the above copyright notice,
00020  *   this list of conditions and the following disclaimer in the documentation
00021  *   and/or other materials provided with the distribution.
00022  * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
00023  *   any contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00029  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00030  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00031  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00032  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00033  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00034  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00035  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00036  * POSSIBILITY OF SUCH DAMAGE.
00037  *
00038  * 
00039 
00040 
00044 
00045 #ifndef INETSTL_INCL_H_INETSTL_SESSION
00046 #define INETSTL_INCL_H_INETSTL_SESSION
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define INETSTL_VER_H_INETSTL_SESSION_MAJOR        3
00050 # define INETSTL_VER_H_INETSTL_SESSION_MINOR        0
00051 # define INETSTL_VER_H_INETSTL_SESSION_REVISION     1
00052 # define INETSTL_VER_H_INETSTL_SESSION_EDIT         38
00053 #endif /* !__STLSOFT_DOCUMENTATION_SKIP_SECTION */
00054 
00055 /* 
00056  * Includes
00057  */
00058 
00059 #ifndef INETSTL_INCL_H_INETSTL
00060 # include "inetstl.h"                        // Include the InetSTL root header
00061 #endif /* !INETSTL_INCL_H_INETSTL */
00062 #ifndef INETSTL_INCL_H_INETSTL_FILESYSTEM_TRAITS
00063 # include "inetstl_filesystem_traits.h"     // filesystem_traits
00064 #endif /* !INETSTL_INCL_H_INETSTL_FILESYSTEM_TRAITS */
00065 #ifdef __STLSOFT_CF_EXCEPTION_SUPPORT
00066 # ifndef INETSTL_INCL_H_INETSTL_EXCEPTIONS
00067 #  include "inetstl_exceptions.h"           // throw_internet_exception_policy
00068 # endif /* !INETSTL_INCL_H_INETSTL_EXCEPTIONS */
00069 #else /* ? __STLSOFT_CF_EXCEPTION_SUPPORT */
00070 # ifndef STLSOFT_INCL_H_STLSOFT_EXCEPTIONS
00071 #  include "stlsoft_exceptions.h"           // stlsoft::null_exception_policy
00072 # endif /* !STLSOFT_INCL_H_STLSOFT_EXCEPTIONS */
00073 #endif /* __STLSOFT_CF_EXCEPTION_SUPPORT */
00074 
00075 /* 
00076  * Namespace
00077  */
00078 
00079 #ifndef _INETSTL_NO_NAMESPACE
00080 # if defined(_STLSOFT_NO_NAMESPACE) || \
00081      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00082 /* There is no stlsoft namespace, so must define ::inetstl */
00083 namespace inetstl
00084 {
00085 # else
00086 /* Define stlsoft::inetstl_project */
00087 
00088 namespace stlsoft
00089 {
00090 
00091 namespace inetstl_project
00092 {
00093 
00094 # endif /* _STLSOFT_NO_NAMESPACE */
00095 #endif /* !_INETSTL_NO_NAMESPACE */
00096 
00097 /* 
00098 
00101 
00105 
00110 
00111 /* 
00112  * Classes
00113  */
00114 
00118 template<   ss_typename_param_k C
00119 #ifdef __STLSOFT_CF_EXCEPTION_SUPPORT
00120         ,   ss_typename_param_k X   =   throw_internet_exception_policy
00121 #else /* ? __STLSOFT_CF_EXCEPTION_SUPPORT */
00122         ,   ss_typename_param_k X   =   stlsoft_ns_qual(null_exception_policy)
00123 #endif /* __STLSOFT_CF_EXCEPTION_SUPPORT */
00124         ,   ss_typename_param_k T   =   filesystem_traits<C>
00125         >
00126 class basic_session
00127 {
00128 public:
00129     typedef C                                                       char_type;
00130     typedef X                                                       exception_policy_type;
00131     typedef ss_typename_param_k exception_policy_type::thrown_type  thrown_type;
00132     typedef T                                                       traits_type;
00133     typedef basic_session<C, X, T>                                  class_type;
00134 
00135 // Construction
00136 public:
00144     basic_session();
00156     ss_explicit_k basic_session(    char_type const *pcszAgent
00157                                 ,   is_dword_t      accessType          =   INTERNET_OPEN_TYPE_PRECONFIG
00158                                 ,   char_type const *pcszProxyName      =   NULL
00159                                 ,   char_type const *pcszProxyBypass    =   NULL
00160                                 ,   is_dword_t      flags               =   0);
00162     ~basic_session();
00163 
00164 // Operations
00165 public:
00177     is_bool_t    open(  char_type const *pcszAgent
00178                     ,   is_dword_t      accessType          =   INTERNET_OPEN_TYPE_PRECONFIG
00179                     ,   char_type const *pcszProxyName      =   NULL
00180                     ,   char_type const *pcszProxyBypass    =   NULL
00181                     ,   is_dword_t      flags               =   0);
00183     void        close();
00186     HINTERNET   detach();
00187 
00188 // State
00189 public:
00191     is_bool_t   is_open() const;
00192 
00194     operator HINTERNET ();
00195 
00196 // Implementation
00197 private:
00198     static char_type const  *null_string_();
00199 
00200 // Members
00201 private:
00202     HINTERNET   m_hConn;
00203 
00204 // Not to be implemented
00205 private:
00206     basic_session(class_type const&);
00207     class_type &operator =(class_type const&);
00208 };
00209 
00210 /* 
00211  * Typedefs for commonly encountered types
00212  */
00213 
00215 typedef basic_session<is_char_a_t>  session_a;
00217 typedef basic_session<is_char_w_t>  session_w;
00219 typedef basic_session<TCHAR>        session;
00220 
00221 /* 
00222 
00223 template<   ss_typename_param_k C
00224         ,   ss_typename_param_k X
00225         ,   ss_typename_param_k T
00226         >
00227 inline /* static */ ss_typename_type_k basic_session<C, X, T>::char_type const *basic_session<C, X, T>::null_string_()
00228 {
00229     static char_type    s_null[1] = { 0 };
00230 
00231     return s_null;
00232 }
00233 
00234 template<   ss_typename_param_k C
00235         ,   ss_typename_param_k X
00236         ,   ss_typename_param_k T
00237         >
00238 inline basic_session<C, X, T>::basic_session()
00239     : m_hConn(traits_type::internet_open(null_string_(), INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0))
00240 {
00241     if(NULL == m_hConn)
00242     {
00243         exception_policy_type()(::GetLastError());
00244     }
00245 }
00246 
00247 template<   ss_typename_param_k C
00248         ,   ss_typename_param_k X
00249         ,   ss_typename_param_k T
00250         >
00251 inline basic_session<C, X, T>::basic_session(   char_type const *pcszAgent
00252                                             ,   is_dword_t      accessType
00253                                             ,   char_type const *pcszProxyName
00254                                             ,   char_type const *pcszProxyBypass
00255                                             ,   is_dword_t      flags)
00256     : m_hConn(traits_type::internet_open(pcszAgent, accessType, pcszProxyName, pcszProxyBypass, flags))
00257 {
00258     if(NULL == m_hConn)
00259     {
00260         exception_policy_type()(::GetLastError());
00261     }
00262 }
00263 
00264 template<   ss_typename_param_k C
00265         ,   ss_typename_param_k X
00266         ,   ss_typename_param_k T
00267         >
00268 inline basic_session<C, X, T>::~basic_session()
00269 {
00270     if(m_hConn != NULL)
00271     {
00272         ::InternetCloseHandle(m_hConn);
00273     }
00274 }
00275 
00276 template<   ss_typename_param_k C
00277         ,   ss_typename_param_k X
00278         ,   ss_typename_param_k T
00279         >
00280 inline is_bool_t basic_session<C, X, T>::open(  char_type const *pcszAgent
00281                                             ,   is_dword_t      accessType
00282                                             ,   char_type const *pcszProxyName
00283                                             ,   char_type const *pcszProxyBypass
00284                                             ,   is_dword_t      flags)
00285 {
00286     is_bool_t    bRet;
00287 
00288     if(is_open())
00289     {
00290         bRet = false;
00291     }
00292     else
00293     {
00294         m_hConn = traits_type::internet_open(pcszAgent, accessType, pcszProxyName, pcszProxyBypass, flags);
00295 
00296         if(NULL == m_hConn)
00297         {
00298             exception_policy_type()(::GetLastError());
00299 
00300             bRet = false;
00301         }
00302         else
00303         {
00304             bRet = true;
00305         }
00306     }
00307 
00308     return bRet;
00309 }
00310 
00311 template<   ss_typename_param_k C
00312         ,   ss_typename_param_k X
00313         ,   ss_typename_param_k T
00314         >
00315 inline void basic_session<C, X, T>::close()
00316 {
00317     if(m_hConn != NULL)
00318     {
00319         ::InternetCloseHandle(m_hConn);
00320 
00321         m_hConn = NULL;
00322     }
00323 }
00324 
00325 template<   ss_typename_param_k C
00326         ,   ss_typename_param_k X
00327         ,   ss_typename_param_k T
00328         >
00329 inline HINTERNET basic_session<C, X, T>::detach()
00330 {
00331     inetstl_message_assert("Attempting to detach from an empty session", is_open());
00332 
00333     HINTERNET   hConn   =   m_hConn;
00334 
00335     m_hConn = NULL;
00336 
00337     return hConn;
00338 }
00339 
00340 template<   ss_typename_param_k C
00341         ,   ss_typename_param_k X
00342         ,   ss_typename_param_k T
00343         >
00344 inline is_bool_t basic_session<C, X, T>::is_open() const
00345 {
00346     return m_hConn != NULL;
00347 }
00348 
00349 template<   ss_typename_param_k C
00350         ,   ss_typename_param_k X
00351         ,   ss_typename_param_k T
00352         >
00353 inline basic_session<C, X, T>::operator HINTERNET ()
00354 {
00355     return m_hConn;
00356 }
00357 
00358 /* 
00359 
00360 template<   ss_typename_param_k C
00361         ,   ss_typename_param_k X
00362         ,   ss_typename_param_k T
00363         >
00364 inline HINTERNET get_handle(basic_session<C, X, T> &s)
00365 {
00366     return s;
00367 }
00368 
00369 /* 
00370 
00372 
00373 /* 
00374 
00375 #ifndef _INETSTL_NO_NAMESPACE
00376 # if defined(_STLSOFT_NO_NAMESPACE) || \
00377      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00378 } // namespace inetstl
00379 # else
00380 } // namespace inetstl_project
00381 } // namespace stlsoft
00382 # endif /* _STLSOFT_NO_NAMESPACE */
00383 #endif /* !_INETSTL_NO_NAMESPACE */
00384 
00385 /* 
00386 
00387 #endif /* INETSTL_INCL_H_INETSTL_SESSION */
00388 
00389 /* 

STLSoft Libraries documentation © Synesis Software Pty Ltd, 2001-2004