11bc8a9dfSShuo Chen // Copyright 2010, Shuo Chen.  All rights reserved.
21bc8a9dfSShuo Chen // http://code.google.com/p/muduo/
31bc8a9dfSShuo Chen //
41bc8a9dfSShuo Chen // Use of this source code is governed by a BSD-style license
51bc8a9dfSShuo Chen // that can be found in the License file.
61bc8a9dfSShuo Chen 
71bc8a9dfSShuo Chen // Author: Shuo Chen (chenshuo at chenshuo dot com)
81bc8a9dfSShuo Chen 
91bc8a9dfSShuo Chen #include "Socket.h"
101bc8a9dfSShuo Chen 
111bc8a9dfSShuo Chen #include "InetAddress.h"
121bc8a9dfSShuo Chen #include "SocketsOps.h"
131bc8a9dfSShuo Chen 
141bc8a9dfSShuo Chen #include <netinet/in.h>
151bc8a9dfSShuo Chen #include <netinet/tcp.h>
161bc8a9dfSShuo Chen #include <strings.h>  // bzero
171bc8a9dfSShuo Chen 
181bc8a9dfSShuo Chen using namespace muduo;
191bc8a9dfSShuo Chen 
201bc8a9dfSShuo Chen Socket::~Socket()
211bc8a9dfSShuo Chen {
221bc8a9dfSShuo Chen   sockets::close(sockfd_);
231bc8a9dfSShuo Chen }
241bc8a9dfSShuo Chen 
251bc8a9dfSShuo Chen void Socket::bindAddress(const InetAddress& addr)
261bc8a9dfSShuo Chen {
271bc8a9dfSShuo Chen   sockets::bindOrDie(sockfd_, addr.getSockAddrInet());
281bc8a9dfSShuo Chen }
291bc8a9dfSShuo Chen 
301bc8a9dfSShuo Chen void Socket::listen()
311bc8a9dfSShuo Chen {
321bc8a9dfSShuo Chen   sockets::listenOrDie(sockfd_);
331bc8a9dfSShuo Chen }
341bc8a9dfSShuo Chen 
351bc8a9dfSShuo Chen int Socket::accept(InetAddress* peeraddr)
361bc8a9dfSShuo Chen {
371bc8a9dfSShuo Chen   struct sockaddr_in addr;
381bc8a9dfSShuo Chen   bzero(&addr, sizeof addr);
391bc8a9dfSShuo Chen   int connfd = sockets::accept(sockfd_, &addr);
401bc8a9dfSShuo Chen   if (connfd >= 0)
411bc8a9dfSShuo Chen   {
421bc8a9dfSShuo Chen     peeraddr->setSockAddrInet(addr);
431bc8a9dfSShuo Chen   }
441bc8a9dfSShuo Chen   return connfd;
451bc8a9dfSShuo Chen }
461bc8a9dfSShuo Chen 
471bc8a9dfSShuo Chen void Socket::setReuseAddr(bool on)
481bc8a9dfSShuo Chen {
491bc8a9dfSShuo Chen   int optval = on ? 1 : 0;
501bc8a9dfSShuo Chen   ::setsockopt(sockfd_, SOL_SOCKET, SO_REUSEADDR,
511bc8a9dfSShuo Chen                &optval, sizeof optval);
521bc8a9dfSShuo Chen   // FIXME CHECK
531bc8a9dfSShuo Chen }
541bc8a9dfSShuo Chen 
551bc8a9dfSShuo Chen+void Socket::shutdownWrite()
561bc8a9dfSShuo Chen+{
571bc8a9dfSShuo Chen+  sockets::shutdownWrite(sockfd_);
581bc8a9dfSShuo Chen+}
591bc8a9dfSShuo Chen+
60