1#include "Acceptor.h" 2 3#include "InetAddress.h" 4#include "TcpStream.h" 5 6#include <stdio.h> 7#include <sys/socket.h> 8 9Acceptor::Acceptor(const InetAddress& listenAddr) 10 : listenSock_(Socket::createTCP(listenAddr.family())) 11{ 12 listenSock_.setReuseAddr(true); 13 listenSock_.bindOrDie(listenAddr); 14 listenSock_.listenOrDie(); 15} 16 17TcpStreamPtr Acceptor::accept() 18{ 19 // FIXME: use accept4 20 int sockfd = ::accept(listenSock_.fd(), NULL, NULL); 21 if (sockfd >= 0) 22 { 23 return TcpStreamPtr(new TcpStream(Socket(sockfd))); 24 } 25 else 26 { 27 perror("Acceptor::accept"); 28 return TcpStreamPtr(); 29 } 30} 31 32Socket Acceptor::acceptSocketOrDie() 33{ 34 // FIXME: use accept4 35 int sockfd = ::accept(listenSock_.fd(), NULL, NULL); 36 if (sockfd >= 0) 37 { 38 return Socket(sockfd); 39 } 40 else 41 { 42 perror("Acceptor::acceptSocketOrDie"); 43 abort(); 44 } 45} 46