• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

ibrcommon/ibrcommon/net/vsocket.h

Go to the documentation of this file.
00001 /*
00002  * vsocket.h
00003  *
00004  *  Created on: 14.12.2010
00005  *      Author: morgenro
00006  */
00007 
00008 #ifndef VSOCKET_H_
00009 #define VSOCKET_H_
00010 
00011 #include <ibrcommon/data/File.h>
00012 #include "ibrcommon/net/LinkManager.h"
00013 #include <ibrcommon/net/vinterface.h>
00014 #include <ibrcommon/net/vaddress.h>
00015 #include <ibrcommon/thread/Mutex.h>
00016 #include <ibrcommon/thread/Thread.h>
00017 #include <string>
00018 #include <list>
00019 #include <set>
00020 
00021 namespace ibrcommon
00022 {
00023         class vsocket_exception : public Exception
00024         {
00025         public:
00026                 vsocket_exception(string error) : Exception(error)
00027                 {};
00028         };
00029 
00030         class vsocket_timeout : public vsocket_exception
00031         {
00032         public:
00033                 vsocket_timeout(string error) : vsocket_exception(error)
00034                 {};
00035         };
00036 
00037         class vsocket_interrupt : public vsocket_exception
00038         {
00039         public:
00040                 vsocket_interrupt(string error) : vsocket_exception(error)
00041                 {};
00042         };
00043 
00044         class vsocket : public ibrcommon::LinkManager::EventCallback
00045         {
00046         // accept method is a friend and can access the file descriptors directly
00047         friend void interrupt(ibrcommon::vsocket &sock, ibrcommon::Thread &th);
00048         friend void select(ibrcommon::vsocket &sock, std::list<int> &fds, struct timespec *tv);
00049         friend int sendto(ibrcommon::vsocket &sock, const void *buf, size_t n, const ibrcommon::vaddress &address, const unsigned int port);
00050 
00051         public:
00052                 enum Option
00053                 {
00054                         VSOCKET_REUSEADDR = 1 << 0,
00055                         VSOCKET_LINGER = 1 << 1,
00056                         VSOCKET_NODELAY = 1 << 2,
00057                         VSOCKET_BROADCAST = 1 << 3,
00058                         VSOCKET_NONBLOCKING = 1 << 4,
00059                         VSOCKET_MULTICAST = 1 << 5,
00060                         VSOCKET_MULTICAST_V6 = 1 << 6
00061                 };
00062 
00063                 vsocket();
00064                 virtual ~vsocket();
00065 
00066                 int bind(const vaddress &address, const int port, unsigned int socktype = SOCK_STREAM);
00067                 void bind(const vinterface &iface, const int port, unsigned int socktype = SOCK_STREAM);
00068                 int bind(const int port, unsigned int socktype = SOCK_STREAM);
00069                 int bind(const ibrcommon::File &file, unsigned int socktype = SOCK_STREAM);
00070 
00071                 void unbind(const vaddress &address, const int port);
00072                 void unbind(const vinterface &iface, const int port);
00073                 void unbind(const int port);
00074                 void unbind(const ibrcommon::File &file);
00075 
00076                 void add(const int fd);
00077 
00078                 const std::list<int> get(const ibrcommon::vinterface &iface, const ibrcommon::vaddress::Family f = vaddress::VADDRESS_UNSPEC);
00079                 const std::list<int> get(const ibrcommon::vaddress::Family f = vaddress::VADDRESS_UNSPEC);
00080 
00081                 void listen(int connections);
00082 
00083                 void set(const Option &o);
00084                 void unset(const Option &o);
00085 
00086                 void close();
00087                 void shutdown();
00088 
00089                 int fd();
00090 
00091                 void eventInterfaceChanged(const vinterface &iface);
00092 
00093         private:
00094                 class vbind
00095                 {
00096                 public:
00097                         enum bind_type
00098                         {
00099                                 BIND_ADDRESS,
00100                                 BIND_FILE,
00101                                 BIND_CUSTOM,
00102                                 BIND_ADDRESS_NOPORT
00103                         };
00104 
00105                         const bind_type _type;
00106                         const vaddress _vaddress;
00107                         const int _port;
00108                         const ibrcommon::File _file;
00109                         const ibrcommon::vinterface _interface;
00110 
00111                         int _fd;
00112 
00113                         vbind(int fd);
00114                         vbind(const vaddress &address, unsigned int socktype);
00115                         vbind(const vaddress &address, const int port, unsigned int socktype);
00116                         vbind(const ibrcommon::vinterface &iface, const vaddress &address, unsigned int socktype);
00117                         vbind(const ibrcommon::vinterface &iface, const vaddress &address, const int port, unsigned int socktype);
00118                         vbind(const ibrcommon::File &file, unsigned int socktype);
00119                         virtual ~vbind();
00120 
00121                         void bind();
00122                         void listen(int connections);
00123 
00124                         void set(const vsocket::Option &o);
00125                         void unset(const vsocket::Option &o);
00126 
00127                         void close();
00128                         void shutdown();
00129 
00130                         bool operator==(const vbind &obj) const;
00131 
00132                 private:
00133                         void check_socket_error(const int err) const;
00134                         void check_bind_error(const int err) const;
00135                 };
00136 
00137                 int bind(const vsocket::vbind &b);
00138                 void unbind(const vsocket::vbind &b);
00139 
00140                 ibrcommon::Mutex _bind_lock;
00141                 std::list<vsocket::vbind> _binds;
00142 
00143                 unsigned int _options;
00144                 bool interrupt;
00145         };
00146 
00153         void select(ibrcommon::vsocket &sock, std::list<int> &fds, struct timespec *tv = NULL);
00154         void interrupt(ibrcommon::vsocket &sock, ibrcommon::Thread &th);
00155         void sighandler_poll(int sig);
00156 
00157         int sendto(ibrcommon::vsocket &sock, const void *buf, size_t n, const ibrcommon::vaddress &address, const unsigned int port);
00158 
00159         int recvfrom(int fd, char* data, size_t maxbuffer, std::string &address);
00160 }
00161 
00162 #endif /* VSOCKET_H_ */

Generated on Thu Mar 10 2011 15:44:03 for IBR-DTNSuite by  doxygen 1.7.1