reSIProcate/stack  9694
TcpConnection.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #include "rutil/Logger.hxx"
00006 #include "rutil/Socket.hxx"
00007 #include "resip/stack/TcpConnection.hxx"
00008 #include "resip/stack/Tuple.hxx"
00009 
00010 using namespace resip;
00011 
00012 #define RESIPROCATE_SUBSYSTEM Subsystem::TRANSPORT
00013 
00014 TcpConnection::TcpConnection(Transport* transport,const Tuple& who, Socket fd,
00015                              Compression &compression) 
00016   : Connection(transport,who, fd, compression)
00017 {
00018    DebugLog (<< "Creating TCP connection " << who << " on " << fd);
00019 }
00020 
00021 int 
00022 TcpConnection::read( char* buf, int count )
00023 {
00024    assert(buf);
00025    assert(count > 0);
00026    
00027 #if defined(WIN32)
00028    int bytesRead = ::recv(getSocket(), buf, count, 0);
00029 #else
00030    int bytesRead = ::read(getSocket(), buf, count);
00031 #endif
00032 
00033    if (bytesRead == SOCKET_ERROR)
00034    {
00035       int e = getErrno();
00036       switch (e)
00037       {
00038          case EAGAIN:
00039 #ifdef WIN32 //EWOULDBLOCK is not returned from recv on *nix/*bsd
00040          case EWOULDBLOCK:  
00041 #endif
00042             InfoLog (<< "No data ready to read");
00043             return 0;
00044          case EINTR:
00045             InfoLog (<< "The call was interrupted by a signal before any data was read.");
00046             return 0;            
00047             break;
00048          case EIO:
00049             InfoLog (<< "I/O error");
00050             break;
00051          case EBADF:
00052             InfoLog (<< "fd is not a valid file descriptor or is not open for reading.");
00053             break;
00054          case EINVAL:
00055             InfoLog (<< "fd is attached to an object which is unsuitable for reading.");
00056             break;
00057          case EFAULT:
00058             InfoLog (<< "buf is outside your accessible address space.");
00059             break;
00060          default:
00061             InfoLog (<< "Some other error");
00062             break;
00063       }
00064 
00065       InfoLog (<< "Failed read on " << getSocket() << " " << strerror(e));
00066       Transport::error(e);
00067       setFailureReason(TransportFailure::ConnectionException, e+2000);
00068       return -1;
00069    }
00070    else if (bytesRead == 0)
00071    {
00072       InfoLog (<< "Connection closed by remote " << *this);
00073       return -1;
00074    }
00075 
00076    return bytesRead;
00077 }
00078 
00079 
00080 int 
00081 TcpConnection::write( const char* buf, const int count )
00082 {
00083    //DebugLog (<< "Writing " << buf);   // Note:  this can end up writing garbage to the logs following the message for non-null terminated buffers
00084 
00085    assert(buf);
00086    assert(count > 0);
00087 
00088 #if defined(WIN32)
00089    int bytesWritten = ::send(getSocket(), buf, count, 0);
00090 #else
00091    int bytesWritten = ::write(getSocket(), buf, count);
00092 #endif
00093 
00094    if (bytesWritten == INVALID_SOCKET)
00095    {
00096       int e = getErrno();
00097       InfoLog (<< "Failed write on " << getSocket() << " " << strerror(e));
00098       Transport::error(e);
00099       //setFailureReason(TransportFailure::ConnectionException, e+1000);
00100       return -1;
00101    }
00102    
00103    return bytesWritten;
00104 }
00105 
00106 bool 
00107 TcpConnection::hasDataToRead()
00108 {
00109    return false;
00110 }
00111 
00112 bool 
00113 TcpConnection::isGood()
00114 {
00115    return true;
00116 }
00117 
00118 bool 
00119 TcpConnection::isWritable()
00120 {
00121    return true;
00122 }
00123 
00124 /* ====================================================================
00125  * The Vovida Software License, Version 1.0 
00126  * 
00127  * Copyright (c) 2000-2005 Vovida Networks, Inc.  All rights reserved.
00128  * 
00129  * Redistribution and use in source and binary forms, with or without
00130  * modification, are permitted provided that the following conditions
00131  * are met:
00132  * 
00133  * 1. Redistributions of source code must retain the above copyright
00134  *    notice, this list of conditions and the following disclaimer.
00135  * 
00136  * 2. Redistributions in binary form must reproduce the above copyright
00137  *    notice, this list of conditions and the following disclaimer in
00138  *    the documentation and/or other materials provided with the
00139  *    distribution.
00140  * 
00141  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00142  *    and "Vovida Open Communication Application Library (VOCAL)" must
00143  *    not be used to endorse or promote products derived from this
00144  *    software without prior written permission. For written
00145  *    permission, please contact vocal@vovida.org.
00146  *
00147  * 4. Products derived from this software may not be called "VOCAL", nor
00148  *    may "VOCAL" appear in their name, without prior written
00149  *    permission of Vovida Networks, Inc.
00150  * 
00151  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00152  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00153  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00154  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00155  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00156  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00157  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00158  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00159  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00160  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00161  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00162  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00163  * DAMAGE.
00164  * 
00165  * ====================================================================
00166  * 
00167  * This software consists of voluntary contributions made by Vovida
00168  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00169  * Inc.  For more information on Vovida Networks, Inc., please see
00170  * <http://www.vovida.org/>.
00171  *
00172  * vi: set shiftwidth=3 expandtab:
00173  */