00001
00002
00003
00004
00005
00006
00007
00008
00009 static const char rcsid[] __attribute__((used)) = "$Id: TCPSocket.cpp 1197 2014-10-14 22:26:11Z sella $";
00010
00011 #include "TCPSocket.h"
00012
00013 #include <sys/types.h>
00014 #include <sys/socket.h>
00015 #include <string.h>
00016
00017 using namespace sella::net;
00018
00019 TCPSocket::TCPSocket(int family) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
00020 }
00021
00022 TCPSocket::TCPSocket(int family, const IPAddressPort &addressport) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
00023 connect(addressport);
00024 }
00025
00026 TCPSocket::TCPSocket(int family, const IPAddress &address, const Port &port) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
00027 connect(address, port);
00028 }
00029
00030 TCPSocket::TCPSocket(int family, const std::string &address, unsigned short port) throw (SocketException) : Socket(family, SOCK_STREAM, IPPROTO_TCP) {
00031 connect(address, port);
00032 }
00033
00034 TCPSocket::TCPSocket(const IPAddressPort &addressport) throw (SocketException) : Socket(addressport.getFamily(), SOCK_STREAM, IPPROTO_TCP) {
00035 connect(addressport);
00036 }
00037
00038 TCPSocket::TCPSocket(const IPAddress &address, const Port &port) throw (SocketException) : Socket(address.getFamily(), SOCK_STREAM, IPPROTO_TCP) {
00039 connect(address, port);
00040 }
00041
00042 TCPSocket::TCPSocket(int s, bool unused) throw (SocketException) : Socket(s) {
00043 usable = true;
00044 }
00045
00046 TCPSocket::~TCPSocket() {
00047 }
00048
00049 void TCPSocket::listen(int backlog) throw (SocketException) {
00050 if (::listen(s, backlog) < 0) {
00051 THROW2(SocketException, "listen()");
00052 }
00053 }
00054
00055 TCPSocket::shared TCPSocket::accept(void) throw (SocketException) {
00056 int fd;
00057
00058 if ((fd = ::accept(s, NULL, NULL)) < 0) {
00059 THROW2(SocketException, "accept()");
00060 }
00061
00062 const TCPSocket::shared socket(new TCPSocket(fd, true));
00063
00064 return socket;
00065 }
00066
00067 const TCPSocket::shared TCPSocket::shared_ptr(int family) throw (SocketException) {
00068 return std::make_shared<TCPSocket>(family);
00069 }
00070
00071 const TCPSocket::shared TCPSocket::shared_ptr(int family, const IPAddressPort &addressport) throw (SocketException) {
00072 return std::make_shared<TCPSocket>(family, addressport);
00073 }
00074
00075 const TCPSocket::shared TCPSocket::shared_ptr(int family, const IPAddress &address, const Port &port) throw (SocketException) {
00076 return std::make_shared<TCPSocket>(family, address, port);
00077 }
00078
00079 const TCPSocket::shared TCPSocket::shared_ptr(int family, const std::string &address, unsigned short port) throw (SocketException) {
00080 return std::make_shared<TCPSocket>(family, address, port);
00081 }
00082
00083 const TCPSocket::shared TCPSocket::shared_ptr(const IPAddressPort &addressport) throw (SocketException) {
00084 return std::make_shared<TCPSocket>(addressport);
00085 }
00086
00087 const TCPSocket::shared TCPSocket::shared_ptr(const IPAddress &address, const Port &port) throw (SocketException) {
00088 return std::make_shared<TCPSocket>(address, port);
00089 }
00090
00091
00092
00093