1a06076b2SShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 2a06076b2SShuo Chen// http://code.google.com/p/muduo/ 3a06076b2SShuo Chen// 4a06076b2SShuo Chen// Use of this source code is governed by a BSD-style license 5a06076b2SShuo Chen// that can be found in the License file. 6a06076b2SShuo Chen 7a06076b2SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 8a06076b2SShuo Chen 9a06076b2SShuo Chen#include "Socket.h" 10a06076b2SShuo Chen 11a06076b2SShuo Chen#include "InetAddress.h" 12a06076b2SShuo Chen#include "SocketsOps.h" 13a06076b2SShuo Chen 14a06076b2SShuo Chen#include <netinet/in.h> 15a06076b2SShuo Chen#include <netinet/tcp.h> 16a06076b2SShuo Chen#include <strings.h> // bzero 17a06076b2SShuo Chen 18a06076b2SShuo Chenusing namespace muduo; 19a06076b2SShuo Chen 20a06076b2SShuo ChenSocket::~Socket() 21a06076b2SShuo Chen{ 22a06076b2SShuo Chen sockets::close(sockfd_); 23a06076b2SShuo Chen} 24a06076b2SShuo Chen 25a06076b2SShuo Chenvoid Socket::bindAddress(const InetAddress& addr) 26a06076b2SShuo Chen{ 27a06076b2SShuo Chen sockets::bindOrDie(sockfd_, addr.getSockAddrInet()); 28a06076b2SShuo Chen} 29a06076b2SShuo Chen 30a06076b2SShuo Chenvoid Socket::listen() 31a06076b2SShuo Chen{ 32a06076b2SShuo Chen sockets::listenOrDie(sockfd_); 33a06076b2SShuo Chen} 34a06076b2SShuo Chen 35a06076b2SShuo Chenint Socket::accept(InetAddress* peeraddr) 36a06076b2SShuo Chen{ 37a06076b2SShuo Chen struct sockaddr_in addr; 38a06076b2SShuo Chen bzero(&addr, sizeof addr); 39a06076b2SShuo Chen int connfd = sockets::accept(sockfd_, &addr); 40a06076b2SShuo Chen if (connfd >= 0) 41a06076b2SShuo Chen { 42a06076b2SShuo Chen peeraddr->setSockAddrInet(addr); 43a06076b2SShuo Chen } 44a06076b2SShuo Chen return connfd; 45a06076b2SShuo Chen} 46a06076b2SShuo Chen 47a06076b2SShuo Chenvoid Socket::setReuseAddr(bool on) 48a06076b2SShuo Chen{ 49a06076b2SShuo Chen int optval = on ? 1 : 0; 50a06076b2SShuo Chen ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, 51a06076b2SShuo Chen &optval, sizeof optval); 52a06076b2SShuo Chen // FIXME CHECK 53a06076b2SShuo Chen} 54a06076b2SShuo Chen 55