PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/service/lib/php/transport/TFramedTransport.php

#
PHP | 179 lines | 70 code | 24 blank | 85 comment | 9 complexity | 33eb9d9257e018bc321a941070a7bb42 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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.transport
  21. */
  22. /**
  23. * Framed transport. Writes and reads data in chunks that are stamped with
  24. * their length.
  25. *
  26. * @package thrift.transport
  27. */
  28. class TFramedTransport extends TTransport {
  29. /**
  30. * Underlying transport object.
  31. *
  32. * @var TTransport
  33. */
  34. private $transport_;
  35. /**
  36. * Buffer for read data.
  37. *
  38. * @var string
  39. */
  40. private $rBuf_;
  41. /**
  42. * Buffer for queued output data
  43. *
  44. * @var string
  45. */
  46. private $wBuf_;
  47. /**
  48. * Whether to frame reads
  49. *
  50. * @var bool
  51. */
  52. private $read_;
  53. /**
  54. * Whether to frame writes
  55. *
  56. * @var bool
  57. */
  58. private $write_;
  59. /**
  60. * Constructor.
  61. *
  62. * @param TTransport $transport Underlying transport
  63. */
  64. public function __construct($transport=null, $read=true, $write=true) {
  65. $this->transport_ = $transport;
  66. $this->read_ = $read;
  67. $this->write_ = $write;
  68. }
  69. public function isOpen() {
  70. return $this->transport_->isOpen();
  71. }
  72. public function open() {
  73. $this->transport_->open();
  74. }
  75. public function close() {
  76. $this->transport_->close();
  77. }
  78. /**
  79. * Reads from the buffer. When more data is required reads another entire
  80. * chunk and serves future reads out of that.
  81. *
  82. * @param int $len How much data
  83. */
  84. public function read($len) {
  85. if (!$this->read_) {
  86. return $this->transport_->read($len);
  87. }
  88. if (strlen($this->rBuf_) === 0) {
  89. $this->readFrame();
  90. }
  91. // Just return full buff
  92. if ($len >= strlen($this->rBuf_)) {
  93. $out = $this->rBuf_;
  94. $this->rBuf_ = null;
  95. return $out;
  96. }
  97. // Return substr
  98. $out = substr($this->rBuf_, 0, $len);
  99. $this->rBuf_ = substr($this->rBuf_, $len);
  100. return $out;
  101. }
  102. /**
  103. * Put previously read data back into the buffer
  104. *
  105. * @param string $data data to return
  106. */
  107. public function putBack($data) {
  108. if (strlen($this->rBuf_) === 0) {
  109. $this->rBuf_ = $data;
  110. } else {
  111. $this->rBuf_ = ($data . $this->rBuf_);
  112. }
  113. }
  114. /**
  115. * Reads a chunk of data into the internal read buffer.
  116. */
  117. private function readFrame() {
  118. $buf = $this->transport_->readAll(4);
  119. $val = unpack('N', $buf);
  120. $sz = $val[1];
  121. $this->rBuf_ = $this->transport_->readAll($sz);
  122. }
  123. /**
  124. * Writes some data to the pending output buffer.
  125. *
  126. * @param string $buf The data
  127. * @param int $len Limit of bytes to write
  128. */
  129. public function write($buf, $len=null) {
  130. if (!$this->write_) {
  131. return $this->transport_->write($buf, $len);
  132. }
  133. if ($len !== null && $len < strlen($buf)) {
  134. $buf = substr($buf, 0, $len);
  135. }
  136. $this->wBuf_ .= $buf;
  137. }
  138. /**
  139. * Writes the output buffer to the stream in the format of a 4-byte length
  140. * followed by the actual data.
  141. */
  142. public function flush() {
  143. if (!$this->write_) {
  144. return $this->transport_->flush();
  145. }
  146. $out = pack('N', strlen($this->wBuf_));
  147. $out .= $this->wBuf_;
  148. // Note that we clear the internal wBuf_ prior to the underlying write
  149. // to ensure we're in a sane state (i.e. internal buffer cleaned)
  150. // if the underlying write throws up an exception
  151. $this->wBuf_ = '';
  152. $this->transport_->write($out);
  153. $this->transport_->flush();
  154. }
  155. }