public IAsyncResult BeginSendRequest(IServiceRequest request, int timeout, AsyncCallback callback, object state)
{
if (request == null) throw new ArgumentNullException("request");
if (timeout <= 0) throw new ArgumentException("Timeout must be greater than zero.", "timeout");
lock (DataLock)
{
bool firstCall = false;
WriteOperation operation = null;
// check if this is the first call.
if (State == TcpChannelState.Closed)
{
if (m_queuedOperations == null)
{
firstCall = true;
m_queuedOperations = new List<QueuedOperation>();
}
}
// queue operations until connect completes.
if (m_queuedOperations != null)
{
operation = BeginOperation(timeout, callback, state);
m_queuedOperations.Add(new QueuedOperation(operation, timeout, request));
if (firstCall)
{
BeginConnect(m_url, timeout, OnConnectOnDemandComplete, null);
}
return operation;
}
if (State != TcpChannelState.Open)
{
throw new ServiceResultException(StatusCodes.BadConnectionClosed);
}
//Utils.Trace("Channel {0}: BeginSendRequest()", ChannelId);
if (m_reconnecting)
{
throw ServiceResultException.Create(StatusCodes.BadRequestInterrupted, "Attempting to reconnect to the server.");
}
// send request.
operation = BeginOperation(timeout, callback, state);
SendRequest(operation, timeout, request);
return operation;
}
}