1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: 15: 16: 17: 18: 19: 20:
21: class TFramedTransport extends TTransport {
22:
23: 24: 25: 26: 27:
28: private $transport_;
29:
30: 31: 32: 33: 34:
35: private $rBuf_;
36:
37: 38: 39: 40: 41:
42: private $wBuf_;
43:
44: 45: 46: 47: 48:
49: private $read_;
50:
51: 52: 53: 54: 55:
56: private $write_;
57:
58: 59: 60: 61: 62:
63: public function __construct($transport=null, $read=true, $write=true) {
64: $this->transport_ = $transport;
65: $this->read_ = $read;
66: $this->write_ = $write;
67: }
68:
69: public function isOpen() {
70: return $this->transport_->isOpen();
71: }
72:
73: public function open() {
74: $this->transport_->open();
75: }
76:
77: public function close() {
78: $this->transport_->close();
79: }
80:
81: 82: 83: 84: 85: 86:
87: public function read($len) {
88: if (!$this->read_) {
89: return $this->transport_->read($len);
90: }
91:
92: if (strlen($this->rBuf_) === 0) {
93: $this->readFrame();
94: }
95:
96:
97: if ($len >= strlen($this->rBuf_)) {
98: $out = $this->rBuf_;
99: $this->rBuf_ = null;
100: return $out;
101: }
102:
103:
104: $out = substr($this->rBuf_, 0, $len);
105: $this->rBuf_ = substr($this->rBuf_, $len);
106: return $out;
107: }
108:
109: 110: 111: 112: 113:
114: public function putBack($data) {
115: if (strlen($this->rBuf_) === 0) {
116: $this->rBuf_ = $data;
117: } else {
118: $this->rBuf_ = ($data . $this->rBuf_);
119: }
120: }
121:
122: 123: 124:
125: private function readFrame() {
126: $buf = $this->transport_->readAll(4);
127: $val = unpack('N', $buf);
128: $sz = $val[1];
129:
130: $this->rBuf_ = $this->transport_->readAll($sz);
131: }
132:
133: 134: 135: 136: 137: 138:
139: public function write($buf, $len=null) {
140: if (!$this->write_) {
141: return $this->transport_->write($buf, $len);
142: }
143:
144: if ($len !== null && $len < strlen($buf)) {
145: $buf = substr($buf, 0, $len);
146: }
147: $this->wBuf_ .= $buf;
148: }
149:
150: 151: 152: 153:
154: public function flush() {
155: if (!$this->write_) {
156: return $this->transport_->flush();
157: }
158:
159: $out = pack('N', strlen($this->wBuf_));
160: $out .= $this->wBuf_;
161:
162:
163:
164:
165: $this->wBuf_ = '';
166: $this->transport_->write($out);
167: $this->transport_->flush();
168: }
169:
170: }
171: