public async Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return await Task.FromCanceled<bool>(cancellationToken); } try { var message = await iprot.ReadMessageBeginAsync(cancellationToken); if ((message.Type != TMessageType.Call) && (message.Type != TMessageType.Oneway)) { await FailAsync(oprot, message, TApplicationException.ExceptionType.InvalidMessageType, "Message exType CALL or ONEWAY expected", cancellationToken); return false; } // Extract the service name var index = message.Name.IndexOf(TMultiplexedProtocol.Separator, StringComparison.Ordinal); if (index < 0) { await FailAsync(oprot, message, TApplicationException.ExceptionType.InvalidProtocol, $"Service name not found in message name: {message.Name}. Did you forget to use a TMultiplexProtocol in your client?", cancellationToken); return false; } // Create a new TMessage, something that can be consumed by any TProtocol var serviceName = message.Name.Substring(0, index); ITAsyncProcessor actualProcessor; if (!_serviceProcessorMap.TryGetValue(serviceName, out actualProcessor)) { await FailAsync(oprot, message, TApplicationException.ExceptionType.InternalError, $"Service name not found: {serviceName}. Did you forget to call RegisterProcessor()?", cancellationToken); return false; } // Create a new TMessage, removing the service name var newMessage = new TMessage( message.Name.Substring(serviceName.Length + TMultiplexedProtocol.Separator.Length), message.Type, message.SeqID); // Dispatch processing to the stored processor return await actualProcessor.ProcessAsync(new StoredMessageProtocol(iprot, newMessage), oprot, cancellationToken); } catch (IOException) { return false; // similar to all other processors } }
/** * Prepends the service name to the function name, separated by TMultiplexedProtocol.SEPARATOR. * Args: * tMessage The original message. */ public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) { switch (message.Type) { case TMessageType.Call: case TMessageType.Oneway: await base.WriteMessageBeginAsync( new TMessage($"{_serviceName}{Separator}{message.Name}", message.Type, message.SeqID), cancellationToken); break; default: await base.WriteMessageBeginAsync(message, cancellationToken); break; } }
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } await Trans.WriteAsync(new[] {ProtocolId}, cancellationToken); await Trans.WriteAsync( new[] {(byte) ((Version & VersionMask) | (((uint) message.Type << TypeShiftAmount) & TypeMask))}, cancellationToken); var bufferTuple = CreateWriteVarInt32((uint) message.SeqID); await Trans.WriteAsync(bufferTuple.Item1, 0, bufferTuple.Item2, cancellationToken); await WriteStringAsync(message.Name, cancellationToken); }
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return; } if (StrictWrite) { var version = Version1 | (uint) message.Type; await WriteI32Async((int) version, cancellationToken); await WriteStringAsync(message.Name, cancellationToken); await WriteI32Async(message.SeqID, cancellationToken); } else { await WriteStringAsync(message.Name, cancellationToken); await WriteByteAsync((sbyte) message.Type, cancellationToken); await WriteI32Async(message.SeqID, cancellationToken); } }
public override async Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) { var message = new TMessage(); await ReadJsonArrayStartAsync(cancellationToken); if (await ReadJsonIntegerAsync(cancellationToken) != Version) { throw new TProtocolException(TProtocolException.BAD_VERSION, "Message contained bad version."); } var buf = await ReadJsonStringAsync(false, cancellationToken); message.Name = Utf8Encoding.GetString(buf, 0, buf.Length); message.Type = (TMessageType) await ReadJsonIntegerAsync(cancellationToken); message.SeqID = (int) await ReadJsonIntegerAsync(cancellationToken); return message; }
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) { await WriteJsonArrayStartAsync(cancellationToken); await WriteJsonIntegerAsync(Version, cancellationToken); var b = Utf8Encoding.GetBytes(message.Name); await WriteJsonStringAsync(b, cancellationToken); await WriteJsonIntegerAsync((long) message.Type, cancellationToken); await WriteJsonIntegerAsync(message.SeqID, cancellationToken); }
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken) { await _wrappedProtocol.WriteMessageBeginAsync(message, cancellationToken); }
public StoredMessageProtocol(TProtocol protocol, TMessage messageBegin) : base(protocol) { _msgBegin = messageBegin; }
private async Task FailAsync(TProtocol oprot, TMessage message, TApplicationException.ExceptionType extype, string etxt, CancellationToken cancellationToken) { var appex = new TApplicationException(extype, etxt); var newMessage = new TMessage(message.Name, TMessageType.Exception, message.SeqID); await oprot.WriteMessageBeginAsync(newMessage, cancellationToken); await appex.WriteAsync(oprot, cancellationToken); await oprot.WriteMessageEndAsync(cancellationToken); await oprot.Transport.FlushAsync(cancellationToken); }
public override async Task<TMessage> ReadMessageBeginAsync(CancellationToken cancellationToken) { if (cancellationToken.IsCancellationRequested) { return await Task.FromCanceled<TMessage>(cancellationToken); } var message = new TMessage(); var size = await ReadI32Async(cancellationToken); if (size < 0) { var version = (uint) size & VersionMask; if (version != Version1) { throw new TProtocolException(TProtocolException.BAD_VERSION, $"Bad version in ReadMessageBegin: {version}"); } message.Type = (TMessageType) (size & 0x000000ff); message.Name = await ReadStringAsync(cancellationToken); message.SeqID = await ReadI32Async(cancellationToken); } else { if (StrictRead) { throw new TProtocolException(TProtocolException.BAD_VERSION, "Missing version in ReadMessageBegin, old client?"); } message.Name = await ReadStringBodyAsync(size, cancellationToken); message.Type = (TMessageType) await ReadByteAsync(cancellationToken); message.SeqID = await ReadI32Async(cancellationToken); } return message; }