Socket.h revision 40161064
140161064SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
240161064SShuo Chen// http://code.google.com/p/muduo/
340161064SShuo Chen//
440161064SShuo Chen// Use of this source code is governed by a BSD-style license
540161064SShuo Chen// that can be found in the License file.
640161064SShuo Chen
740161064SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
840161064SShuo Chen//
940161064SShuo Chen// This is an internal header file, you should not include this.
1040161064SShuo Chen
1140161064SShuo Chen#ifndef MUDUO_NET_SOCKET_H
1240161064SShuo Chen#define MUDUO_NET_SOCKET_H
1340161064SShuo Chen
1440161064SShuo Chen#include <boost/noncopyable.hpp>
1540161064SShuo Chen
1640161064SShuo Chennamespace muduo
1740161064SShuo Chen{
1840161064SShuo Chen
1940161064SShuo Chenclass InetAddress;
2040161064SShuo Chen
2140161064SShuo Chen///
2240161064SShuo Chen/// Wrapper of socket file descriptor.
2340161064SShuo Chen///
2440161064SShuo Chen/// It closes the sockfd when desctructs.
2540161064SShuo Chen/// It's thread safe, all operations are delagated to OS.
2640161064SShuo Chenclass Socket : boost::noncopyable
2740161064SShuo Chen{
2840161064SShuo Chen public:
2940161064SShuo Chen  explicit Socket(int sockfd)
3040161064SShuo Chen    : sockfd_(sockfd)
3140161064SShuo Chen  { }
3240161064SShuo Chen
3340161064SShuo Chen  ~Socket();
3440161064SShuo Chen
3540161064SShuo Chen  int fd() const { return sockfd_; }
3640161064SShuo Chen
3740161064SShuo Chen  /// abort if address in use
3840161064SShuo Chen  void bindAddress(const InetAddress& localaddr);
3940161064SShuo Chen  /// abort if address in use
4040161064SShuo Chen  void listen();
4140161064SShuo Chen
4240161064SShuo Chen  /// On success, returns a non-negative integer that is
4340161064SShuo Chen  /// a descriptor for the accepted socket, which has been
4440161064SShuo Chen  /// set to non-blocking and close-on-exec. *peeraddr is assigned.
4540161064SShuo Chen  /// On error, -1 is returned, and *peeraddr is untouched.
4640161064SShuo Chen  int accept(InetAddress* peeraddr);
4740161064SShuo Chen
4840161064SShuo Chen  ///
4940161064SShuo Chen  /// Enable/disable SO_REUSEADDR
5040161064SShuo Chen  ///
5140161064SShuo Chen  void setReuseAddr(bool on);
5240161064SShuo Chen
5340161064SShuo Chen  void shutdownWrite();
5440161064SShuo Chen
5540161064SShuo Chen  ///
5640161064SShuo Chen  /// Enable/disable TCP_NODELAY (disable/enable Nagle's algorithm).
5740161064SShuo Chen  ///
5840161064SShuo Chen  void setTcpNoDelay(bool on);
5940161064SShuo Chen
6040161064SShuo Chen private:
6140161064SShuo Chen  const int sockfd_;
6240161064SShuo Chen};
6340161064SShuo Chen
6440161064SShuo Chen}
6540161064SShuo Chen#endif  // MUDUO_NET_SOCKET_H
66