debian/libconexus-dev/usr/include/conexus-0.5/conexus/socket.h

00001 /***************************************************************************
00002  *   Copyright (C) 2001 by Rick L. Vinyard, Jr.                            *
00003  *   rvinyard@cs.nmsu.edu                                                  *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation version 2.1.                *
00008  *                                                                         *
00009  *   This program is distributed in the hope that it will be useful,       *
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00012  *   GNU General Public License for more details.                          *
00013  *                                                                         *
00014  *   You should have received a copy of the GNU Lesser General Public      *
00015  *   License along with this library; if not, write to the                 *
00016  *   Free Software Foundation, Inc.,                                       *
00017  *   51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA              *
00018  ***************************************************************************/
00019 #ifndef CONEXUSSOCKET_H
00020 #define CONEXUSSOCKET_H
00021 
00022 #include <sys/types.h>
00023 #include <utility>
00024 
00025 #include <conexus/enums.h>
00026 #include <conexus/filedescriptor.h>
00027 #include <conexus/address.h>
00028 #include <conexus/except.h>
00029 
00030 #include <iostream>
00031 
00036 namespace Conexus
00037   {
00038 
00051   class Socket: public FileDescriptor
00052     {
00053     public:
00054 
00055       typedef ConexusPointer<Socket> pointer;
00056 
00060       Socket(int domain=-1, int type=-1, int protocol=0) throw ();
00061 
00062       virtual ~Socket() throw ();
00063 
00074       virtual void open() throw (open_exception);
00075 
00080       virtual void close(bool force=false) throw (close_exception);
00081 
00087       virtual void bind() throw (bind_exception);
00088 
00096       virtual void bind(Conexus::Address& a) throw (bind_exception);
00097 
00103       virtual void connect() throw (connect_exception);
00104 
00112       virtual void connect(Address& a) throw (connect_exception);
00113 
00121       virtual void listen(int backlog=0);
00122 
00126       int domain() throw ();
00127 
00149       void set_domain(int) throw ();
00150 
00154       int type() throw ();
00155 
00182       void set_type(int) throw ();
00183 
00187       int protocol() throw ();
00188 
00193       void set_protocol(int) throw ();
00194 
00195       virtual ssize_t writeto(Address& a, Data::const_pointer data) throw (write_exception);
00196 
00197       virtual void set_option(int option, bool b);
00198 
00199       template <typename T>
00200       void set_option(int level, int optname, T& value);
00201 
00202       template <typename T>
00203       void option(int level, int optname, T& value);
00204 
00205       virtual void change_state(long states) throw (state_exception);
00206 
00207       sigc::signal<void> signal_bound();
00208 
00209       sigc::signal<void> signal_connected();
00210 
00211       sigc::signal<void> signal_listening();
00212 
00213       bool is_bound();
00214 
00215       bool is_connected();
00216 
00217       bool is_listening();
00218 
00219       bool is_accepted();
00220 
00221       virtual const std::string& object_type() { static std::string s("Conexus::Socket"); return s; }
00222 
00223     protected:
00224       int m_domain;
00225       int m_type;
00226       int m_protocol;
00227 
00228       virtual void read_thread_main();
00229 
00230       virtual void set_state_closed();
00231       virtual void set_state_bound();
00232       virtual void set_state_connected();
00233       virtual void set_state_listening();
00234 
00235       sigc::signal<void> m_signal_bound;
00236       sigc::signal<void> m_signal_connected;
00237       sigc::signal<void> m_signal_listening;
00238 
00239       virtual ssize_t write_data(long int timeout, Data::const_pointer data) throw (write_exception);
00240 
00241       virtual Data::pointer read_data(long int timeout, size_t s = 0) throw (read_exception);
00242 
00243     };
00244 
00245   template <typename T>
00246   inline
00247   void Socket::set_option(int level, int optname, T& value)
00248   {
00249     if ( ! is_bound() )
00250       try
00251         {
00252           change_state(SOCKET_BOUND);
00253         }
00254       catch (exception::state::failed)
00255         {
00256           return;
00257         }
00258 
00259         //TODO do something proper with result
00260 //     int result = ::setsockopt(m_fd, level, optname, &value, sizeof(T));
00261       ::setsockopt(m_fd, level, optname, &value, sizeof(T));
00262   }
00263 
00264   template <typename T>
00265   inline
00266   void Socket::option(int level, int optname, T& value)
00267   {
00268     if ( ! is_bound() )
00269       try
00270         {
00271           change_state(SOCKET_BOUND);
00272         }
00273       catch (exception::state::failed)
00274         {
00275           return;
00276         }
00277 
00278     socklen_t size = sizeof(T);
00279     ::getsockopt(m_fd, level, optname, &value, &size);
00280   }
00281 }
00282 
00283 #endif

Generated on Tue Mar 13 19:54:48 2007 by  doxygen 1.5.1