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