libBsdSockets
C++ Wrapper classes to the BSD Socket API
ClientSocket.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 <system_error>
17 #include <memory>
18 
19 #include "ClientSocket.h"
20 #include "Address.h"
21 #include "LowLevelAddress.h"
22 
23 namespace BsdSockets {
24 
26  return ClientSocket::Ptr (new ClientSocket(theAddress));
27  }
28 
30  ClientSocket::Ptr socket = create(theAddress);
31  socket->connect();
32  return socket;
33  }
34 
36  : Socket(theAddress)
37  {
38  }
39 
41  }
42 
43  void ClientSocket::connect() const {
44  const LowLevelAddress& lowLevelAddress = getAddress()->getLowLevelAddress();
45 
46  if(-1 == ::connect(getLowLevelSocket(), &lowLevelAddress.getSockAddr(), lowLevelAddress.getSockAddrLen())) {
47  throw std::system_error(errno, std::system_category());
48  }
49  }
50 
51 } // namespace BsdSockets
std::shared_ptr< Address > Ptr
Definition: Address.h:38
ClientSocket(std::shared_ptr< Address > theAddress)
static ClientSocket::Ptr open(std::shared_ptr< Address > theAddress)
static ClientSocket::Ptr create(std::shared_ptr< Address > theAddress)
std::shared_ptr< ClientSocket > Ptr
Definition: ClientSocket.h:37
Interface for the low-level address implementation.
virtual socklen_t getSockAddrLen() const =0
virtual const struct sockaddr & getSockAddr() const =0
BSD Socket base class providing the basic, common functionality between ServerSockets and ClientSocke...
Definition: Socket.h:66
int getLowLevelSocket() const
Definition: Socket.cpp:139
std::shared_ptr< Address > getAddress() const
Definition: Socket.cpp:135
Namespace of the BsdSockets library.
Definition: Address.cpp:20