1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
18 using System.Threading;
19 using System.Threading.Tasks;
20 using Thrift.Protocols.Entities;
22 namespace Thrift.Protocols
25 * TMultiplexedProtocol is a protocol-independent concrete decorator that allows a Thrift
26 * client to communicate with a multiplexing Thrift server, by prepending the service name
27 * to the function name during function calls.
29 * NOTE: THIS IS NOT TO BE USED BY SERVERS.
30 * On the server, use TMultiplexedProcessor to handle requests from a multiplexing client.
32 * This example uses a single socket transport to invoke two services:
34 * TSocketClientTransport transport = new TSocketClientTransport("localhost", 9090);
37 * TBinaryProtocol protocol = new TBinaryProtocol(transport);
39 * TMultiplexedProtocol mp = new TMultiplexedProtocol(protocol, "Calculator");
40 * Calculator.Client service = new Calculator.Client(mp);
42 * TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "WeatherReport");
43 * WeatherReport.Client service2 = new WeatherReport.Client(mp2);
45 * System.out.println(service.add(2,2));
46 * System.out.println(service2.getTemperature());
50 //TODO: implementation of TProtocol
52 // ReSharper disable once InconsistentNaming
53 public class TMultiplexedProtocol : TProtocolDecorator
55 /** Used to delimit the service name from the function name */
56 public const string Separator = ":";
58 private readonly string _serviceName;
61 * Wrap the specified protocol, allowing it to be used to communicate with a
62 * multiplexing server. The <code>serviceName</code> is required as it is
63 * prepended to the message header so that the multiplexing server can broker
64 * the function call to the proper service.
67 * protocol Your communication protocol of choice, e.g. TBinaryProtocol
68 * serviceName The service name of the service communicating via this protocol.
71 public TMultiplexedProtocol(TProtocol protocol, string serviceName)
74 _serviceName = serviceName;
77 public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
81 case TMessageType.Call:
82 case TMessageType.Oneway:
83 await base.WriteMessageBeginAsync(new TMessage($"{_serviceName}{Separator}{message.Name}", message.Type, message.SeqID), cancellationToken);
86 await base.WriteMessageBeginAsync(message, cancellationToken);