reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
TcpConnection.cxx
Go to the documentation of this file.
1#if defined(HAVE_CONFIG_H)
2#include "config.h"
3#endif
4
5#include "rutil/Logger.hxx"
6#include "rutil/Socket.hxx"
9
10using namespace resip;
11
12#define RESIPROCATE_SUBSYSTEM Subsystem::TRANSPORT
13
14TcpConnection::TcpConnection(Transport* transport,const Tuple& who, Socket fd,
15 Compression &compression, bool isServer)
16 : Connection(transport,who, fd, compression, isServer)
17{
18 DebugLog (<< "Creating TCP connection " << who << " on " << fd);
19}
20
21int
22TcpConnection::read( char* buf, int count )
23{
24 resip_assert(buf);
25 resip_assert(count > 0);
26
27#if defined(WIN32)
28 int bytesRead = ::recv(getSocket(), buf, count, 0);
29#else
30 int bytesRead = ::read(getSocket(), buf, count);
31#endif
32
33 if (bytesRead == SOCKET_ERROR)
34 {
35 int e = getErrno();
36 switch (e)
37 {
38 case EAGAIN:
39#if EAGAIN != EWOULDBLOCK
40 case EWOULDBLOCK: // Treat EGAIN and EWOULDBLOCK as the same: http://stackoverflow.com/questions/7003234/which-systems-define-eagain-and-ewouldblock-as-different-values
41#endif
42 StackLog (<< "No data ready to read");
43 return 0;
44 case EINTR:
45 DebugLog (<< "The call was interrupted by a signal before any data was read.");
46 return 0;
47 break;
48 case EIO:
49 InfoLog (<< "I/O error");
50 break;
51 case EBADF:
52 InfoLog (<< "fd is not a valid file descriptor or is not open for reading.");
53 break;
54 case EINVAL:
55 InfoLog (<< "fd is attached to an object which is unsuitable for reading.");
56 break;
57 case EFAULT:
58 ErrLog (<< "buf is outside your accessible address space.");
59 break;
60 default:
61 ErrLog (<< "Some other error, code = " << e);
62 break;
63 }
64
65 Data failureString;
66 {
67 DataStream ds(failureString);
68 ds << "Failed read on " << getSocket() << ", errNum=" << e << ", err=" << Transport::errorToString(e);
69 }
70 InfoLog (<< failureString);
72 return -1;
73 }
74 else if (bytesRead == 0)
75 {
76 InfoLog (<< "Connection closed by remote " << *this);
77 return -1;
78 }
79
80 return bytesRead;
81}
82
83
84int
85TcpConnection::write( const char* buf, const int count )
86{
87 //DebugLog (<< "Writing " << buf); // Note: this can end up writing garbage to the logs following the message for non-null terminated buffers
88
89 resip_assert(buf);
90 resip_assert(count > 0);
91
92#if defined(WIN32)
93 int bytesWritten = ::send(getSocket(), buf, count, 0);
94#else
95 int bytesWritten = ::write(getSocket(), buf, count);
96#endif
97
98 if (bytesWritten == INVALID_SOCKET)
99 {
100 int e = getErrno();
101 //setFailureReason(TransportFailure::ConnectionException, e+1000);
102 if (e == EAGAIN || e == EWOULDBLOCK) // Treat EGAIN and EWOULDBLOCK as the same: http://stackoverflow.com/questions/7003234/which-systems-define-eagain-and-ewouldblock-as-different-values
103 {
104 // TCP buffers are backed up - we couldn't write anything - but we shouldn't treat this an error - return we wrote 0 bytes
105 return 0;
106 }
107 InfoLog (<< "Failed write on " << getSocket() << " " << strError(e));
109 return -1;
110 }
111
112 return bytesWritten;
113}
114
115bool
117{
118 return false;
119}
120
121bool
123{
124 return true;
125}
126
127bool
129{
130 return true;
131}
132
133/* ====================================================================
134 * The Vovida Software License, Version 1.0
135 *
136 * Copyright (c) 2000-2005 Vovida Networks, Inc. All rights reserved.
137 *
138 * Redistribution and use in source and binary forms, with or without
139 * modification, are permitted provided that the following conditions
140 * are met:
141 *
142 * 1. Redistributions of source code must retain the above copyright
143 * notice, this list of conditions and the following disclaimer.
144 *
145 * 2. Redistributions in binary form must reproduce the above copyright
146 * notice, this list of conditions and the following disclaimer in
147 * the documentation and/or other materials provided with the
148 * distribution.
149 *
150 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
151 * and "Vovida Open Communication Application Library (VOCAL)" must
152 * not be used to endorse or promote products derived from this
153 * software without prior written permission. For written
154 * permission, please contact vocal@vovida.org.
155 *
156 * 4. Products derived from this software may not be called "VOCAL", nor
157 * may "VOCAL" appear in their name, without prior written
158 * permission of Vovida Networks, Inc.
159 *
160 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
161 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
162 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
163 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
164 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
165 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
166 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
167 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
168 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
169 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
170 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
171 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
172 * DAMAGE.
173 *
174 * ====================================================================
175 *
176 * This software consists of voluntary contributions made by Vovida
177 * Networks, Inc. and many individuals on behalf of Vovida Networks,
178 * Inc. For more information on Vovida Networks, Inc., please see
179 * <http://www.vovida.org/>.
180 *
181 * vi: set shiftwidth=3 expandtab:
182 */
#define DebugLog(args_)
Definition Logger.hxx:128
#define StackLog(args_)
Definition Logger.hxx:125
#define InfoLog(args_)
Definition Logger.hxx:131
#define ErrLog(args_)
Definition Logger.hxx:137
#define resip_assert(x)
This file needs to work for both C and C++ code.
Handles cross-platform sockets compatibility.
#define INVALID_SOCKET
Definition Socket.hxx:95
#define SOCKET_ERROR
Definition Socket.hxx:96
void setFailureReason(TransportFailure::FailureReason failReason, int subCode, const Data &failureString)
Connection implements, via sockets, ConnectionBase for managed connections.
Socket getSocket() const
int read()
move data from the connection to the buffer; move this to front of least recently used list.
An iostream that operates on an existing Data.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
virtual bool hasDataToRead()
always true – always add to fdset as read ready
virtual bool isGood()
has valid connection
int write(const char *buf, const int count)
pure virtual, but need concrete Connection for book-ends of lists
virtual bool isWritable()
The base class for Transport classes.
Definition Transport.hxx:83
static Data errorToString(int e)
static void error(int e)
Generates a generic log for the platform specific socket error number.
Represents a network address.
Definition Tuple.hxx:59
dcm! – add UnusedChecking(_enum) below;
int Socket
Definition Socket.hxx:94
int getErrno()
Definition Socket.hxx:97
std::string strError(int err)
Definition compat.hxx:274