private void CompleteStartConnection(bool async, HttpWebRequest httpWebRequest)
{
GlobalLog.Enter("Connection#" + ValidationHelper.HashString(this) + "::CompleteStartConnection", "async:" + async.ToString() + " httpWebRequest:" + ValidationHelper.HashString(httpWebRequest));
GlobalLog.ThreadContract(ThreadKinds.Unknown, "Connection#" + ValidationHelper.HashString(this) + "::CompleteStartConnection");
WebExceptionStatus ws = WebExceptionStatus.ConnectFailure;
m_InnerException = null;
bool success = true;
try {
#if DEBUG
lock (this)
{
// m_WriteList can be empty if request got aborted. In that case no new requests can come in so it should remain zero.
if (m_WriteList.Count != 0)
{
GlobalLog.Assert(m_WriteList.Count == 1, "Connection#{0}::CompleteStartConnection()|WriteList is not sized 1.", ValidationHelper.HashString(this));
GlobalLog.Assert((m_WriteList[0] as HttpWebRequest) == httpWebRequest, "Connection#{0}::CompleteStartConnection()|Last request on write list does not match.", ValidationHelper.HashString(this));
}
}
#endif
//
// we will create a tunnel through a proxy then create
// and connect the socket we will use for the connection
// otherwise we will just create a socket and use it
//
if (httpWebRequest.Address.Scheme == Uri.UriSchemeHttps && ServicePoint.InternalProxyServicePoint) {
if(!TunnelThroughProxy(ServicePoint.InternalAddress, httpWebRequest,async)) {
ws = WebExceptionStatus.ConnectFailure;
success = false;
}
if (async) {
return;
}
} else {
TimerThread.Timer timer = httpWebRequest.RequestTimer;
GlobalLog.Print("Connection#" + ValidationHelper.HashString(this) + "::CompleteStartConnection() Calling Activate(). TimeRemaining:" + (timer == null? "<null timer>" : timer.TimeRemaining.ToString()));
if (!Activate(httpWebRequest, async, ((timer!=null)?timer.TimeRemaining:0), new GeneralAsyncDelegate(CompleteConnectionWrapper)))
{
return;
}
}
}
catch (Exception exception) {
if (m_InnerException == null)
m_InnerException = exception;
if (exception is WebException) {
ws = ((WebException)exception).Status;
}
success = false;
}
if(!success)
{
ConnectionReturnResult returnResult = null;
HandleError(false, false, ws, ref returnResult);
ConnectionReturnResult.SetResponses(returnResult);
GlobalLog.Leave("Connection#" + ValidationHelper.HashString(this) + "::CompleteStartConnection Failed to connect.");
return;
}
// Getting here means we connected synchronously. Continue with the next step.
CompleteConnection(async, httpWebRequest);
GlobalLog.Leave("Connection#" + ValidationHelper.HashString(this) + "::CompleteStartConnection");
}