X-Git-Url: http://developer.intra2net.com/git/?p=libt2n;a=blobdiff_plain;f=src%2Fsocket_wrapper.cpp;fp=src%2Fsocket_wrapper.cpp;h=1ebc00cec374ceb8bc1a74bb232b00d2f1715338;hp=0000000000000000000000000000000000000000;hb=ffbbf9abeb195a4017c1ede383cc9ab906aa4a0c;hpb=e98b5dc1fe10ed25b986fc5cfb8fd0ba2f329a3a diff --git a/src/socket_wrapper.cpp b/src/socket_wrapper.cpp new file mode 100644 index 0000000..1ebc00c --- /dev/null +++ b/src/socket_wrapper.cpp @@ -0,0 +1,121 @@ +/*************************************************************************** + * Copyright (C) 2008 by Gerd v. Egidy * + * gve@intra2net.com * + * * + * This library is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Lesser General Public License version * + * 2.1 as published by the Free Software Foundation. * + * * + * This library is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU Lesser General Public License for more details. * + * * + * You should have received a copy of the GNU Lesser General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include +#include + +#include + +namespace libt2n +{ + +client_connection* BasicSocketWrapper::get_connection(void) +{ + if (c.get() == NULL) + { + if (socket_type == tcp_s) + c=std::auto_ptr + (new socket_client_connection(port,server,connect_timeout_usec,max_retries)); + else if (socket_type == unix_s) + c=std::auto_ptr + (new socket_client_connection(path,connect_timeout_usec,max_retries)); + } + + return c.get(); +} + +void ReconnectSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) +{ + int tries=0; + + while(true) + { + try + { + // we always reconnect if something went wrong before: + // makes sure the buffers are clean of half-sent packets etc. + if (tries > 0) + c->reconnect(); + + f(); + // we were successful + return; + } + catch(t2n_connect_error &e) + { + // reconnect already tries max_tries times to reconnect: we are done if that failed + throw(e); + } + catch(t2n_communication_error &e) + { + // aborts the loop with an exception after max_retries iterations + // retries means that the first try doesn't count: use > + if (tries > max_retries) + throw(e); + + // otherwise ignore the exception and reconnect in the next iteration + } + catch(...) + { + throw; + } + + tries++; + } +} + +client_connection* ReconnectIgnoreFailureSocketWrapper::get_connection(void) +{ + client_connection* tmp; + + try + { + tmp=BasicSocketWrapper::get_connection(); + } + catch(t2n_connect_error &e) + { + // TODO: return some kind of dummy connection + } + catch(...) + { + throw; + } + + return tmp; +} + +void ReconnectIgnoreFailureSocketWrapper::handle(command_client* stubBase, boost::function< void() > f) +{ + // TODO: check for dummy connection and try to establish a real one + + try + { + ReconnectSocketWrapper::handle(stubBase,f); + } + catch(t2n_communication_error &e) + { + // ignore + } + catch(...) + { + throw; + } +} + +}