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