socket.h
Go to the documentation of this file.
1 /*
2  * rcdiscover - the network discovery tool for Roboception devices
3  *
4  * Copyright (c) 2017 Roboception GmbH
5  * All rights reserved
6  *
7  * Author: Raphael Schaller
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. Neither the name of the copyright holder nor the names of its contributors
20  * may be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #ifndef RCDISCOVER_SOCKET_H
37 #define RCDISCOVER_SOCKET_H
38 
39 #include <vector>
40 #include <cstdint>
41 #include <string>
42 
43 struct sockaddr_in;
44 
45 namespace rcdiscover
46 {
47 
51 template<typename Derived>
52 class Socket
53 {
54  private:
55  Derived &getDerived()
56  {
57  return *static_cast<Derived *>(this);
58  }
59 
60  const Derived &getDerived() const
61  {
62  return *static_cast<const Derived *>(this);
63  }
64 
65  public:
66  explicit Socket(std::string iface_name) : iface_name_(std::move(iface_name))
67  { }
68 
69  Socket(const Socket&) = delete;
70  Socket& operator=(const Socket&) = delete;
71 
72  Socket(Socket &&) = default;
73  Socket& operator=(Socket &&) = default;
74 
79  template<typename T>
80  const T &getHandle() const
81  {
82  return getDerived().getHandleImpl();
83  }
84 
89  void bind(const sockaddr_in& addr)
90  {
91  getDerived().bindImpl(addr);
92  }
93 
98  void send(const std::vector<uint8_t>& sendbuf)
99  {
100  getDerived().sendImpl(sendbuf);
101  }
102 
107  {
108  getDerived().enableBroadcastImpl();
109  }
110 
115  {
116  getDerived().enableNonBlockingImpl();
117  }
118 
122  const std::string &getIfaceName() const
123  {
124  return iface_name_;
125  }
126 
127  private:
128  std::string iface_name_;
129 };
130 
131 }
132 
133 #endif //RCDISCOVER_SOCKET_H
rcdiscover::Socket::getHandle
const T & getHandle() const
Returns the native socket handle.
Definition: socket.h:80
rcdiscover::Socket::Socket
Socket(std::string iface_name)
Definition: socket.h:66
rcdiscover::Socket::getDerived
const Derived & getDerived() const
Definition: socket.h:60
rcdiscover::Socket::send
void send(const std::vector< uint8_t > &sendbuf)
Sends data.
Definition: socket.h:98
rcdiscover
Definition: deviceinfo.cc:40
rcdiscover::Socket::getDerived
Derived & getDerived()
Definition: socket.h:55
rcdiscover::Socket::operator=
Socket & operator=(const Socket &)=delete
rcdiscover::Socket::enableNonBlocking
void enableNonBlocking()
Enables non-blocking operation for this socket.
Definition: socket.h:114
rcdiscover::Socket::getIfaceName
const std::string & getIfaceName() const
Returns the interface name.
Definition: socket.h:122
rcdiscover::Socket::enableBroadcast
void enableBroadcast()
Enables broadcast for this socket.
Definition: socket.h:106
rcdiscover::Socket::bind
void bind(const sockaddr_in &addr)
Binds the socket to an interface.
Definition: socket.h:89
rcdiscover::Socket
CRTP class for platform specific socket implementation.
Definition: socket.h:52
rcdiscover::Socket::iface_name_
std::string iface_name_
Definition: socket.h:128


rcdiscover
Author(s): Heiko Hirschmueller , Raphael Schaller
autogenerated on Thu Aug 1 2024 02:55:56