1// Copyright 2010, Shuo Chen.  All rights reserved.
2// http://code.google.com/p/muduo/
3//
4// Use of this source code is governed by a BSD-style license
5// that can be found in the License file.
6
7// Author: Shuo Chen (chenshuo at chenshuo dot com)
8
9#include "Socket.h"
10
11#include "InetAddress.h"
12#include "SocketsOps.h"
13
14#include <netinet/in.h>
15#include <netinet/tcp.h>
16#include <strings.h>  // bzero
17
18using namespace muduo;
19
20Socket::~Socket()
21{
22  sockets::close(sockfd_);
23}
24
25void Socket::bindAddress(const InetAddress& addr)
26{
27  sockets::bindOrDie(sockfd_, addr.getSockAddrInet());
28}
29
30void Socket::listen()
31{
32  sockets::listenOrDie(sockfd_);
33}
34
35int Socket::accept(InetAddress* peeraddr)
36{
37  struct sockaddr_in addr;
38  bzero(&addr, sizeof addr);
39  int connfd = sockets::accept(sockfd_, &addr);
40  if (connfd >= 0)
41  {
42    peeraddr->setSockAddrInet(addr);
43  }
44  return connfd;
45}
46
47void Socket::setReuseAddr(bool on)
48{
49  int optval = on ? 1 : 0;
50  ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR,
51               &optval, sizeof optval);
52  // FIXME CHECK
53}
54
55void Socket::shutdownWrite()
56{
57  sockets::shutdownWrite(sockfd_);
58}
59
60void Socket::setTcpNoDelay(bool on)
61{
62  int optval = on ? 1 : 0;
63  ::setsockopt(sockfd_, IPPROTO_TCP, TCP_NODELAY,
64               &optval, sizeof optval);
65  // FIXME CHECK
66}
67
68