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