Socket.h revision 354280cf
1354280cfSShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 2354280cfSShuo Chen// http://code.google.com/p/muduo/ 3354280cfSShuo Chen// 4354280cfSShuo Chen// Use of this source code is governed by a BSD-style license 5354280cfSShuo Chen// that can be found in the License file. 6354280cfSShuo Chen 7354280cfSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 8354280cfSShuo Chen// 9354280cfSShuo Chen// This is an internal header file, you should not include this. 10354280cfSShuo Chen 11354280cfSShuo Chen#ifndef MUDUO_NET_SOCKET_H 12354280cfSShuo Chen#define MUDUO_NET_SOCKET_H 13354280cfSShuo Chen 14354280cfSShuo Chen#include <boost/noncopyable.hpp> 15354280cfSShuo Chen 16354280cfSShuo Chennamespace muduo 17354280cfSShuo Chen{ 18354280cfSShuo Chen 19354280cfSShuo Chenclass InetAddress; 20354280cfSShuo Chen 21354280cfSShuo Chen/// 22354280cfSShuo Chen/// Wrapper of socket file descriptor. 23354280cfSShuo Chen/// 24354280cfSShuo Chen/// It closes the sockfd when desctructs. 25354280cfSShuo Chen/// It's thread safe, all operations are delagated to OS. 26354280cfSShuo Chenclass Socket : boost::noncopyable 27354280cfSShuo Chen{ 28354280cfSShuo Chen public: 29354280cfSShuo Chen explicit Socket(int sockfd) 30354280cfSShuo Chen : sockfd_(sockfd) 31354280cfSShuo Chen { } 32354280cfSShuo Chen 33354280cfSShuo Chen ~Socket(); 34354280cfSShuo Chen 35354280cfSShuo Chen int fd() const { return sockfd_; } 36354280cfSShuo Chen 37354280cfSShuo Chen /// abort if address in use 38354280cfSShuo Chen void bindAddress(const InetAddress& localaddr); 39354280cfSShuo Chen /// abort if address in use 40354280cfSShuo Chen void listen(); 41354280cfSShuo Chen 42354280cfSShuo Chen /// On success, returns a non-negative integer that is 43354280cfSShuo Chen /// a descriptor for the accepted socket, which has been 44354280cfSShuo Chen /// set to non-blocking and close-on-exec. *peeraddr is assigned. 45354280cfSShuo Chen /// On error, -1 is returned, and *peeraddr is untouched. 46354280cfSShuo Chen int accept(InetAddress* peeraddr); 47354280cfSShuo Chen 48354280cfSShuo Chen /// 49354280cfSShuo Chen /// Enable/disable SO_REUSEADDR 50354280cfSShuo Chen /// 51354280cfSShuo Chen void setReuseAddr(bool on); 52354280cfSShuo Chen 53354280cfSShuo Chen void shutdownWrite(); 54354280cfSShuo Chen 55354280cfSShuo Chen /// 56354280cfSShuo Chen /// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm). 57354280cfSShuo Chen /// 58354280cfSShuo Chen void setTcpNoDelay(bool on); 59354280cfSShuo Chen 60354280cfSShuo Chen private: 61354280cfSShuo Chen const int sockfd_; 62354280cfSShuo Chen}; 63354280cfSShuo Chen 64354280cfSShuo Chen} 65354280cfSShuo Chen#endif // MUDUO_NET_SOCKET_H 66