internal bool TryCreateRemoteLink(Attach attach, out AmqpLink link) { link = null; if (this.linkFactory == null) { return(false); } AmqpLinkSettings linkSettings = AmqpLinkSettings.Create(attach); Exception error = null; try { link = this.LinkFactory.CreateLink(this, linkSettings); } catch (Exception exception) when(!Fx.IsFatal(exception)) { AmqpException amqpException = exception as AmqpException; if (amqpException != null && amqpException.Error != null && (amqpException.Error.Condition.Equals(AmqpErrorCode.ResourceLimitExceeded) || amqpException.Error.Condition.Equals(AmqpErrorCode.ResourceLocked))) { // out of handle or link name exists throw; } AmqpTrace.Provider.AmqpLogError(this, "CreateLink", exception); // detach requires a handle so the error link has to be attached first link = new ErrorLink(this, linkSettings); error = exception; } link.RemoteHandle = attach.Handle; this.linksByRemoteHandle.Add(attach.Handle.Value, link); if (error != null) { link.SafeClose(error); return(false); } return(true); }
static void OnHeartBeatTimer(object state) { TimedHeartBeat thisPtr = (TimedHeartBeat)state; if (thisPtr.connection.IsClosing()) { return; } DateTime now = DateTime.UtcNow; try { if (thisPtr.localInterval < uint.MaxValue && now.Subtract(thisPtr.lastReceiveTime).TotalMilliseconds > thisPtr.localInterval) { string message = AmqpResources.GetString(AmqpResources.AmqpConnectionInactive, thisPtr.localInterval, thisPtr.connection.Settings.ContainerId); var amqpException = new AmqpException(AmqpErrorCode.ConnectionForced, message); AmqpTrace.Provider.AmqpLogError(thisPtr.connection, "OnHeartBeatTimer", amqpException); thisPtr.connection.SafeClose(amqpException); return; } if (thisPtr.remoteInterval < uint.MaxValue && now.Subtract(thisPtr.lastSendTime).TotalMilliseconds >= thisPtr.remoteInterval) { thisPtr.connection.SendCommand(null, 0, null); } thisPtr.heartBeatTimer.Change(thisPtr.GetTimerInterval(now), Timeout.InfiniteTimeSpan); } catch (Exception exception) when(!Fx.IsFatal(exception)) { if (!thisPtr.connection.IsClosing()) { AmqpTrace.Provider.AmqpLogError(thisPtr.connection, "OnHeartBeatTimer", exception); thisPtr.connection.SafeClose(exception); } } }
static Exception ConvertToException(int statusCode, string statusDescription) { Exception exception; if (Enum.IsDefined(typeof(AmqpResponseStatusCode), statusCode)) { AmqpResponseStatusCode amqpResponseStatusCode = (AmqpResponseStatusCode)statusCode; switch (amqpResponseStatusCode) { case AmqpResponseStatusCode.BadRequest: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.NotFound: exception = new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Forbidden: exception = new AmqpException(AmqpErrorCode.TransferLimitExceeded, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Unauthorized: exception = new AmqpException(AmqpErrorCode.UnauthorizedAccess, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; default: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; } } else { exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); } return(exception); }
protected override void OnProtocolHeader(ProtocolHeader header) { #if DEBUG header.Trace(false); AmqpTrace.Provider.AmqpLogOperationVerbose(this, TraceOperation.Receive, header); #endif this.heartBeat.OnReceive(); if (this.UsageMeter != null) { this.UsageMeter.OnRead(this, 0, header.EncodeSize); } this.TransitState("R:HDR", StateTransition.ReceiveHeader); Exception exception = null; if (this.isInitiator) { if (!this.initialHeader.Equals(header)) { exception = new AmqpException(AmqpErrorCode.NotImplemented, AmqpResources.GetString(AmqpResources.AmqpProtocolVersionNotSupported, this.initialHeader.ToString(), header.ToString())); } } else { ProtocolHeader supportedHeader = this.amqpSettings.GetSupportedHeader(header); this.SendProtocolHeader(supportedHeader); if (!supportedHeader.Equals(header)) { exception = new AmqpException(AmqpErrorCode.NotImplemented, AmqpResources.GetString(AmqpResources.AmqpProtocolVersionNotSupported, this.initialHeader.ToString(), header.ToString())); } } if (exception != null) { this.CompleteOpen(false, exception); } }
/// <summary> /// Sends a security token for a resource for later access. /// </summary> /// <param name="tokenProvider">The provider for issuing security tokens.</param> /// <param name="namespaceAddress">The namespace (or tenant) name.</param> /// <param name="audience">The audience. In most cases it is the same as resource.</param> /// <param name="resource">The resource to access.</param> /// <param name="requiredClaims">The required claims to access the resource.</param> /// <param name="timeout">The operation timeout.</param> /// <returns></returns> public async Task <DateTime> SendTokenAsync(ICbsTokenProvider tokenProvider, Uri namespaceAddress, string audience, string resource, string[] requiredClaims, TimeSpan timeout) { if (this.connection.IsClosing()) { throw new OperationCanceledException("Connection is closing or closed."); } CbsToken token = await tokenProvider.GetTokenAsync(namespaceAddress, resource, requiredClaims).ConfigureAwait(false); string tokenType = token.TokenType; if (tokenType == null) { throw new NotSupportedException(AmqpResources.AmqpUnsupportedTokenType); } RequestResponseAmqpLink requestResponseLink; if (!this.linkFactory.TryGetOpenedObject(out requestResponseLink)) { requestResponseLink = await this.linkFactory.GetOrCreateAsync(timeout).ConfigureAwait(false); } AmqpValue value = new AmqpValue(); value.Value = token.TokenValue; AmqpMessage putTokenRequest = AmqpMessage.Create(value); putTokenRequest.ApplicationProperties.Map[CbsConstants.Operation] = CbsConstants.PutToken.OperationValue; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Type] = tokenType; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Audience] = audience; putTokenRequest.ApplicationProperties.Map[CbsConstants.PutToken.Expiration] = token.ExpiresAtUtc; AmqpMessage putTokenResponse = await requestResponseLink.RequestAsync(putTokenRequest, timeout).ConfigureAwait(false); int statusCode = (int)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusCode]; string statusDescription = (string)putTokenResponse.ApplicationProperties.Map[CbsConstants.PutToken.StatusDescription]; if (statusCode == (int)AmqpResponseStatusCode.Accepted || statusCode == (int)AmqpResponseStatusCode.OK) { return(token.ExpiresAtUtc); } Exception exception; AmqpResponseStatusCode amqpResponseStatusCode = (AmqpResponseStatusCode)statusCode; switch (amqpResponseStatusCode) { case AmqpResponseStatusCode.BadRequest: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.NotFound: exception = new AmqpException(AmqpErrorCode.NotFound, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Forbidden: exception = new AmqpException(AmqpErrorCode.TransferLimitExceeded, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; case AmqpResponseStatusCode.Unauthorized: exception = new AmqpException(AmqpErrorCode.UnauthorizedAccess, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; default: exception = new AmqpException(AmqpErrorCode.InvalidField, AmqpResources.GetString(AmqpResources.AmqpPutTokenFailed, statusCode, statusDescription)); break; } throw exception; }
void OnReceiveBegin(Begin begin) { StateTransition stateTransition = this.TransitState("R:BEGIN", StateTransition.ReceiveOpen); this.incomingChannel.OnBegin(begin); if (stateTransition.To == AmqpObjectState.OpenReceived) { this.outgoingChannel.OnBegin(begin); this.UpdateHandleTable(begin); this.Open(); } else { Exception exception = null; Error error = this.Negotiate(begin); if (error != null) { exception = new AmqpException(error); } this.CompleteOpen(false, exception); if (exception != null) { this.SafeClose(exception); } } }
bool HandleReadComplete(TransportAsyncCallbackArgs args) { bool completed = true; Exception exception = null; if (args.Exception != null) { exception = args.Exception; } else if (args.BytesTransfered == 0) { exception = new ObjectDisposedException(this.transport.ToString()); } else if (args.BytesTransfered == args.Count) { args.ByteBuffer?.Append(args.BytesTransfered); } else { completed = false; if (args.ByteBuffer == null) { args.SetBuffer(args.Buffer, args.Offset + args.BytesTransfered, args.Count - args.BytesTransfered); } else { args.ByteBuffer.Append(args.BytesTransfered); args.SetReadBuffer(args.ByteBuffer); } } if (completed) { if (exception != null || object.ReferenceEquals(args.CompletedCallback, onFrameComplete)) { ByteBuffer buffer = args.ByteBuffer; try { if (exception == null) { this.parent.OnReceiveBuffer(buffer); } else { this.parent.OnIoFault(exception); } } finally { buffer?.Dispose(); } } else { // read size completed ok uint size = AmqpBitConverter.ReadUInt(this.sizeBuffer, 0, this.sizeBuffer.Length); if (size > this.maxFrameSize) { completed = true; exception = new AmqpException(AmqpErrorCode.FramingError, CommonResources.GetString(CommonResources.InvalidFrameSize, size, this.maxFrameSize)); this.parent.OnIoFault(exception); } else { ByteBuffer buffer = this.parent.CreateBuffer((int)size); args.SetReadBuffer(buffer); args.CompletedCallback = onFrameComplete; completed = false; } } } return(completed); }
bool HandleReadComplete(TransportAsyncCallbackArgs args) { bool completed = true; Exception exception = null; if (args.Exception != null) { exception = args.Exception; } else if (args.BytesTransfered == 0) { exception = new ObjectDisposedException(this.transport.ToString()); } else if (args.BytesTransfered < args.Count) { args.SetBuffer(args.Buffer, args.Offset + args.BytesTransfered, args.Count - args.BytesTransfered); completed = false; } if (completed) { if (exception != null || object.ReferenceEquals(args.CompletedCallback, onFrameComplete)) { ByteBuffer buffer = null; if (exception == null) { buffer = new ByteBuffer(args.Buffer, 0, args.Buffer.Length); this.parent.OnReceiveBuffer(buffer); } else { this.parent.OnIoFault(exception); } } else { // read size completed ok uint size = AmqpBitConverter.ReadUInt(this.sizeBuffer, 0, this.sizeBuffer.Length); if (size > this.maxFrameSize) { completed = true; exception = new AmqpException(AmqpErrorCode.FramingError, CommonResources.GetString(CommonResources.InvalidFrameSize, size, this.maxFrameSize)); this.parent.OnIoFault(exception); } else { byte[] frameBuffer = new byte[size]; Buffer.BlockCopy(this.sizeBuffer, 0, frameBuffer, 0, this.sizeBuffer.Length); args.SetBuffer(frameBuffer, this.sizeBuffer.Length, (int)size - this.sizeBuffer.Length); args.CompletedCallback = onFrameComplete; completed = false; } } } return completed; }