1a06076b2SShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 2a06076b2SShuo Chen// http://code.google.com/p/muduo/ 3a06076b2SShuo Chen// 4a06076b2SShuo Chen// Use of this source code is governed by a BSD-style license 5a06076b2SShuo Chen// that can be found in the License file. 6a06076b2SShuo Chen 7a06076b2SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 8a06076b2SShuo Chen// 9a06076b2SShuo Chen// This is an internal header file, you should not include this. 10a06076b2SShuo Chen 11a06076b2SShuo Chen#ifndef MUDUO_NET_SOCKET_H 12a06076b2SShuo Chen#define MUDUO_NET_SOCKET_H 13a06076b2SShuo Chen 14a06076b2SShuo Chen#include <boost/noncopyable.hpp> 15a06076b2SShuo Chen 16a06076b2SShuo Chennamespace muduo 17a06076b2SShuo Chen{ 18a06076b2SShuo Chen 19a06076b2SShuo Chenclass InetAddress; 20a06076b2SShuo Chen 21a06076b2SShuo Chen/// 22a06076b2SShuo Chen/// Wrapper of socket file descriptor. 23a06076b2SShuo Chen/// 24a06076b2SShuo Chen/// It closes the sockfd when desctructs. 25a06076b2SShuo Chen/// It's thread safe, all operations are delagated to OS. 26a06076b2SShuo Chenclass Socket : boost::noncopyable 27a06076b2SShuo Chen{ 28a06076b2SShuo Chen public: 29a06076b2SShuo Chen explicit Socket(int sockfd) 30a06076b2SShuo Chen : sockfd_(sockfd) 31a06076b2SShuo Chen { } 32a06076b2SShuo Chen 33a06076b2SShuo Chen ~Socket(); 34a06076b2SShuo Chen 35a06076b2SShuo Chen int fd() const { return sockfd_; } 36a06076b2SShuo Chen 37a06076b2SShuo Chen /// abort if address in use 38a06076b2SShuo Chen void bindAddress(const InetAddress& localaddr); 39a06076b2SShuo Chen /// abort if address in use 40a06076b2SShuo Chen void listen(); 41a06076b2SShuo Chen 42a06076b2SShuo Chen /// On success, returns a non-negative integer that is 43a06076b2SShuo Chen /// a descriptor for the accepted socket, which has been 44a06076b2SShuo Chen /// set to non-blocking and close-on-exec. *peeraddr is assigned. 45a06076b2SShuo Chen /// On error, -1 is returned, and *peeraddr is untouched. 46a06076b2SShuo Chen int accept(InetAddress* peeraddr); 47a06076b2SShuo Chen 48a06076b2SShuo Chen /// 49a06076b2SShuo Chen /// Enable/disable SO_REUSEADDR 50a06076b2SShuo Chen /// 51a06076b2SShuo Chen void setReuseAddr(bool on); 52a06076b2SShuo Chen 53a06076b2SShuo Chen private: 54a06076b2SShuo Chen const int sockfd_; 55a06076b2SShuo Chen}; 56a06076b2SShuo Chen 57a06076b2SShuo Chen} 58a06076b2SShuo Chen#endif // MUDUO_NET_SOCKET_H 59