15 #include <sys/socket.h>
16 #include <system_error>
30 class AcceptedSocketData {
37 AcceptedSocketData(
int theLowLevelSocket,
Address::Ptr theAddress)
38 :lowLevelSocket(theLowLevelSocket), address(theAddress)
44 const int lowLevelSocket;
57 AcceptedSocketData lowLevelAccept(
int serverLowLevelSocket,
Address::Ptr serverAddress) {
61 const int lowLevelSocket = ::accept(serverLowLevelSocket, &lowLevelAddress->getSockAddr(), &len);
64 if(len > lowLevelAddress->getSockAddrLen()) {
65 throw std::runtime_error(
"accept over-write the length of the sockaddr for the ServerSocket Address");
68 if(-1 == lowLevelSocket) {
69 throw std::system_error(errno, std::system_category());
75 if(AF_UNSPEC == lowLevelAddress->getSockAddr().sa_family) {
76 lowLevelAddress->getSockAddr().sa_family = serverAddress->getLowLevelAddress().getSockAddr().sa_family;
80 return AcceptedSocketData(lowLevelSocket, serverAddress->create(lowLevelAddress));
95 socket->setSocketOption(SOL_SOCKET, SO_REUSEADDR, &reuse,
sizeof(reuse));
96 socket->setSocketOption(SOL_SOCKET, SO_REUSEPORT, &reuse,
sizeof(reuse));
99 socket->listen(backlog);
115 throw std::system_error(errno, std::system_category());
121 throw std::system_error(errno, std::system_category());
135 return std::thread ([=]() {
std::shared_ptr< Address > Ptr
Interface for the low-level address implementation.
std::shared_ptr< LowLevelAddress > Ptr
virtual socklen_t getSockAddrLen() const =0
virtual const struct sockaddr & getSockAddr() const =0
std::thread acceptInNewThread(std::function< void(Socket::Ptr)> toCall)
static ServerSocket::Ptr open(std::shared_ptr< Address > theAddress, int backlog=128, bool reuse=false)
Socket::Ptr accept() const
static ServerSocket::Ptr create(std::shared_ptr< Address > theAddress)
void listen(int backlog=128) const
std::shared_ptr< ServerSocket > Ptr
ServerSocket(std::shared_ptr< Address > theAddress)
BSD Socket base class providing the basic, common functionality between ServerSockets and ClientSocke...
static Socket::Ptr create(std::shared_ptr< Address > theAddress, int existingLowLevelSocket)
int getLowLevelSocket() const
std::shared_ptr< Socket > Ptr
std::shared_ptr< Address > getAddress() const
Namespace of the BsdSockets library.