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     protected:
00054 
00058       Socket( int domain = -1, int type = -1, int protocol = 0 ) throw ();
00059 
00060     public:
00061 
00062       typedef ConexusPointer<Socket> pointer;
00063 
00064       static pointer create( int domain = -1, int type = -1, int protocol = 0 ) throw ();
00065 
00066       virtual ~Socket() throw ();
00067 
00078       virtual void open() throw ( open_exception );
00079 
00084       virtual void close( bool force = false ) throw ( close_exception );
00085 
00091       virtual void bind() throw ( bind_exception );
00092 
00100       virtual void bind( const Conexus::Address& a ) throw ( bind_exception );
00101 
00107       virtual void connect() throw ( connect_exception );
00108 
00116       virtual void connect( const Address& a ) throw ( connect_exception );
00117 
00125       virtual void listen( int backlog = 0 );
00126 
00130       int domain() throw ();
00131 
00153       void set_domain( int ) throw ();
00154 
00158       int type() throw ();
00159 
00186       void set_type( int ) throw ();
00187 
00191       int protocol() throw ();
00192 
00197       void set_protocol( int ) throw ();
00198 
00199       virtual ssize_t writeto( Address& a, const Data data ) throw ( write_exception );
00200 
00201       virtual void set_option( int option, bool b );
00202 
00203       template <typename T>
00204       void set_option( int level, int optname, T& value );
00205 
00206       template <typename T>
00207       void option( int level, int optname, T& value );
00208 
00209       virtual void change_state( long states ) throw ( state_exception );
00210 
00211       sigc::signal<void> signal_bound();
00212 
00213       sigc::signal<void> signal_connected();
00214 
00215       sigc::signal<void> signal_listening();
00216 
00217       bool is_bound();
00218 
00219       bool is_connected();
00220 
00221       bool is_listening();
00222 
00223       bool is_accepted();
00224 
00225     protected:
00226       int m_domain;
00227       int m_type;
00228       int m_protocol;
00229 
00230       virtual void read_thread_main();
00231 
00232       virtual void set_state_closed();
00233       virtual void set_state_bound();
00234       virtual void set_state_connected();
00235       virtual void set_state_listening();
00236 
00237       sigc::signal<void> m_signal_bound;
00238       sigc::signal<void> m_signal_connected;
00239       sigc::signal<void> m_signal_listening;
00240 
00241       virtual ssize_t write_data( long int timeout, const Data data ) throw ( write_exception );
00242 
00243       virtual Data read_data( long int timeout, size_t s = 0 ) throw ( read_exception );
00244 
00245   };
00246 
00247   template <typename T>
00248   inline
00249   void Socket::set_option( int level, int optname, T& value )
00250   {
00251     if ( ! is_bound() )
00252       try {
00253         change_state( SOCKET_BOUND );
00254       } catch ( exception::state::failed ) {
00255         return ;
00256       }
00257 
00258     int result = ::setsockopt(m_fd, level, optname, &value, sizeof(T));
00259 
00260     if ( result == -1 ) throw_socket_exception( errno );
00261   }
00262 
00263   template <typename T>
00264   inline
00265   void Socket::option( int level, int optname, T& value )
00266   {
00267     if ( ! is_bound() )
00268       try {
00269         change_state( SOCKET_BOUND );
00270       } catch ( exception::state::failed ) {
00271         return ;
00272       }
00273 
00274     socklen_t size = sizeof( T );
00275     ::getsockopt( m_fd, level, optname, &value, &size );
00276   }
00277 }
00278 
00279 #endif

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