/[resiprocate]/main/sip/resiprocate/Connection.cxx
ViewVC logotype

Contents of /main/sip/resiprocate/Connection.cxx

Parent Directory Parent Directory | Revision Log Revision Log


Revision 4599 - (show annotations) (download)
Wed May 11 23:10:19 2005 UTC (14 years, 7 months ago) by derek
File size: 5515 byte(s)
set svn:eol-style to LF
1 #if defined(HAVE_CONFIG_H)
2 #include "resiprocate/config.hxx"
3 #endif
4
5 #include "resiprocate/os/Socket.hxx"
6 #include "resiprocate/os/Logger.hxx"
7 #include "resiprocate/Connection.hxx"
8 #include "resiprocate/ConnectionManager.hxx"
9 #include "resiprocate/SipMessage.hxx"
10 #include "resiprocate/Security.hxx"
11 #include "resiprocate/TcpBaseTransport.hxx"
12 #include "resiprocate/os/WinLeakCheck.hxx"
13
14 using namespace resip;
15
16 #define RESIPROCATE_SUBSYSTEM Subsystem::TRANSPORT
17
18 Connection::Connection()
19 : mSocket(INVALID_SOCKET)
20 {
21 }
22
23 Connection::Connection(const Tuple& who, Socket socket)
24 : ConnectionBase(who),
25 mSocket(socket)
26 {
27 getConnectionManager().addConnection(this);
28 }
29
30 Connection::~Connection()
31 {
32 if (mSocket != INVALID_SOCKET) // bogus Connections
33 {
34 closeSocket(mSocket);
35 getConnectionManager().removeConnection(this);
36 }
37 }
38
39 ConnectionId
40 Connection::getId() const
41 {
42 return mWho.connectionId;
43 }
44
45
46 void
47 Connection::requestWrite(SendData* sendData)
48 {
49 assert(mWho.transport);
50 if (mOutstandingSends.empty())
51 {
52 getConnectionManager().addToWritable(this);
53 }
54 mOutstandingSends.push_back(sendData);
55 }
56
57 void
58 Connection::performWrite()
59 {
60 //assert(hasDataToWrite());
61
62 assert(!mOutstandingSends.empty());
63 const Data& data = mOutstandingSends.front()->data;
64 DebugLog (<< "Sending " << data.size() - mSendPos << " bytes");
65 int nBytes = write(data.data() + mSendPos,data.size() - mSendPos);
66
67 if (nBytes < 0)
68 {
69 //fail(data.transactionId);
70 InfoLog(<< "Write failed on socket: " << this->getSocket() << ", closing connection");
71 delete this;
72 }
73 else
74 {
75 // Safe because of the conditional above ( < 0 ).
76 Data::size_type bytesWritten = static_cast<Data::size_type>(nBytes);
77 mSendPos += bytesWritten;
78 if (mSendPos == data.size())
79 {
80 mSendPos = 0;
81 delete mOutstandingSends.front();
82 mOutstandingSends.pop_front();
83
84 if (mOutstandingSends.empty())
85 {
86 getConnectionManager().removeFromWritable();
87 }
88 }
89 }
90 }
91
92 ConnectionManager&
93 Connection::getConnectionManager() const
94 {
95 assert(mWho.transport);
96 TcpBaseTransport* transport = static_cast<TcpBaseTransport*>(mWho.transport);
97
98 return transport->getConnectionManager();
99 }
100
101 std::ostream&
102 resip::operator<<(std::ostream& strm, const resip::Connection& c)
103 {
104 strm << "CONN: " << &c << " " << int(c.getSocket()) << " " << c.mWho;
105 return strm;
106 }
107
108 Transport*
109 Connection::transport()
110 {
111 assert(this);
112 return mWho.transport;
113 }
114
115 int
116 Connection::read(Fifo<TransactionMessage>& fifo)
117 {
118 std::pair<char*, size_t> writePair = getWriteBuffer();
119 size_t bytesToRead = resipMin(writePair.second,
120 static_cast<size_t>(Connection::ChunkSize));
121
122 assert(bytesToRead > 0);
123 int bytesRead = read(writePair.first, bytesToRead);
124 if (bytesRead <= 0)
125 {
126 return bytesRead;
127 }
128 preparseNewBytes(bytesRead, fifo);
129 getConnectionManager().touch(this);
130 return bytesRead;
131 }
132
133 bool
134 Connection::hasDataToRead()
135 {
136 return true;
137 }
138
139 bool
140 Connection::isGood()
141 {
142 return true;
143 }
144
145
146 /* ====================================================================
147 * The Vovida Software License, Version 1.0
148 *
149 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
150 *
151 * Redistribution and use in source and binary forms, with or without
152 * modification, are permitted provided that the following conditions
153 * are met:
154 *
155 * 1. Redistributions of source code must retain the above copyright
156 * notice, this list of conditions and the following disclaimer.
157 *
158 * 2. Redistributions in binary form must reproduce the above copyright
159 * notice, this list of conditions and the following disclaimer in
160 * the documentation and/or other materials provided with the
161 * distribution.
162 *
163 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
164 * and "Vovida Open Communication Application Library (VOCAL)" must
165 * not be used to endorse or promote products derived from this
166 * software without prior written permission. For written
167 * permission, please contact vocal@vovida.org.
168 *
169 * 4. Products derived from this software may not be called "VOCAL", nor
170 * may "VOCAL" appear in their name, without prior written
171 * permission of Vovida Networks, Inc.
172 *
173 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
174 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
175 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
176 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
177 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
178 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
179 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
180 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
181 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
182 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
183 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
184 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
185 * DAMAGE.
186 *
187 * ====================================================================
188 *
189 * This software consists of voluntary contributions made by Vovida
190 * Networks, Inc. and many individuals on behalf of Vovida Networks,
191 * Inc. For more information on Vovida Networks, Inc., please see
192 * <http://www.vovida.org/>.
193 *
194 */
195

Properties

Name Value
svn:eol-style LF

webmaster AT resiprocate DOT org
ViewVC Help
Powered by ViewVC 1.1.27