Fawkes API Fawkes Development Version

stream.cpp

00001 
00002 /***************************************************************************
00003  *  stream.cpp - Fawkes stream socket (TCP)
00004  *
00005  *  Created: Fri Nov 10 10:02:54 2006 (on train to Google, Hamburg)
00006  *  Copyright  2006  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #include <netcomm/socket/stream.h>
00025 
00026 #include <sys/types.h>
00027 #include <sys/socket.h>
00028 #include <netinet/in.h>
00029 #include <netinet/tcp.h>
00030 #include <errno.h>
00031 
00032 namespace fawkes {
00033 
00034 
00035 /** @class StreamSocket netcomm/socket/stream.h
00036  * TCP stream socket over IP.
00037  *
00038  * @ingroup NetComm
00039  * @author Tim Niemueller
00040  */
00041 
00042 /** Constructor.
00043  * @param timeout timeout, if 0 all operationsare blocking, otherwise it
00044  * is tried for timeout seconds.
00045  */
00046 StreamSocket::StreamSocket(float timeout)
00047   : Socket(PF_INET, SOCK_STREAM, 0, timeout)
00048 {
00049 }
00050 
00051 
00052 /** Copy constructor.
00053  * @param stream_socket socket to copy.
00054  */
00055 StreamSocket::StreamSocket(StreamSocket &stream_socket)
00056   : Socket(stream_socket)
00057 {
00058 }
00059 
00060 
00061 /** Clone socket.
00062  * @return a copied instance of StreamSocket.
00063  */
00064 Socket *
00065 StreamSocket::clone()
00066 {
00067   return new StreamSocket(*this);
00068 }
00069 
00070 
00071 /** Check if Nalge algorithm is disabled.
00072  * This checks the TCP_NODELAY option on the socket. If it is set then the
00073  * Nagle algorithm is disabled and all data is send out immediately.
00074  * @return true, if nodelay is enabled and thus the Nagle algorithm disabled,
00075  * false otherwise
00076  */
00077 bool
00078 StreamSocket::nodelay()
00079 {
00080   int val = 0;
00081  socklen_t val_len = sizeof(val);
00082   if ( getsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &val, &val_len) == -1 ) {
00083     throw SocketException("StreamSocket::nodelay: getsockopt failed", errno);
00084   }
00085   return (val == 1);
00086 }
00087 
00088 
00089 /** Enable or disable Nagle algorithm.
00090  * @param nodelay true to disable Nagle algorithm, false to enable it
00091  * @see nodelay()
00092  */
00093 void
00094 StreamSocket::set_nodelay(bool nodelay)
00095 {
00096   int val = (nodelay ? 1 : 0);
00097   socklen_t val_len = sizeof(val);
00098   if ( setsockopt(sock_fd, IPPROTO_TCP, TCP_NODELAY, &val, val_len) == -1 ) {
00099     throw SocketException("StreamSocket::set_nodelay: setsockopt failed", errno);
00100   }
00101 }
00102 
00103 } // end namespace fawkes
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends