]>
git.saurik.com Git - apple/security.git/blob - Network/netconnection.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // connection - a (potentially) persistent access path to a (possibly :-) remote entity
22 #include "netconnection.h"
24 #include "netmanager.h"
33 // Create a Connection object for a particular Protocol and HostTarget.
34 // Note that these two arguments are potentially unrelated; in general,
35 // you can't assume that &proto == &host.protocol().
37 Connection::Connection(Protocol
&proto
, const HostTarget
&host
)
38 : protocol(proto
), hostTarget(host
), mTransfer(NULL
), mRetainMe(false), mRestarting(false)
41 "connection %p created for %s", this, hostTarget
.urlForm().c_str());
46 // Destroy a Connection, assuming it's idle.
48 Connection::~Connection()
51 debug("netconn", "connection %p destroyed", this);
56 // Dock the Connection to a Transfer.
58 void Connection::dock(Transfer
*xfer
)
61 assert(!xfer
->isDocked());
63 xfer
->mConnection
= this;
64 debug("netconn", "connection %p docked xfer %p", this, xfer
);
69 // Undock the Connection from its currently docked Transfer.
70 // The mRetainMe flag determines what happens next: we either
71 // submit ourselves to our Manager for retention, or for cleanup.
73 void Connection::undock()
77 assert(mTransfer
->mConnection
== this);
80 bool retain
= mRetainMe
&& mTransfer
->shareConnections();
82 // physically sever our relationship with the Transfer
83 debug("netconn", "connection %p undocking xfer %p", this, mTransfer
);
84 mTransfer
->mConnection
= NULL
;
87 // submit ourselves to the manager for retention
89 protocol
.manager
.retainConnection(this);
91 protocol
.manager
.closeConnection(this);
96 // Forwarders for finish/fail
98 void Connection::finish()
104 void Connection::fail()
107 // fail the transfer we're docked to, which will undock us and dispose of us
110 // we failed while in limbo. Self-dispose
112 protocol
.manager
.closeConnection(this);
118 // Drop the current Connection and re-execute start()
120 void Connection::restart()
123 Transfer
*transfer
= mTransfer
;
124 debug("netconn", "%p restarting xfer %p", this, transfer
);
126 // throw outselves out
130 // restart the transfer
133 // restart request on Connection that's not marked restarting.
134 // Presumably a real error, and we assume error indications have already
135 // been set (in the Transfer) by the caller as desired.
142 // The default implementation of validate() does nothing and succeeds.
144 bool Connection::validate()
151 // The file descriptor of a TCPConnection is itself (as a TCPClientSocket)
153 int TCPConnection::fileDesc() const
160 // The TCPConnection destructor will remove any remaining I/O hook
162 TCPConnection::~TCPConnection()
167 void TCPConnection::close()
170 protocol
.manager
.removeIO(this);
171 TCPClientSocket::close();
177 // Asynchronous connect processing for TCPClient subclasses.
178 // The full call sets up data and initiates the first connect attempt; the second
179 // form needs to be called on failure notification to (re)try other addresses.
181 void TCPConnection::connect(const Host
&host
, IPPort port
)
183 mAddressCandidates
= host
.addresses();
186 protocol
.manager
.addIO(this);
190 void TCPConnection::connect()
192 if (mAddressCandidates
.empty()) {
193 // out of candidates. This connection attempt is failing
194 UnixError::throwMe(EHOSTUNREACH
);
199 protocol
.manager
.addIO(this);
203 void TCPConnection::nextCandidate()
205 // pull the next address from the candidate set
206 std::set
<IPAddress
>::iterator it
= mAddressCandidates
.begin();
207 IPAddress addr
= *it
;
208 mAddressCandidates
.erase(it
);
210 open(addr
, mPort
, O_NONBLOCK
);
214 } // end namespace Network
215 } // end namespace Security