TTransport.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. namespace Thrift\Transport;
  23. use Thrift\Factory\TStringFuncFactory;
  24. /**
  25. * Base interface for a transport agent.
  26. *
  27. * @package thrift.transport
  28. */
  29. abstract class TTransport
  30. {
  31. /**
  32. * Whether this transport is open.
  33. *
  34. * @return boolean true if open
  35. */
  36. abstract public function isOpen();
  37. /**
  38. * Open the transport for reading/writing
  39. *
  40. * @throws TTransportException if cannot open
  41. */
  42. abstract public function open();
  43. /**
  44. * Close the transport.
  45. */
  46. abstract public function close();
  47. /**
  48. * Read some data into the array.
  49. *
  50. * @param int $len How much to read
  51. * @return string The data that has been read
  52. * @throws TTransportException if cannot read any more data
  53. */
  54. abstract public function read($len);
  55. /**
  56. * Guarantees that the full amount of data is read.
  57. *
  58. * @return string The data, of exact length
  59. * @throws TTransportException if cannot read data
  60. */
  61. public function readAll($len)
  62. {
  63. // return $this->read($len);
  64. $data = '';
  65. $got = 0;
  66. while (($got = TStringFuncFactory::create()->strlen($data)) < $len) {
  67. $data .= $this->read($len - $got);
  68. }
  69. return $data;
  70. }
  71. /**
  72. * Writes the given data out.
  73. *
  74. * @param string $buf The data to write
  75. * @throws TTransportException if writing fails
  76. */
  77. abstract public function write($buf);
  78. /**
  79. * Flushes any pending data out of a buffer
  80. *
  81. * @throws TTransportException if a writing error occurs
  82. */
  83. public function flush() {}
  84. }