PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

#
PHP | 163 lines | 75 code | 21 blank | 67 comment | 15 complexity | 4345341b79670b84054af5c9ad7ead4a 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. * Buffered transport. Stores data to an internal buffer that it doesn't
  24. * actually write out until flush is called. For reading, we do a greedy
  25. * read and then serve data out of the internal buffer.
  26. *
  27. * @package thrift.transport
  28. */
  29. class TBufferedTransport extends TTransport {
  30. /**
  31. * Constructor. Creates a buffered transport around an underlying transport
  32. */
  33. public function __construct($transport=null, $rBufSize=512, $wBufSize=512) {
  34. $this->transport_ = $transport;
  35. $this->rBufSize_ = $rBufSize;
  36. $this->wBufSize_ = $wBufSize;
  37. }
  38. /**
  39. * The underlying transport
  40. *
  41. * @var TTransport
  42. */
  43. protected $transport_ = null;
  44. /**
  45. * The receive buffer size
  46. *
  47. * @var int
  48. */
  49. protected $rBufSize_ = 512;
  50. /**
  51. * The write buffer size
  52. *
  53. * @var int
  54. */
  55. protected $wBufSize_ = 512;
  56. /**
  57. * The write buffer.
  58. *
  59. * @var string
  60. */
  61. protected $wBuf_ = '';
  62. /**
  63. * The read buffer.
  64. *
  65. * @var string
  66. */
  67. protected $rBuf_ = '';
  68. public function isOpen() {
  69. return $this->transport_->isOpen();
  70. }
  71. public function open() {
  72. $this->transport_->open();
  73. }
  74. public function close() {
  75. $this->transport_->close();
  76. }
  77. public function putBack($data) {
  78. if (strlen($this->rBuf_) === 0) {
  79. $this->rBuf_ = $data;
  80. } else {
  81. $this->rBuf_ = ($data . $this->rBuf_);
  82. }
  83. }
  84. /**
  85. * The reason that we customize readAll here is that the majority of PHP
  86. * streams are already internally buffered by PHP. The socket stream, for
  87. * example, buffers internally and blocks if you call read with $len greater
  88. * than the amount of data available, unlike recv() in C.
  89. *
  90. * Therefore, use the readAll method of the wrapped transport inside
  91. * the buffered readAll.
  92. */
  93. public function readAll($len) {
  94. $have = strlen($this->rBuf_);
  95. if ($have == 0) {
  96. $data = $this->transport_->readAll($len);
  97. } else if ($have < $len) {
  98. $data = $this->rBuf_;
  99. $this->rBuf_ = '';
  100. $data .= $this->transport_->readAll($len - $have);
  101. } else if ($have == $len) {
  102. $data = $this->rBuf_;
  103. $this->rBuf_ = '';
  104. } else if ($have > $len) {
  105. $data = substr($this->rBuf_, 0, $len);
  106. $this->rBuf_ = substr($this->rBuf_, $len);
  107. }
  108. return $data;
  109. }
  110. public function read($len) {
  111. if (strlen($this->rBuf_) === 0) {
  112. $this->rBuf_ = $this->transport_->read($this->rBufSize_);
  113. }
  114. if (strlen($this->rBuf_) <= $len) {
  115. $ret = $this->rBuf_;
  116. $this->rBuf_ = '';
  117. return $ret;
  118. }
  119. $ret = substr($this->rBuf_, 0, $len);
  120. $this->rBuf_ = substr($this->rBuf_, $len);
  121. return $ret;
  122. }
  123. public function write($buf) {
  124. $this->wBuf_ .= $buf;
  125. if (strlen($this->wBuf_) >= $this->wBufSize_) {
  126. $out = $this->wBuf_;
  127. // Note that we clear the internal wBuf_ prior to the underlying write
  128. // to ensure we're in a sane state (i.e. internal buffer cleaned)
  129. // if the underlying write throws up an exception
  130. $this->wBuf_ = '';
  131. $this->transport_->write($out);
  132. }
  133. }
  134. public function flush() {
  135. if (strlen($this->wBuf_) > 0) {
  136. $this->transport_->write($this->wBuf_);
  137. $this->wBuf_ = '';
  138. }
  139. $this->transport_->flush();
  140. }
  141. }
  142. ?>