15af4b7fbSShuo Chen // Copyright 2010, Shuo Chen. All rights reserved. 25af4b7fbSShuo Chen // http://code.google.com/p/muduo/ 35af4b7fbSShuo Chen // 45af4b7fbSShuo Chen // Use of this source code is governed by a BSD-style license 55af4b7fbSShuo Chen // that can be found in the License file. 65af4b7fbSShuo Chen 75af4b7fbSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com) 85af4b7fbSShuo Chen 95af4b7fbSShuo Chen #include "Socket.h" 105af4b7fbSShuo Chen 115af4b7fbSShuo Chen #include "InetAddress.h" 125af4b7fbSShuo Chen #include "SocketsOps.h" 135af4b7fbSShuo Chen 145af4b7fbSShuo Chen #include <netinet/in.h> 155af4b7fbSShuo Chen #include <netinet/tcp.h> 165af4b7fbSShuo Chen #include <strings.h> // bzero 175af4b7fbSShuo Chen 185af4b7fbSShuo Chen using namespace muduo; 195af4b7fbSShuo Chen 205af4b7fbSShuo Chen Socket::~Socket() 215af4b7fbSShuo Chen { 225af4b7fbSShuo Chen sockets::close(sockfd_); 235af4b7fbSShuo Chen } 245af4b7fbSShuo Chen 255af4b7fbSShuo Chen void Socket::bindAddress(const InetAddress& addr) 265af4b7fbSShuo Chen { 275af4b7fbSShuo Chen sockets::bindOrDie(sockfd_, addr.getSockAddrInet()); 285af4b7fbSShuo Chen } 295af4b7fbSShuo Chen 305af4b7fbSShuo Chen void Socket::listen() 315af4b7fbSShuo Chen { 325af4b7fbSShuo Chen sockets::listenOrDie(sockfd_); 335af4b7fbSShuo Chen } 345af4b7fbSShuo Chen 355af4b7fbSShuo Chen int Socket::accept(InetAddress* peeraddr) 365af4b7fbSShuo Chen { 375af4b7fbSShuo Chen struct sockaddr_in addr; 385af4b7fbSShuo Chen bzero(&addr, sizeof addr); 395af4b7fbSShuo Chen int connfd = sockets::accept(sockfd_, &addr); 405af4b7fbSShuo Chen if (connfd >= 0) 415af4b7fbSShuo Chen { 425af4b7fbSShuo Chen peeraddr->setSockAddrInet(addr); 435af4b7fbSShuo Chen } 445af4b7fbSShuo Chen return connfd; 455af4b7fbSShuo Chen } 465af4b7fbSShuo Chen 475af4b7fbSShuo Chen void Socket::setReuseAddr(bool on) 485af4b7fbSShuo Chen { 495af4b7fbSShuo Chen int optval = on ? 1 : 0; 505af4b7fbSShuo Chen ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR, 515af4b7fbSShuo Chen &optval, sizeof optval); 525af4b7fbSShuo Chen // FIXME CHECK 535af4b7fbSShuo Chen } 545af4b7fbSShuo Chen 555af4b7fbSShuo Chen void Socket::shutdownWrite() 565af4b7fbSShuo Chen { 575af4b7fbSShuo Chen sockets::shutdownWrite(sockfd_); 585af4b7fbSShuo Chen } 595af4b7fbSShuo Chen 605af4b7fbSShuo Chen+void Socket::setTcpNoDelay(bool on) 615af4b7fbSShuo Chen+{ 625af4b7fbSShuo Chen+ int optval = on ? 1 : 0; 635af4b7fbSShuo Chen+ ::setsockopt(sockfd_, IPPROTO_TCP, TCP_NODELAY, 645af4b7fbSShuo Chen+ &optval, sizeof optval); 655af4b7fbSShuo Chen+ // FIXME CHECK 665af4b7fbSShuo Chen+} 675af4b7fbSShuo Chen+ 68