Robotics Library  0.7.0
Socket.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_HAL_SOCKET_H
28 #define RL_HAL_SOCKET_H
29 
30 #ifdef WIN32
31 #include <winsock2.h>
32 #else
33 #include <sys/socket.h>
34 #endif // WIN32
35 
36 #include <chrono>
37 #include <string>
38 #include <vector>
39 #include <rl/math/Real.h>
40 
41 #include "Com.h"
42 
43 namespace rl
44 {
45  namespace hal
46  {
47  class Socket : public Com
48  {
49  public:
50  class Address
51  {
52  public:
53  Address();
54 
55  Address(const Address& address);
56 
57  Address(const ::sockaddr_storage& addr);
58 
59  virtual ~Address();
60 
61  static Address Ipv4(const ::std::string& string, const unsigned short int& port, const bool& asNumeric = false);
62 
63  static Address Ipv4(const ::std::string& string, const ::std::string& port, const bool& asNumeric = false);
64 
65  static Address Ipv6(const ::std::string& string, const unsigned short int& port, const bool& asNumeric = false);
66 
67  static Address Ipv6(const ::std::string& string, const ::std::string& port, const bool& asNumeric = false);
68 
69  const ::sockaddr_storage& get() const;
70 
71  ::std::vector<unsigned char> getHexadecimal();
72 
73  ::std::string getNameInfo(const bool& asNumeric = false) const;
74 
75  void setInfo(const ::std::string& string, const unsigned short int& port, const bool& asNumeric = false);
76 
77  void setInfo(const ::std::string& string, const ::std::string& port, const bool& asNumeric = false);
78 
79  protected:
80  Address(const int& family);
81 
82  private:
83  ::sockaddr_storage addr;
84  };
85 
86  enum Option
87  {
91 #if defined(__APPLE__) || defined(__QNX__) || defined(WIN32)
93 #else // __APPLE__ || __QNX__ || WIN32
96 #endif // __APPLE__ || __QNX__ || WIN32
97  };
98 
99  Socket(const Socket& socket);
100 
101  virtual ~Socket();
102 
103  static Socket Tcp(const Address& address);
104 
105  static Socket Udp(const Address& address);
106 
107  Socket accept();
108 
109  void bind();
110 
111  void close();
112 
113  void connect();
114 
115  const Address& getAddress() const;
116 
117  int getOption(const Option& option) const;
118 
119  const int& getProtocol() const;
120 
121  const int& getType() const;
122 
123  void listen();
124 
125  void listen(const int& backlog);
126 
127  void open();
128 
129  ::std::size_t recv(void* buf, const ::std::size_t& count);
130 
131  ::std::size_t recvfrom(void* buf, const ::std::size_t& count, Address& address);
132 
133  ::std::size_t select(const bool& read, const bool& write, const ::std::chrono::nanoseconds& timeout);
134 
135  ::std::size_t send(const void* buf, const ::std::size_t& count);
136 
137  ::std::size_t sendto(const void* buf, const ::std::size_t& count, const Address& address);
138 
139  void setAddress(const Address& address);
140 
141  void setOption(const Option& option, const int& value);
142 
143  void shutdown(const bool& read = true, const bool& write = true);
144 
145  protected:
146  Socket(const int& type, const int& protocol, const Address& address);
147 
148 #ifdef WIN32
149  Socket(const int& type, const int& protocol, const Address& address, const SOCKET& fd);
150 #else // WIN32
151  Socket(const int& type, const int& protocol, const Address& address, const int& fd);
152 #endif // WIN32
153 
154 #ifdef WIN32
155  SOCKET fd;
156 #else // WIN32
157  int fd;
158 #endif // WIN32
159 
160  private:
161 #ifdef WIN32
162  static void cleanup();
163 
164  static void startup();
165 #endif // WIN32
166 
168 
169  int protocol;
170 
171  int type;
172  };
173  }
174 }
175 
176 #endif // RL_HAL_SOCKET_H
rl::hal::Socket::Address::Ipv6
static Address Ipv6(const ::std::string &string, const unsigned short int &port, const bool &asNumeric=false)
Definition: Socket.cpp:709
rl::hal::Socket::getAddress
const Address & getAddress() const
Definition: Socket.cpp:221
rl::hal::Socket
Definition: Socket.h:48
rl::hal::Socket::Address
Definition: Socket.h:51
rl::hal::Socket::Option
Option
Definition: Socket.h:87
rl::hal::Socket::setOption
void setOption(const Option &option, const int &value)
Definition: Socket.cpp:511
Com.h
rl::hal::Socket::type
int type
Definition: Socket.h:171
rl::hal::Socket::sendto
::std::size_t sendto(const void *buf, const ::std::size_t &count, const Address &address)
Definition: Socket.cpp:481
rl::hal::Socket::getType
const int & getType() const
Definition: Socket.cpp:310
rl::hal::Socket::close
void close()
Definition: Socket.cpp:181
rl::hal::Socket::Address::get
const ::sockaddr_storage & get() const
Definition: Socket.cpp:725
rl::hal::Socket::recvfrom
::std::size_t recvfrom(void *buf, const ::std::size_t &count, Address &address)
Definition: Socket.cpp:386
rl::hal::Socket::Address::getHexadecimal
::std::vector< unsigned char > getHexadecimal()
Definition: Socket.cpp:731
rl::hal::Socket::Tcp
static Socket Tcp(const Address &address)
Definition: Socket.cpp:113
rl::hal::Socket::recv
::std::size_t recv(void *buf, const ::std::size_t &count)
Definition: Socket.cpp:360
rl::hal::Socket::fd
int fd
Definition: Socket.h:157
rl::hal::Socket::Address::Ipv4
static Address Ipv4(const ::std::string &string, const unsigned short int &port, const bool &asNumeric=false)
Definition: Socket.cpp:693
rl::hal::Socket::address
Address address
Definition: Socket.h:167
rl::hal::Socket::OPTION_MULTICAST_TTL
@ OPTION_MULTICAST_TTL
Definition: Socket.h:90
rl::hal::Socket::listen
void listen()
Definition: Socket.cpp:316
rl::hal::Socket::~Socket
virtual ~Socket()
Definition: Socket.cpp:100
rl::hal::Socket::open
void open()
Definition: Socket.cpp:340
rl::hal::Socket::getOption
int getOption(const Option &option) const
Definition: Socket.cpp:227
rl::hal::Socket::setAddress
void setAddress(const Address &address)
Definition: Socket.cpp:505
rl::hal::Socket::Address::~Address
virtual ~Address()
Definition: Socket.cpp:685
rl::hal::Socket::Socket
Socket(const Socket &socket)
Definition: Socket.cpp:55
rl::hal::Com
Definition: Com.h:35
rl::hal::Socket::OPTION_KEEPALIVE
@ OPTION_KEEPALIVE
Definition: Socket.h:88
Real.h
rl::hal::Socket::Address::setInfo
void setInfo(const ::std::string &string, const unsigned short int &port, const bool &asNumeric=false)
Definition: Socket.cpp:768
rl::hal::Socket::Address::addr
::sockaddr_storage addr
Definition: Socket.h:83
rl::hal::Socket::shutdown
void shutdown(const bool &read=true, const bool &write=true)
Definition: Socket.cpp:580
rl::hal::Socket::OPTION_MULTICAST_LOOP
@ OPTION_MULTICAST_LOOP
Definition: Socket.h:89
rl::hal::Socket::Udp
static Socket Udp(const Address &address)
Definition: Socket.cpp:119
rl::hal::Socket::OPTION_NODELAY
@ OPTION_NODELAY
Definition: Socket.h:94
rl::hal::Socket::connect
void connect()
Definition: Socket.cpp:203
rl::hal::Socket::Address::Address
Address()
Definition: Socket.cpp:651
rl::hal::Socket::send
::std::size_t send(const void *buf, const ::std::size_t &count)
Definition: Socket.cpp:457
rl::hal::Socket::bind
void bind()
Definition: Socket.cpp:152
rl::hal::Socket::getProtocol
const int & getProtocol() const
Definition: Socket.cpp:304
rl::hal::Socket::OPTION_QUICKACK
@ OPTION_QUICKACK
Definition: Socket.h:95
rl::hal::Socket::protocol
int protocol
Definition: Socket.h:169
rl::hal::Socket::accept
Socket accept()
Definition: Socket.cpp:125
rl::hal::Socket::Address::getNameInfo
::std::string getNameInfo(const bool &asNumeric=false) const
Definition: Socket.cpp:753
rl::hal::Socket::select
::std::size_t select(const bool &read, const bool &write, const ::std::chrono::nanoseconds &timeout)
Definition: Socket.cpp:417
rl
Robotics Library.
Definition: AnalogInput.cpp:30