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