socket_server_pos.hpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Ifdefs
12 *****************************************************************************/
13 
14 #ifndef ECL_DEVICES_SOCKET_SERVER_POS_HPP_
15 #define ECL_DEVICES_SOCKET_SERVER_POS_HPP_
16 
17 /*****************************************************************************
18 ** Cross platform
19 *****************************************************************************/
20 
21 #include <ecl/config/ecl.hpp>
22 #ifndef ECL_IS_APPLE
23 #ifdef ECL_IS_POSIX
24 
25 /*****************************************************************************
26 ** Includes
27 *****************************************************************************/
28 
29 #include <arpa/inet.h> // inet_addr()
30 #include <sys/ioctl.h> // used in remaining()
31 #include <errno.h>
32 #include <sys/socket.h>
33 
34 #include "detail/socket_error_handler_pos.hpp"
35 #include "detail/socket_exception_handler_pos.hpp"
36 #include "traits.hpp"
37 
38 /*****************************************************************************
39 ** Namespaces
40 *****************************************************************************/
41 
42 namespace ecl {
43 
44 /*****************************************************************************
45 ** Interface [SocketServer]
46 *****************************************************************************/
60 class SocketServer {
61 public:
62  /*********************
63  ** C&D
64  **********************/
65  SocketServer() : is_open(false) {};
76  SocketServer(const unsigned int &port_number) ecl_throw_decl(StandardException);
77  virtual ~SocketServer() { close(); };
79  /*********************
80  ** Configuration
81  **********************/
92  bool open( const unsigned int& port_number ) ecl_throw_decl(StandardException);
110  void close() { is_open = false; ::close(socket_fd); }
111 
112  bool open() const { return is_open; }
113 
114  /*********************
115  ** Writing
116  **********************/
125  long write(const char &c) ecl_debug_throw_decl(StandardException) { return write(&c,1); }
126 
135  long write(const char *s, unsigned long n) ecl_debug_throw_decl(StandardException);
136 
143  void flush() {}
144 
145  /*********************
146  ** Reading
147  **********************/
155  long remaining();
174  long read(char &c) ecl_debug_throw_decl(StandardException) { return read(&c,1); }
184  long read(char *s, const unsigned long &n) ecl_debug_throw_decl(StandardException);
195  long peek(char*s, const unsigned long &n) ecl_debug_throw_decl(StandardException);
196 
197  /*********************
198  ** Socket Specific
199  **********************/
208  int listen() ecl_throw_decl(StandardException);
209 
213  const Error& error() const { return error_handler; }
214 
215 private:
216  int port;
217  int socket_fd;
218  int client_socket_fd;
219  bool is_open;
220  Error error_handler;
221 };
222 
223 /*****************************************************************************
224 ** Traits [Serial]
225 *****************************************************************************/
231 template <>
232 class is_sink<SocketServer> : public True {};
233 
239 template <>
240 class is_source<SocketServer> : public True {};
241 
247 template <>
248 class is_sourcesink<SocketServer> : public True {};
249 
250 } // namespace ecl
251 
252 #endif /* ECL_IS_POSIX */
253 #endif /* !ECL_IS_APPLE */
254 
255 #endif /* ECL_DEVICES_SOCKET_SERVER_POS_HPP_ */
#define ecl_throw_decl(exception)
Standard ecl throw exception declaration.
Error
Bool< true > True
#define ecl_debug_throw_decl(exception)
Debug mode throw exception declaration.


xbot_node
Author(s): Roc, wangpeng@droid.ac.cn
autogenerated on Sat Oct 10 2020 03:28:13