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
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
00260
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