diff options
author | lloyd <[email protected]> | 2006-05-18 18:33:19 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2006-05-18 18:33:19 +0000 |
commit | a2c99d3270eb73ef2db5704fc54356c6b75096f8 (patch) | |
tree | ad3d6c4fcc8dd0f403f8105598943616246fe172 /modules/eng_aep/aep_conn.cpp |
Initial checkin1.5.6
Diffstat (limited to 'modules/eng_aep/aep_conn.cpp')
-rw-r--r-- | modules/eng_aep/aep_conn.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/modules/eng_aep/aep_conn.cpp b/modules/eng_aep/aep_conn.cpp new file mode 100644 index 000000000..9293f45fc --- /dev/null +++ b/modules/eng_aep/aep_conn.cpp @@ -0,0 +1,92 @@ +/************************************************* +* AEP Connection Management Source File * +* (C) 1999-2006 The Botan Project * +*************************************************/ + +#include <botan/aep_conn.h> +#include <botan/libstate.h> +#include <botan/parsing.h> +#include <botan/hw_aep.h> + +namespace Botan { + +/************************************************* +* Persistent connection pool * +*************************************************/ +std::vector<AEP_Connection::Connection_Info> AEP_Connection::pool; +Mutex* AEP_Connection::guard = 0; + +/************************************************* +* Close all currently open connections * +*************************************************/ +void AEP_Connection::close_all_connections() + { + guard->lock(); + for(u32bit j = 0; j != pool.size(); j++) + AEP::AEP_CloseConnection(pool[j].id); + pool.clear(); + guard->unlock(); + delete guard; + guard = 0; + } + +/************************************************* +* Get a new connection handle * +*************************************************/ +AEP_Connection::AEP_Connection() + { + // FIXME: race condition + if(!guard) + guard = global_state().get_mutex(); + + Mutex_Holder lock(guard); + + this_connection = 0; + + for(u32bit j = 0; j != pool.size(); j++) + { + if(pool[j].in_use) + continue; + + pool[j].in_use = true; + this_connection = pool[j].id; + } + + if(this_connection == 0) + { + Connection_Info new_conn; + + u32bit retval = AEP::AEP_OpenConnection(&new_conn.id); + if(retval != 0) + throw Stream_IO_Error("AEP_OpenConnection failed"); + new_conn.in_use = true; + + if(pool.size() < MAX_CACHED_CONNECTIONS) + pool.push_back(new_conn); + + this_connection = new_conn.id; + } + } + +/************************************************* +* Free a connection handle * +*************************************************/ +AEP_Connection::~AEP_Connection() + { + Mutex_Holder lock(guard); + + for(u32bit j = 0; j != pool.size(); j++) + { + if(pool[j].id != this_connection) + continue; + + pool[j].in_use = false; + return; + } + + int retval = AEP::AEP_CloseConnection(this_connection); + if(retval != 0) + throw Exception("AEP_CloseConnection returned " + to_string(retval)); + } + +} |