socket.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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
00032 namespace Conexus
00033 {
00034
00048 class Socket: public FileDescriptor
00049 {
00050 protected:
00051
00055 Socket( int domain = -1, int type = -1, int protocol = 0 ) throw ();
00056
00057 public:
00058
00059 typedef ConexusPointer<Socket> pointer;
00060
00061 static pointer create( int domain = -1, int type = -1, int protocol = 0 ) throw ();
00062
00063 virtual ~Socket() throw ();
00064
00075 virtual void open() throw ( open_exception );
00076
00081 virtual void close( bool force = false ) throw ( close_exception );
00082
00088 virtual void bind() throw ( bind_exception );
00089
00097 virtual void bind( const Conexus::Address& a ) throw ( bind_exception );
00098
00104 virtual void connect() throw ( connect_exception );
00105
00113 virtual void connect( const Address& a ) throw ( connect_exception );
00114
00122 virtual void listen( int backlog = 0 );
00123
00127 int domain() throw ();
00128
00150 void set_domain( int ) throw ();
00151
00155 int type() throw ();
00156
00183 void set_type( int ) throw ();
00184
00188 int protocol() throw ();
00189
00194 void set_protocol( int ) throw ();
00195
00196 virtual size_t writeto( Address& a, const void* data, size_t size, Timeout timeout=Timeout(-1) ) throw ( write_exception );
00197
00198 virtual size_t writeto( Address& a, const Data data, Timeout timeout=Timeout(-1) ) throw ( write_exception );
00199
00200 virtual void set_option( int option, bool b, long required_state=SOCKET_BOUND );
00201
00202 template <typename T>
00203 void set_option( int level, int optname, T& value, long required_state=SOCKET_BOUND );
00204
00205 template <typename T>
00206 void option( int level, int optname, T& value, long required_state=SOCKET_BOUND );
00207
00208 void enable_reuse_address( bool enable=true );
00209
00210 virtual void change_state( long states ) throw ( state_exception );
00211
00212 sigc::signal<void> signal_bound();
00213
00214 sigc::signal<void> signal_connected();
00215
00216 sigc::signal<void> signal_listening();
00217
00218 bool is_bound();
00219
00220 bool is_connected();
00221
00222 bool is_listening();
00223
00224 bool is_accepted();
00225
00226 protected:
00227 int m_domain;
00228 int m_type;
00229 int m_protocol;
00230
00231 virtual void read_thread_main();
00232
00233 virtual void set_state_closed();
00234 virtual void set_state_bound();
00235 virtual void set_state_connected();
00236 virtual void set_state_listening();
00237
00238 sigc::signal<void> m_signal_bound;
00239 sigc::signal<void> m_signal_connected;
00240 sigc::signal<void> m_signal_listening;
00241
00242 virtual size_t write_data( const Data data, Timeout timeout ) throw ( write_exception );
00243
00251 virtual Data read_data( size_t size, Timeout timeout ) throw ( read_exception );
00252
00253 };
00254
00255 template <typename T>
00256 inline
00257 void Socket::set_option( int level, int optname, T& value, long required_state )
00258 {
00259 try {
00260 change_state( required_state );
00261 } catch ( exception::state::failed ) {
00262 return ;
00263 }
00264
00265 int result = ::setsockopt(m_fd, level, optname, &value, sizeof(T));
00266
00267 if ( result == -1 ) throw_socket_exception( errno );
00268 }
00269
00270 template <typename T>
00271 inline
00272 void Socket::option( int level, int optname, T& value, long required_state )
00273 {
00274 try {
00275 change_state( required_state );
00276 } catch ( exception::state::failed ) {
00277 return ;
00278 }
00279
00280 socklen_t size = sizeof( T );
00281 ::getsockopt( m_fd, level, optname, &value, &size );
00282 }
00283 }
00284
00285 #endif