12a18e699SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
22a18e699SShuo Chen// http://code.google.com/p/muduo/
32a18e699SShuo Chen//
42a18e699SShuo Chen// Use of this source code is governed by a BSD-style license
52a18e699SShuo Chen// that can be found in the License file.
62a18e699SShuo Chen
72a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
82a18e699SShuo Chen//
92a18e699SShuo Chen// This is an internal header file, you should not include this.
102a18e699SShuo Chen
112a18e699SShuo Chen#ifndef MUDUO_NET_SOCKET_H
122a18e699SShuo Chen#define MUDUO_NET_SOCKET_H
132a18e699SShuo Chen
142a18e699SShuo Chen#include <boost/noncopyable.hpp>
152a18e699SShuo Chen
162a18e699SShuo Chennamespace muduo
172a18e699SShuo Chen{
182a18e699SShuo Chen
192a18e699SShuo Chenclass InetAddress;
202a18e699SShuo Chen
212a18e699SShuo Chen///
222a18e699SShuo Chen/// Wrapper of socket file descriptor.
232a18e699SShuo Chen///
242a18e699SShuo Chen/// It closes the sockfd when desctructs.
252a18e699SShuo Chen/// It's thread safe, all operations are delagated to OS.
262a18e699SShuo Chenclass Socket : boost::noncopyable
272a18e699SShuo Chen{
282a18e699SShuo Chen public:
292a18e699SShuo Chen  explicit Socket(int sockfd)
302a18e699SShuo Chen    : sockfd_(sockfd)
312a18e699SShuo Chen  { }
322a18e699SShuo Chen
332a18e699SShuo Chen  ~Socket();
342a18e699SShuo Chen
352a18e699SShuo Chen  int fd() const { return sockfd_; }
362a18e699SShuo Chen
372a18e699SShuo Chen  /// abort if address in use
382a18e699SShuo Chen  void bindAddress(const InetAddress& localaddr);
392a18e699SShuo Chen  /// abort if address in use
402a18e699SShuo Chen  void listen();
412a18e699SShuo Chen
422a18e699SShuo Chen  /// On success, returns a non-negative integer that is
432a18e699SShuo Chen  /// a descriptor for the accepted socket, which has been
442a18e699SShuo Chen  /// set to non-blocking and close-on-exec. *peeraddr is assigned.
452a18e699SShuo Chen  /// On error, -1 is returned, and *peeraddr is untouched.
462a18e699SShuo Chen  int accept(InetAddress* peeraddr);
472a18e699SShuo Chen
482a18e699SShuo Chen  ///
492a18e699SShuo Chen  /// Enable/disable SO_REUSEADDR
502a18e699SShuo Chen  ///
512a18e699SShuo Chen  void setReuseAddr(bool on);
522a18e699SShuo Chen
53129fe122SShuo Chen  void shutdownWrite();
54129fe122SShuo Chen
552a18e699SShuo Chen private:
562a18e699SShuo Chen  const int sockfd_;
572a18e699SShuo Chen};
582a18e699SShuo Chen
592a18e699SShuo Chen}
602a18e699SShuo Chen#endif  // MUDUO_NET_SOCKET_H
61