19a1e991dSShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
29a1e991dSShuo Chen// http://code.google.com/p/muduo/
39a1e991dSShuo Chen//
49a1e991dSShuo Chen// Use of this source code is governed by a BSD-style license
59a1e991dSShuo Chen// that can be found in the License file.
69a1e991dSShuo Chen
79a1e991dSShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
89a1e991dSShuo Chen//
99a1e991dSShuo Chen// This is an internal header file, you should not include this.
109a1e991dSShuo Chen
119a1e991dSShuo Chen#ifndef MUDUO_NET_SOCKET_H
129a1e991dSShuo Chen#define MUDUO_NET_SOCKET_H
139a1e991dSShuo Chen
149a1e991dSShuo Chen#include <boost/noncopyable.hpp>
159a1e991dSShuo Chen
169a1e991dSShuo Chennamespace muduo
179a1e991dSShuo Chen{
189a1e991dSShuo Chen
199a1e991dSShuo Chenclass InetAddress;
209a1e991dSShuo Chen
219a1e991dSShuo Chen///
229a1e991dSShuo Chen/// Wrapper of socket file descriptor.
239a1e991dSShuo Chen///
249a1e991dSShuo Chen/// It closes the sockfd when desctructs.
259a1e991dSShuo Chen/// It's thread safe, all operations are delagated to OS.
269a1e991dSShuo Chenclass Socket : boost::noncopyable
279a1e991dSShuo Chen{
289a1e991dSShuo Chen public:
299a1e991dSShuo Chen  explicit Socket(int sockfd)
309a1e991dSShuo Chen    : sockfd_(sockfd)
319a1e991dSShuo Chen  { }
329a1e991dSShuo Chen
339a1e991dSShuo Chen  ~Socket();
349a1e991dSShuo Chen
359a1e991dSShuo Chen  int fd() const { return sockfd_; }
369a1e991dSShuo Chen
379a1e991dSShuo Chen  /// abort if address in use
389a1e991dSShuo Chen  void bindAddress(const InetAddress& localaddr);
399a1e991dSShuo Chen  /// abort if address in use
409a1e991dSShuo Chen  void listen();
419a1e991dSShuo Chen
429a1e991dSShuo Chen  /// On success, returns a non-negative integer that is
439a1e991dSShuo Chen  /// a descriptor for the accepted socket, which has been
449a1e991dSShuo Chen  /// set to non-blocking and close-on-exec. *peeraddr is assigned.
459a1e991dSShuo Chen  /// On error, -1 is returned, and *peeraddr is untouched.
469a1e991dSShuo Chen  int accept(InetAddress* peeraddr);
479a1e991dSShuo Chen
489a1e991dSShuo Chen  ///
499a1e991dSShuo Chen  /// Enable/disable SO_REUSEADDR
509a1e991dSShuo Chen  ///
519a1e991dSShuo Chen  void setReuseAddr(bool on);
529a1e991dSShuo Chen
539a1e991dSShuo Chen private:
549a1e991dSShuo Chen  const int sockfd_;
559a1e991dSShuo Chen};
569a1e991dSShuo Chen
579a1e991dSShuo Chen}
589a1e991dSShuo Chen#endif  // MUDUO_NET_SOCKET_H
59