ANNA Suite  2020b
Multipurpose development suite for Telco applications
Socket.hpp
Go to the documentation of this file.
1 // ANNA - Anna is Not Nothingness Anymore //
2 // //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
4 // //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
7 
8 
9 #ifndef anna_comm_Socket_hpp
10 #define anna_comm_Socket_hpp
11 
12 #include <unistd.h>
13 #include <sys/socket.h>
14 #include <sys/un.h>
15 
16 #include <anna/config/defines.hpp>
17 #include <anna/core/mt/Mutex.hpp>
18 
21 
22 namespace anna {
23 
24 class DataBlock;
25 
26 namespace xml {
27 class Node;
28 }
29 
30 namespace comm {
31 
32 class TransportFactory;
33 class ReceiverFactory;
34 
39 class Socket : public Mutex {
40 public:
44  struct Domain { enum _v { Unix = PF_UNIX, Inet = PF_INET } ; };
45 
49  struct Type { enum _v { Stream = SOCK_STREAM, Datagram = SOCK_DGRAM, Raw = SOCK_RAW } ; };
50 
55  struct Notify {
56  enum _v {
57  None,
60  Corrupt
61  };
62  };
63 
67  virtual ~Socket();
68 
73  int getfd() const { return a_fd; }
74 
79  Type::_v getType() const { return a_type; }
80 
85  Domain::_v getDomain() const { return a_domain; }
86 
91  int getCategory() const { return a_category; }
92 
101  bool support(const char* transportClassName) const ;
102 
108  bool isBound() const { return a_isBound; }
109 
114  bool isOpened() const { return a_fd != -1; }
115 
122  virtual bool isSecure() const { return false; }
123 
128  const AccessPoint& getLocalAccessPoint() const { return a_localAccessPoint; }
129 
134  TransportFactory* getTransportFactory() const { return a_transportFactory; }
135 
140  ReceiverFactory* getReceiverFactory() { return a_receiverFactory; }
141 
147  bool setBlockingMode(const bool blockingMode) noexcept(false);
148 
156  bool setReuseMode(const bool reuseMode) noexcept(false);
157 
162  void setTransportFactory(TransportFactory* transportFactory) { a_transportFactory = transportFactory; }
163 
168  void setReceiverFactory(ReceiverFactory& receiverFactory) { a_receiverFactory = &receiverFactory; }
169 
177  void setCategory(const int category) { a_category = category; }
178 
182  void close() ;
183 
187  virtual void bind() noexcept(false);
188 
193  virtual std::string asString() const ;
194 
200  virtual xml::Node* asXML(xml::Node* parent) const noexcept(false);
201 
202 protected:
205  int a_fd;
207  bool a_isBound;
211 
220  Socket(const Domain::_v domain, const Type::_v type, TransportFactory* transportFactory = NULL);
221 
232  Socket(const INetAddress& localAddress, const Type::_v type, TransportFactory* transportFactory = NULL);
233 
242  Socket(const std::string& path, const Type::_v type, TransportFactory* transportFactory = NULL);
243 
247  void open() noexcept(false);
248 
252  virtual void do_close() { ::close(a_fd); }
253 
258  virtual int do_bind(const struct sockaddr*, const int) noexcept(false);
259 
265  static const char* asText(const Notify::_v v) ;
266 
267 private:
268  bool a_reuseMode;
269 };
270 
271 #define anna_socket_assert(a,b) \
272  if ((a)) { \
273  std::string msg (asString ()); \
274  msg += " | "; \
275  msg += b; \
276  throw RuntimeException (msg, __FILE__, __LINE__); \
277  }
278 
279 #define anna_comm_socket_check(a,b) \
280  if ((a) < 0) { \
281  std::string msg (asString ()); \
282  msg += " | "; \
283  msg += b; \
284  throw RuntimeException (msg, errno, ANNA_FILE_LOCATION); \
285  }
286 }
287 }
288 
289 
290 #endif
291 
292 
293 
Definition: INetAddress.hpp:34
Definition: Socket.hpp:57
Definition: Socket.hpp:55
const AccessPoint & getLocalAccessPoint() const
Definition: Socket.hpp:128
ReceiverFactory * getReceiverFactory()
Definition: Socket.hpp:140
Domain::_v getDomain() const
Definition: Socket.hpp:85
int a_fd
Definition: Socket.hpp:205
bool isOpened() const
Definition: Socket.hpp:114
TransportFactory * a_transportFactory
Definition: Socket.hpp:208
Definition: Socket.hpp:59
void setTransportFactory(TransportFactory *transportFactory)
Definition: Socket.hpp:162
Definition: Node.hpp:56
Definition: Socket.hpp:44
TransportFactory * getTransportFactory() const
Definition: Socket.hpp:134
bool isBound() const
Definition: Socket.hpp:108
Type::_v getType() const
Definition: Socket.hpp:79
void setReceiverFactory(ReceiverFactory &receiverFactory)
Definition: Socket.hpp:168
const Domain::_v a_domain
Definition: Socket.hpp:203
AccessPoint a_localAccessPoint
Definition: Socket.hpp:206
_v
Definition: Socket.hpp:49
ReceiverFactory * a_receiverFactory
Definition: Socket.hpp:209
void setCategory(const int category)
Definition: Socket.hpp:177
_v
Definition: Socket.hpp:44
_v
Definition: Socket.hpp:56
bool a_isBound
Definition: Socket.hpp:207
Definition: Socket.hpp:39
xml::Node Node
Definition: Node.hpp:21
Definition: ReceiverFactory.hpp:53
const Type::_v a_type
Definition: Socket.hpp:204
Definition: app.hpp:12
Definition: Socket.hpp:49
Definition: Mutex.hpp:41
Definition: TransportFactory.hpp:30
int getCategory() const
Definition: Socket.hpp:91
virtual bool isSecure() const
Definition: Socket.hpp:122
int a_category
Definition: Socket.hpp:210
Definition: AccessPoint.hpp:37
int getfd() const
Definition: Socket.hpp:73