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