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