00001 // -*- Socket Streams -*- 00002 00003 // Copyright (C) 2003, 2004, 2005 Pedro LamarĂ£o <pedro.lamarao@mndfck.org> 00004 00005 // This file is part of the Socket Streams Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU Lesser General Public License as published by the 00008 // Free Software Foundation; either version 2.1 of the License 00009 // or (at your option) any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU Lesser General Public License for more details. 00015 00016 // You should have received a copy of the GNU Lesser General Public License along 00017 // with this library; see the file COPYING. If not, write to the Free 00018 // Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, 00019 // USA 00020 00025 #ifndef SOCKETSTREAM_BASIC_LISTENER_H 00026 #define SOCKETSTREAM_BASIC_LISTENER_H 1 00027 00028 #include <socketstream/config.h> 00029 00030 #include <memory> 00031 00032 #include <socketstream/sockets_base.h> 00033 #include <socketstream/basic_iosocketstream.h> 00034 00035 namespace ss { 00042 template <typename _CharT, typename _Traits> 00043 class basic_listener { 00044 public: 00045 00047 00054 typedef _CharT char_type; 00055 typedef _Traits traits_type; 00056 00057 typedef typename traits_type::int_type int_type; 00058 typedef typename traits_type::pos_type pos_type; 00059 typedef typename traits_type::off_type off_type; 00061 00062 typedef basic_socketstream<char_type, traits_type> socketstream_type; 00063 00067 basic_listener () 00068 : _M_socket(sockets_base::def::family, sockets_base::stream) 00069 { } 00070 00074 basic_listener (sockets_base::family __f) 00075 : _M_socket(__f, sockets_base::stream) 00076 { } 00077 00081 ~basic_listener () 00082 { } 00083 00089 template <typename AddressT> 00090 void 00091 bind (AddressT& __addr) 00092 { _M_socket.bind(&__addr, sizeof(__addr)); } 00093 00099 void 00100 listen (int __queue = 5) 00101 { _M_socket.listen(__queue); } 00102 00108 std::auto_ptr<socketstream_type> 00109 accept () 00110 { 00111 try 00112 { 00113 basic_socket::descriptor_t __s = _M_socket.accept(); 00114 return std::auto_ptr<socketstream_type>(new socketstream_type(__s)); 00115 } 00116 catch (const socket_exception& se) 00117 { 00118 switch (se.code()) 00119 { 00120 case EWOULDBLOCK: 00121 return std::auto_ptr<socketstream_type>(0); 00122 case EINTR: 00123 default: 00124 throw(se); 00125 } 00126 } 00127 } 00128 00129 protected: 00130 basic_socket _M_socket; 00131 }; 00132 00133 typedef basic_listener<char, std::char_traits<char> > listener; 00134 00135 } 00136 00137 #endif // SOCKETSTREAM_BASIC_LISTENER_H 00138