12a18e699SShuo Chen// Copyright 2010, Shuo Chen.  All rights reserved.
22a18e699SShuo Chen// http://code.google.com/p/muduo/
32a18e699SShuo Chen//
42a18e699SShuo Chen// Use of this source code is governed by a BSD-style license
52a18e699SShuo Chen// that can be found in the License file.
62a18e699SShuo Chen
72a18e699SShuo Chen// Author: Shuo Chen (chenshuo at chenshuo dot com)
82a18e699SShuo Chen
92a18e699SShuo Chen#include "Socket.h"
102a18e699SShuo Chen
112a18e699SShuo Chen#include "InetAddress.h"
122a18e699SShuo Chen#include "SocketsOps.h"
132a18e699SShuo Chen
142a18e699SShuo Chen#include <netinet/in.h>
152a18e699SShuo Chen#include <netinet/tcp.h>
162a18e699SShuo Chen#include <strings.h>  // bzero
172a18e699SShuo Chen
182a18e699SShuo Chenusing namespace muduo;
192a18e699SShuo Chen
202a18e699SShuo ChenSocket::~Socket()
212a18e699SShuo Chen{
222a18e699SShuo Chen  sockets::close(sockfd_);
232a18e699SShuo Chen}
242a18e699SShuo Chen
252a18e699SShuo Chenvoid Socket::bindAddress(const InetAddress& addr)
262a18e699SShuo Chen{
272a18e699SShuo Chen  sockets::bindOrDie(sockfd_, addr.getSockAddrInet());
282a18e699SShuo Chen}
292a18e699SShuo Chen
302a18e699SShuo Chenvoid Socket::listen()
312a18e699SShuo Chen{
322a18e699SShuo Chen  sockets::listenOrDie(sockfd_);
332a18e699SShuo Chen}
342a18e699SShuo Chen
352a18e699SShuo Chenint Socket::accept(InetAddress* peeraddr)
362a18e699SShuo Chen{
372a18e699SShuo Chen  struct sockaddr_in addr;
382a18e699SShuo Chen  bzero(&addr, sizeof addr);
392a18e699SShuo Chen  int connfd = sockets::accept(sockfd_, &addr);
402a18e699SShuo Chen  if (connfd >= 0)
412a18e699SShuo Chen  {
422a18e699SShuo Chen    peeraddr->setSockAddrInet(addr);
432a18e699SShuo Chen  }
442a18e699SShuo Chen  return connfd;
452a18e699SShuo Chen}
462a18e699SShuo Chen
472a18e699SShuo Chenvoid Socket::setReuseAddr(bool on)
482a18e699SShuo Chen{
492a18e699SShuo Chen  int optval = on ? 1 : 0;
502a18e699SShuo Chen  ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR,
512a18e699SShuo Chen               &optval, sizeof optval);
522a18e699SShuo Chen  // FIXME CHECK
532a18e699SShuo Chen}
542a18e699SShuo Chen
55129fe122SShuo Chenvoid Socket::shutdownWrite()
56129fe122SShuo Chen{
57129fe122SShuo Chen  sockets::shutdownWrite(sockfd_);
58129fe122SShuo Chen}
59129fe122SShuo Chen
60