libutil++  1.9.3
 All Classes Functions Variables
TCPSocket.cpp
1 /*
2 ** libutil++
3 ** $Id: TCPSocket.cpp 1653 2016-02-28 19:54:59Z sella $
4 ** Copyright (c) 2011-2016 Digital Genesis, LLC. All Rights Reserved.
5 ** Released under the LGPL Version 2.1 License.
6 ** http://www.digitalgenesis.com
7 */
8 
9 static const char rcsid[] __attribute__((used)) = "$Id: TCPSocket.cpp 1653 2016-02-28 19:54:59Z sella $";
10 
11 #include "TCPSocket.h"
12 
13 #include <sys/types.h>
14 #include <sys/socket.h>
15 #include <string.h>
16 
17 using namespace sella::net;
18 
19 TCPSocket::TCPSocket(int family) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
20 }
21 
22 TCPSocket::TCPSocket(int family, int type, int protocol) throw (SocketException) : Socket(family, type, protocol) {
23 }
24 
25 TCPSocket::TCPSocket(int family, const IPAddressPort &addressport) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
26  connect(addressport);
27 }
28 
29 TCPSocket::TCPSocket(int family, const IPAddress &address, const Port &port) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
30  connect(address, port);
31 }
32 
33 TCPSocket::TCPSocket(int family, const std::string &address, unsigned short port) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
34  connect(address, port);
35 }
36 
37 TCPSocket::TCPSocket(const IPAddressPort &addressport) throw (SocketException) : Socket(addressport.getFamily(), SOCK_STREAM, IPPROTO_TCP) {
38  connect(addressport);
39 }
40 
41 TCPSocket::TCPSocket(const IPAddress &address, const Port &port) throw (SocketException) : Socket(address.getFamily(), SOCK_STREAM, IPPROTO_TCP) {
42  connect(address, port);
43 }
44 
45 TCPSocket::TCPSocket(int s, bool unused) throw (SocketException) : Socket(s) {
46  usable = true;
47 }
48 
49 TCPSocket::~TCPSocket() {
50 }
51 
52 void TCPSocket::listen(int backlog) throw (SocketException) {
53  if (::listen(s, backlog) < 0) {
54  THROW2(SocketException, "listen()");
55  }
56 }
57 
58 TCPSocket::shared TCPSocket::accept(void) throw (SocketException) {
59  int fd;
60 
61  if ((fd = ::accept(s, NULL, NULL)) < 0) {
62  THROW2(SocketException, "accept()");
63  }
64 
65  const TCPSocket::shared socket(new TCPSocket(fd, true));
66 
67  return socket;
68 }
69 
70 const TCPSocket::shared TCPSocket::shared_ptr(int family) throw (SocketException) {
71  return std::make_shared<TCPSocket>(family);
72 }
73 
74 const TCPSocket::shared TCPSocket::shared_ptr(int family, const IPAddressPort &addressport) throw (SocketException) {
75  return std::make_shared<TCPSocket>(family, addressport);
76 }
77 
78 const TCPSocket::shared TCPSocket::shared_ptr(int family, const IPAddress &address, const Port &port) throw (SocketException) {
79  return std::make_shared<TCPSocket>(family, address, port);
80 }
81 
82 const TCPSocket::shared TCPSocket::shared_ptr(int family, const std::string &address, unsigned short port) throw (SocketException) {
83  return std::make_shared<TCPSocket>(family, address, port);
84 }
85 
86 const TCPSocket::shared TCPSocket::shared_ptr(const IPAddressPort &addressport) throw (SocketException) {
87  return std::make_shared<TCPSocket>(addressport);
88 }
89 
90 const TCPSocket::shared TCPSocket::shared_ptr(const IPAddress &address, const Port &port) throw (SocketException) {
91  return std::make_shared<TCPSocket>(address, port);
92 }
93 
94 /*
95 ** vim: noet ts=3 sw=3
96 */