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