Socket.h revision 65c497a3
165c497a3SShuo Chen// Copyright 2010, Shuo Chen. All rights reserved. 265c497a3SShuo Chen// http://code.google.com/p/muduo/ 365c497a3SShuo Chen// 465c497a3SShuo Chen// Use of this source code is governed by a BSD-style license 565c497a3SShuo Chen// that can be found in the License file. 665c497a3SShuo Chen 765c497a3SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com) 865c497a3SShuo Chen// 965c497a3SShuo Chen// This is an internal header file, you should not include this. 1065c497a3SShuo Chen 1165c497a3SShuo Chen#ifndef MUDUO_NET_SOCKET_H 1265c497a3SShuo Chen#define MUDUO_NET_SOCKET_H 1365c497a3SShuo Chen 1465c497a3SShuo Chen#include <boost/noncopyable.hpp> 1565c497a3SShuo Chen 1665c497a3SShuo Chennamespace muduo 1765c497a3SShuo Chen{ 1865c497a3SShuo Chen 1965c497a3SShuo Chenclass InetAddress; 2065c497a3SShuo Chen 2165c497a3SShuo Chen/// 2265c497a3SShuo Chen/// Wrapper of socket file descriptor. 2365c497a3SShuo Chen/// 2465c497a3SShuo Chen/// It closes the sockfd when desctructs. 2565c497a3SShuo Chen/// It's thread safe, all operations are delagated to OS. 2665c497a3SShuo Chenclass Socket : boost::noncopyable 2765c497a3SShuo Chen{ 2865c497a3SShuo Chen public: 2965c497a3SShuo Chen explicit Socket(int sockfd) 3065c497a3SShuo Chen : sockfd_(sockfd) 3165c497a3SShuo Chen { } 3265c497a3SShuo Chen 3365c497a3SShuo Chen ~Socket(); 3465c497a3SShuo Chen 3565c497a3SShuo Chen int fd() const { return sockfd_; } 3665c497a3SShuo Chen 3765c497a3SShuo Chen /// abort if address in use 3865c497a3SShuo Chen void bindAddress(const InetAddress& localaddr); 3965c497a3SShuo Chen /// abort if address in use 4065c497a3SShuo Chen void listen(); 4165c497a3SShuo Chen 4265c497a3SShuo Chen /// On success, returns a non-negative integer that is 4365c497a3SShuo Chen /// a descriptor for the accepted socket, which has been 4465c497a3SShuo Chen /// set to non-blocking and close-on-exec. *peeraddr is assigned. 4565c497a3SShuo Chen /// On error, -1 is returned, and *peeraddr is untouched. 4665c497a3SShuo Chen int accept(InetAddress* peeraddr); 4765c497a3SShuo Chen 4865c497a3SShuo Chen /// 4965c497a3SShuo Chen /// Enable/disable SO_REUSEADDR 5065c497a3SShuo Chen /// 5165c497a3SShuo Chen void setReuseAddr(bool on); 5265c497a3SShuo Chen 5365c497a3SShuo Chen private: 5465c497a3SShuo Chen const int sockfd_; 5565c497a3SShuo Chen}; 5665c497a3SShuo Chen 5765c497a3SShuo Chen} 5865c497a3SShuo Chen#endif // MUDUO_NET_SOCKET_H 59