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