libBsdSockets
C++ Wrapper classes to the BSD Socket API
Socket.cpp
Go to the documentation of this file.
1 /*
2 
3 Copyright (c) 2013, Komodo Does Inc
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
7 
8 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
9 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
10 Neither the name of the Komodo Does Inc nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12 
13 */
14 
15 #include <sys/socket.h>
16 #include <sys/select.h>
17 #include <unistd.h>
18 #include <poll.h>
19 
20 #include <system_error>
21 #include <memory>
22 
23 #include "Address.h"
24 #include "AddressInfoConversions.h"
25 #include "Socket.h"
26 
27 namespace BsdSockets {
28 
30  if(-1 == lowLevelSocket) {
31  return;
32  }
33 
34  // Close it, ignoring the consequences since there is nothing that can be done
35  close(lowLevelSocket);
36  }
37 
38  Socket::Ptr Socket::create(Address::Ptr theAddress, int existingLowLevelSocket) {
39  return Socket::Ptr(new Socket(theAddress, existingLowLevelSocket));
40  }
41 
42  Socket::Socket(Address::Ptr theAddress)
43  : lowLevelSocket(::socket(
44  socketDomainToLowLevel(theAddress->getSocketDomain()),
45  socketTypeToLowLevel(theAddress->getSocketType()),
46  theAddress->getProtocol()
47  )),
48  address(theAddress)
49  {
50  }
51 
52  Socket::Socket(Address::Ptr theAddress, int existingLowLevelSocket)
53  : lowLevelSocket(existingLowLevelSocket),
54  address(theAddress)
55  {
56  if(-1 == lowLevelSocket) {
57  throw std::system_error(errno, std::system_category());
58  }
59  }
60 
61  SelectResult Socket::select(int timeout_ms, bool checkRead, bool checkWrite, bool checkError) {
62  struct timeval timeout;
63  if(0 != timeout_ms) {
64  timeout.tv_sec = timeout_ms / 1000;
65  timeout.tv_usec = 1000 * (timeout_ms % 1000);
66  }
67 
68  fd_set read; FD_ZERO(&read); if(checkRead) { FD_SET(lowLevelSocket, &read); }
69  fd_set write; FD_ZERO(&write); if(checkWrite) { FD_SET(lowLevelSocket, &write); }
70  fd_set error; FD_ZERO(&error); if(checkError) { FD_SET(lowLevelSocket, &error); }
71  const int result = ::select(lowLevelSocket + 1, &read, &write, &error, 0 == timeout_ms ? nullptr : &timeout);
72  if(-1 == result) {
73  throw std::system_error(errno, std::system_category());
74  }
75 
76  const bool readyToRead = FD_ISSET(lowLevelSocket, &read);
77  const bool readyToWrite = FD_ISSET(lowLevelSocket, &write);
78  const bool readyWithError = FD_ISSET(lowLevelSocket, &error);
79 
80  return SelectResult(checkRead, checkWrite, checkError, readyToRead, readyToWrite, readyWithError);
81  }
82 
83  short Socket::poll(int timeout_ms, short eventsToLookFor) {
84  struct pollfd fds[1];
85 
86  fds[0].fd = lowLevelSocket;
87  fds[0].events = eventsToLookFor;
88 
89  if(-1 == ::poll(fds, 1, timeout_ms)) {
90  throw std::system_error(errno, std::system_category());
91  }
92 
93  return fds[0].revents;
94  }
95 
96  ssize_t Socket::send(const void* buffer, size_t length, int flags) const {
97  const ssize_t result = ::send(lowLevelSocket, buffer, length, flags);
98  if(-1 != result) {
99  return result;
100  }
101 
102  throw std::system_error(errno, std::system_category());
103  }
104 
105  ssize_t Socket::receive(void* buffer, size_t length, int flags) const {
106  const ssize_t result = ::recv(lowLevelSocket, buffer, length, flags);
107  if(-1 != result) {
108  return result;
109  }
110 
111  throw std::system_error(errno, std::system_category());
112  }
113 
114  ssize_t Socket::blockingReceive(void* buffer, size_t length) const {
115  const ssize_t result = ::read(lowLevelSocket, buffer, length);
116  if(-1 != result) {
117  return result;
118  }
119 
120  throw std::system_error(errno, std::system_category());
121  }
122 
123  void Socket::getSocketOption(int level, int optionName, void* optionValue, socklen_t* optionLength) const {
124  if(-1 == ::getsockopt(lowLevelSocket, level, optionName, optionValue, optionLength)) {
125  throw std::system_error(errno, std::system_category());
126  }
127  }
128 
129  void Socket::setSocketOption(int level, int optionName, const void* optionValue, socklen_t optionLength) const {
130  if(-1 == ::setsockopt(lowLevelSocket, level, optionName, optionValue, optionLength)) {
131  throw std::system_error(errno, std::system_category());
132  }
133  }
134 
135  std::shared_ptr<Address> Socket::getAddress() const {
136  return address;
137  }
138 
140  return lowLevelSocket;
141  }
142 
143 } // namespace BsdSockets
std::shared_ptr< Address > Ptr
Definition: Address.h:38
Result of Socket::select() indicating the result status.
Definition: Socket.h:32
static Socket::Ptr create(std::shared_ptr< Address > theAddress, int existingLowLevelSocket)
Definition: Socket.cpp:38
int getLowLevelSocket() const
Definition: Socket.cpp:139
short poll(int timeout_ms, short eventsToLookFor)
Definition: Socket.cpp:83
ssize_t receive(void *buffer, size_t length, int flags=0) const
Definition: Socket.cpp:105
std::shared_ptr< Socket > Ptr
Definition: Socket.h:68
ssize_t send(const void *buffer, size_t length, int flags=0) const
Definition: Socket.cpp:96
void setSocketOption(int level, int optionName, const void *optionValue, socklen_t optionLength) const
Definition: Socket.cpp:129
std::shared_ptr< Address > getAddress() const
Definition: Socket.cpp:135
void getSocketOption(int level, int optionName, void *optionValue, socklen_t *optionLength) const
Definition: Socket.cpp:123
Socket(std::shared_ptr< Address > theAddress)
ssize_t blockingReceive(void *buffer, size_t length) const
Definition: Socket.cpp:114
SelectResult select(int timeout_ms, bool checkRead, bool checkWrite, bool checkError)
Definition: Socket.cpp:61
virtual ~Socket()
Definition: Socket.cpp:29
Namespace of the BsdSockets library.
Definition: Address.cpp:20
int socketDomainToLowLevel(SocketDomain domain)
int socketTypeToLowLevel(SocketType type)