2 * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
3 * Author "Romain Forlot" <romain.forlot@iot.bzh>
4 * Author "Loïc Collignon" <loic.collignon@iot.bzh>
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
23 /// @brief Construct a default, invalid, socket.
25 : socket_{INVALID_SOCKET}
29 /// @brief Construct a socket by moving an existing one.
30 socket_t::socket_t(socket_t&& s)
33 s.socket_ = INVALID_SOCKET;
36 /// @brief Destruct the socket.
39 if(socket_ != INVALID_SOCKET)
43 /// @brief Test if socket is valid.
44 /// @return true if valid, false otherwise.
45 socket_t::operator bool() const
47 return socket_ != INVALID_SOCKET;
50 /// @brief Open the socket.
51 /// @param[in] domain Specifies the communications domain in which a socket is to be created.
52 /// @param[in] type Specifies the type of socket to be created.
53 /// @param[in] protocol Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type.
54 /// @return Upon successful completion, shall return a non-negative integer, the socket file descriptor. Otherwise, a value of -1 shall be returned and errno set to indicate the error.
55 int socket_t::open(int domain, int type, int protocol)
58 socket_ = ::socket(domain, type, protocol);
62 /// @brief Close the socket.
63 /// @return 0 if success.
66 return socket_ != INVALID_SOCKET ? ::close(socket_) : 0;
69 /// @brief Set socket option.
70 /// @return 0 if success.
71 int socket_t::setopt(int level, int optname, const void* optval, socklen_t optlen)
73 return socket_ != INVALID_SOCKET ? ::setsockopt(socket_, level, optname, optval, optlen) : 0;
76 /// @brief Bind the socket.
77 /// @return 0 if success.
78 int socket_t::bind(const struct sockaddr* addr, socklen_t len)
80 return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
83 /// @brief Get the file descriptor.
84 /// @return The socket's file descriptor
85 int socket_t::socket() const