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