TMultiplexedProtocol.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. * @package thrift.protocol
  21. */
  22. namespace Thrift\Protocol;
  23. use Thrift\Type\TMessageType;
  24. /**
  25. * <code>TMultiplexedProtocol</code> is a protocol-independent concrete decorator
  26. * that allows a Thrift client to communicate with a multiplexing Thrift server,
  27. * by prepending the service name to the function name during function calls.
  28. *
  29. * @package Thrift\Protocol
  30. */
  31. class TMultiplexedProtocol extends TProtocolDecorator
  32. {
  33. /**
  34. * Separator between service name and function name.
  35. * Should be the same as used at multiplexed Thrift server.
  36. *
  37. * @var string
  38. */
  39. const SEPARATOR = ":";
  40. /**
  41. * The name of service.
  42. *
  43. * @var string
  44. */
  45. private $serviceName_;
  46. /**
  47. * Constructor of <code>TMultiplexedProtocol</code> class.
  48. *
  49. * Wrap the specified protocol, allowing it to be used to communicate with a
  50. * multiplexing server. The <code>$serviceName</code> is required as it is
  51. * prepended to the message header so that the multiplexing server can broker
  52. * the function call to the proper service.
  53. *
  54. * @param TProtocol $protocol
  55. * @param string $serviceName The name of service.
  56. */
  57. public function __construct(TProtocol $protocol, $serviceName)
  58. {
  59. parent::__construct($protocol);
  60. $this->serviceName_ = $serviceName;
  61. }
  62. /**
  63. * Writes the message header.
  64. * Prepends the service name to the function name, separated by <code>TMultiplexedProtocol::SEPARATOR</code>.
  65. *
  66. * @param string $name Function name.
  67. * @param int $type Message type.
  68. * @param int $seqid The sequence id of this message.
  69. */
  70. public function writeMessageBegin($name, $type, $seqid)
  71. {
  72. if ($type == TMessageType::CALL || $type == TMessageType::ONEWAY) {
  73. $nameWithService = $this->serviceName_ . self::SEPARATOR . $name;
  74. parent::writeMessageBegin($nameWithService, $type, $seqid);
  75. } else {
  76. parent::writeMessageBegin($name, $type, $seqid);
  77. }
  78. }
  79. }