1: <?php
2:
3: /**
4: * Copyright (c) 2006- Facebook
5: * Distributed under the Thrift Software License
6: *
7: * See accompanying file LICENSE or visit the Thrift site at:
8: * http://developers.facebook.com/thrift/
9: *
10: * @package thrift.transport
11: * @author Mark Slee <mcslee@facebook.com>
12: */
13:
14: /**
15: * Transport exceptions
16: */
17: class TTransportException extends TException {
18:
19: const UNKNOWN = 0;
20: const NOT_OPEN = 1;
21: const ALREADY_OPEN = 2;
22: const TIMED_OUT = 3;
23: const END_OF_FILE = 4;
24:
25: function __construct($message=null, $code=0) {
26: parent::__construct($message, $code);
27: }
28: }
29:
30: /**
31: * Base interface for a transport agent.
32: *
33: * @package thrift.transport
34: * @author Mark Slee <mcslee@facebook.com>
35: */
36: abstract class TTransport {
37:
38: /**
39: * Whether this transport is open.
40: *
41: * @return boolean true if open
42: */
43: public abstract function isOpen();
44:
45: /**
46: * Open the transport for reading/writing
47: *
48: * @throws TTransportException if cannot open
49: */
50: public abstract function open();
51:
52: /**
53: * Close the transport.
54: */
55: public abstract function close();
56:
57: /**
58: * Read some data into the array.
59: *
60: * @param int $len How much to read
61: * @return string The data that has been read
62: * @throws TTransportException if cannot read any more data
63: */
64: public abstract function read($len);
65:
66: /**
67: * Guarantees that the full amount of data is read.
68: *
69: * @return string The data, of exact length
70: * @throws TTransportException if cannot read data
71: */
72: public function readAll($len) {
73: // return $this->read($len);
74:
75: $data = '';
76: $got = 0;
77: while (($got = strlen($data)) < $len) {
78: $data .= $this->read($len - $got);
79: }
80: return $data;
81: }
82:
83: /**
84: * Writes the given data out.
85: *
86: * @param string $buf The data to write
87: * @throws TTransportException if writing fails
88: */
89: public abstract function write($buf);
90:
91: /**
92: * Flushes any pending data out of a buffer
93: *
94: * @throws TTransportException if a writing error occurs
95: */
96: public function flush() {}
97: }
98:
99: ?>
100: