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_connection.h

Go to the documentation of this file.
00001 /* 
00002  * File:        inetstl_connection.h (formerly MIntConn.h)
00003  *
00004  * Purpose:     Contains the basic_connection 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_CONNECTION
00046 #define INETSTL_INCL_H_INETSTL_CONNECTION
00047 
00048 #ifndef __STLSOFT_DOCUMENTATION_SKIP_SECTION
00049 # define INETSTL_VER_H_INETSTL_CONNECTION_MAJOR     3
00050 # define INETSTL_VER_H_INETSTL_CONNECTION_MINOR     0
00051 # define INETSTL_VER_H_INETSTL_CONNECTION_REVISION  1
00052 # define INETSTL_VER_H_INETSTL_CONNECTION_EDIT      44
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_SESSION
00063 # include "inetstl_session.h"               // session
00064 #endif /* !INETSTL_INCL_H_INETSTL_SESSION */
00065 #ifndef INETSTL_INCL_H_INETSTL_FILESYSTEM_TRAITS
00066 # include "inetstl_filesystem_traits.h"     // filesystem_traits
00067 #endif /* !INETSTL_INCL_H_INETSTL_FILESYSTEM_TRAITS */
00068 #ifdef __STLSOFT_CF_EXCEPTION_SUPPORT
00069 # ifndef INETSTL_INCL_H_INETSTL_EXCEPTIONS
00070 #  include "inetstl_exceptions.h"           // throw_internet_exception_policy
00071 # endif /* !INETSTL_INCL_H_INETSTL_EXCEPTIONS */
00072 #else /* ? __STLSOFT_CF_EXCEPTION_SUPPORT */
00073 # ifndef STLSOFT_INCL_H_STLSOFT_EXCEPTIONS
00074 #  include "stlsoft_exceptions.h"           // stlsoft::null_exception_policy
00075 # endif /* !STLSOFT_INCL_H_STLSOFT_EXCEPTIONS */
00076 #endif /* __STLSOFT_CF_EXCEPTION_SUPPORT */
00077 
00078 /* 
00079  * Namespace
00080  */
00081 
00082 #ifndef _INETSTL_NO_NAMESPACE
00083 # if defined(_STLSOFT_NO_NAMESPACE) || \
00084      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00085 /* There is no stlsoft namespace, so must define ::inetstl */
00086 namespace inetstl
00087 {
00088 # else
00089 /* Define stlsoft::inetstl_project */
00090 
00091 namespace stlsoft
00092 {
00093 
00094 namespace inetstl_project
00095 {
00096 
00097 # endif /* _STLSOFT_NO_NAMESPACE */
00098 #endif /* !_INETSTL_NO_NAMESPACE */
00099 
00100 /* 
00101 
00104 
00108 
00113 
00114 /* 
00115  * Classes
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 /* ? __STLSOFT_CF_EXCEPTION_SUPPORT */
00123         ,   ss_typename_param_k X   =   stlsoft_ns_qual(null_exception_policy)
00124 #endif /* __STLSOFT_CF_EXCEPTION_SUPPORT */
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 // Construction
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 // Operations
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 // State
00199 public:
00201     is_bool_t   is_connected() const;
00203     is_dword_t  last_error() const;
00204 
00206     operator HINTERNET ();
00207 
00208 // Implementation
00209 private:
00210     static char_type const  *null_string_();
00211 
00212 // Members
00213 private:
00214     HINTERNET   m_hConn;
00215     is_dword_t  m_lastError;
00216 };
00217 
00218 /* 
00219  * Typedefs for commonly encountered types
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 template<   ss_typename_param_k C
00232         ,   ss_typename_param_k X
00233         ,   ss_typename_param_k T
00234         >
00235 inline /* static */ 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 /* = 0 */)
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 /* = 0 */)
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 template<   ss_typename_param_k C
00382         ,   ss_typename_param_k X
00383         ,   ss_typename_param_k T
00384         >
00385 inline HINTERNET get_handle(basic_connection<C, X, T> &s)
00386 {
00387     return s;
00388 }
00389 
00390 /* 
00391 
00393 
00394 /* 
00395 
00396 #ifndef _INETSTL_NO_NAMESPACE
00397 # if defined(_STLSOFT_NO_NAMESPACE) || \
00398      defined(__STLSOFT_DOCUMENTATION_SKIP_SECTION)
00399 } // namespace inetstl
00400 # else
00401 } // namespace inetstl_project
00402 } // namespace stlsoft
00403 # endif /* _STLSOFT_NO_NAMESPACE */
00404 #endif /* !_INETSTL_NO_NAMESPACE */
00405 
00406 /* 
00407 
00408 #endif /* INETSTL_INCL_H_INETSTL_CONNECTION */
00409 
00410 /* 

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