Free Electron
Socket.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __platform_Socket_h__
8 #define __platform_Socket_h__
9 namespace fe
10 {
11 namespace ext
12 {
13 
14 typedef int SockHandle;
15 
16 /** BSD socket address wrapper. */
17 class FE_DL_EXPORT SockAddr
18 {
19  public:
20  SockAddr(void)
21  {
22  memset(&m_sockaddr, 0, sizeof(m_sockaddr));
23  m_sockaddr.sin_family = AF_INET;
24  setAddress(INADDR_ANY);
25  }
26 
27  /** Access pointer to underlying (struct sockaddr *) */
28  struct sockaddr * ptr(void)
29  {
30  return (struct sockaddr *)&m_sockaddr;
31  }
32  /** Return size of address. */
33  int size(void)
34  {
35  return sizeof(m_sockaddr);
36  }
37  /** Set the port. @todo add a by-service-name port set. */
38  void setPort(FE_UWORD port)
39  {
40  m_sockaddr.sin_port = htons((short)port);
41  }
42  /** Set the address. */
43  void setAddress(const U32 address)
44  {
45  m_sockaddr.sin_addr.s_addr = htonl(address);
46  }
47  /** Set the address. This may either be the hostname or the
48  dot notation address. */
49  void setAddress(const String &host);
50  /** Return the hostname for the address. */
51  String hostname(void);
52  FE_UWORD port(void)
53  {
54  return (long)ntohs(m_sockaddr.sin_port);
55  }
56  /** Return the address. */
57  static U32 getIp4Address(const String &host);
58 
59  protected:
60  struct sockaddr_in m_sockaddr;
61 };
62 
63 /** A socket wrapper. The premise of this wrapper is to be simple and
64  to the point. Flexibility and extensibility are not explicitly considered.
65  The intent is that all network communication will go through class and does
66  not care about TCP/IP details.
67 
68  A concept of thread poison is supported by this class due to the blocking
69  calls (accept, read, connect). If Poison is associated with the Socket,
70  then blocking calls will be aborted if the Poison becomes active before
71  the calls can return normally. If this happens then a Poisoned exception
72  is thrown.
73 
74  A concept of network identifier is also supported by this class to provide
75  a notion of site uniqueness in a networked application. It is implemented
76  in this lower level abstraction to assure simplicity in maintaining
77  uniqueness. A socket's unique id is 0 until it does a connect() in which
78  case it will be given a unique id from the other side of the connection
79  (presumably a 'server').
80  */
81 class FE_DL_EXPORT Socket : public Counted, ObjectSafe<Socket>
82 {
83  public:
84 
85  enum Transport
86  {
87  e_tcp = 0,
88  e_udp = 1
89  };
90 
91  //* bit field
92  enum ReadMode
93  {
94  e_readNormal = 0,
95  e_readPeek = 1,
96  e_readNoWait = 2
97  };
98 
99  /** Constructor. */
100  Socket(void);
101  /** Construct a socket with the given poison. */
102  Socket(Poison *pPoison);
103 virtual ~Socket(void);
104  /** Wraps bsd socket(AF_INET, SOCK_STREAM, 0) call. */
105 virtual void openStream(Transport transport=e_tcp,
106  BWORD server=FALSE);
107  /** Wraps bsd socket() call. */
108 virtual SockHandle socket(int domain, int type, int protocol);
109 
110  /** Wraps bsd bind() call. */
111 virtual void bind(SockAddr &sockaddr);
112  /** Wraps bsd listen() call. */
113 virtual void listen(int backlog);
114  /** Wraps bsd accept() call. */
115 virtual sp<Socket> accept(SockAddr &sockaddr);
116  /** Wraps bsd connect() call. */
117 virtual void connect(SockAddr &sockaddr);
118  /** Wraps bsd recv() call. */
119 virtual IWORD read(const void *pBuffer, IWORD count,
120  ReadMode readMode=e_readNormal);
121  /** Wraps bsd send() call. */
122 virtual IWORD write(const void *pBuffer, IWORD count);
123 
124  /** Set a Poison object. */
125 virtual void setPoison(Poison *pPoison);
126  /** Wait for the socket to be readable (via select), checking at
127  a regular interval for Poison to be active. Mainly for
128  internal use by read(), accept(), and connect() */
129 virtual void wait(void);
130 
131  /** Close the underlying socket. */
132 virtual void close(void);
133 
134  /** Return true if the socket is open, otherwise return false. */
135 virtual bool isOpen(void);
136 
137  /** Return a network identifier unique among Sockets connected
138  to the same accept()ing 'server' Socket. Note that this has
139  nothing to do with a socket descriptor. */
140 virtual U32 id(void);
141 
142  /** Startup socket networking. Only required on Win32, but should
143  always be done for portability. */
144 static void startup(void);
145  /** Shutdown socket networking. */
146 static void shutdown(void);
147  SockHandle sockHandle() const {return m_sock_handle;}
148 
149  // WORKAROUND to UDP read socket. This may go away soon.
150  Transport transportType() const {return m_transportType;}
151  SockAddr sockAddrOwn()const {return m_udpSockAddrOwn;}
152  //------------------------------------------------------------
153  private:
154  IWORD udpRead(const void *pBuffer, IWORD count,
155  ReadMode readMode);
156  IWORD udpWrite(const void *pBuffer, IWORD count);
157 
158  SockHandle m_sock_handle;
159  bool m_open;
160  bool m_original;
161  Poison *m_pPoison;
162  U32 m_id;
163  U32 m_nextID;
164  Transport m_transportType;
165  BWORD m_server;
166  SockAddr m_udpSockAddrOwn;
167 #if FE_OS==FE_WIN32 || FE_OS==FE_WIN64
168  static bool m_wsaStarted;
169 #endif
170 };
171 
172 } /* namespace ext */
173 } /* namespace fe */
174 
175 #endif /* __platform_Socket_h__ */
176 
Object level locking for thread safety.
Definition: Safe.h:216
int size(void)
Return size of address.
Definition: Socket.h:33
Heap-based support for classes participating in fe::ptr <>
Definition: Counted.h:35
kernel
Definition: namespace.dox:3
A socket wrapper.
Definition: Socket.h:81
BSD socket address wrapper.
Definition: Socket.h:17
struct sockaddr * ptr(void)
Access pointer to underlying (struct sockaddr *)
Definition: Socket.h:28
Automatically reference-counted string container.
Definition: String.h:128
void setPort(FE_UWORD port)
Set the port.
Definition: Socket.h:38
Death pill for threads.
Definition: Poison.h:15
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53
void setAddress(const U32 address)
Set the address.
Definition: Socket.h:43