HproseClient.php
5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**********************************************************\
| |
| hprose |
| |
| Official WebSite: http://www.hprose.com/ |
| http://www.hprose.net/ |
| http://www.hprose.org/ |
| |
\**********************************************************/
/**********************************************************\
* *
* HproseClient.php *
* *
* hprose client library for php5. *
* *
* LastModified: Nov 13, 2013 *
* Author: Ma Bingyao <andot@hprfc.com> *
* *
\**********************************************************/
require_once('HproseCommon.php');
require_once('HproseIO.php');
abstract class HproseClient {
protected $url;
private $filter;
private $simple;
protected abstract function send($request);
public function __construct($url = '') {
$this->useService($url);
$this->filter = NULL;
$this->simple = false;
}
public function useService($url = '', $namespace = '') {
if ($url) {
$this->url = $url;
}
return new HproseProxy($this, $namespace);
}
public function invoke($functionName, &$arguments = array(), $byRef = false, $resultMode = HproseResultMode::Normal, $simple = NULL) {
if ($simple === NULL) $simple = $this->simple;
$stream = new HproseStringStream(HproseTags::TagCall);
$hproseWriter = ($simple ? new HproseSimpleWriter($stream) : new HproseWriter($stream));
$hproseWriter->writeString($functionName);
if (count($arguments) > 0 || $byRef) {
$hproseWriter->reset();
$hproseWriter->writeList($arguments);
if ($byRef) {
$hproseWriter->writeBoolean(true);
}
}
$stream->write(HproseTags::TagEnd);
$request = $stream->toString();
if ($this->filter) $request = $this->filter->outputFilter($request);
$stream->close();
$response = $this->send($request);
if ($this->filter) $response = $this->filter->inputFilter($response);
if ($resultMode == HproseResultMode::RawWithEndTag) {
return $response;
}
if ($resultMode == HproseResultMode::Raw) {
return substr($response, 0, -1);
}
$stream = new HproseStringStream($response);
$hproseReader = new HproseReader($stream);
$result = NULL;
while (($tag = $hproseReader->checkTags(
array(HproseTags::TagResult,
HproseTags::TagArgument,
HproseTags::TagError,
HproseTags::TagEnd))) !== HproseTags::TagEnd) {
switch ($tag) {
case HproseTags::TagResult:
if ($resultMode == HproseResultMode::Serialized) {
$result = $hproseReader->readRaw()->toString();
}
else {
$hproseReader->reset();
$result = &$hproseReader->unserialize();
}
break;
case HproseTags::TagArgument:
$hproseReader->reset();
$args = &$hproseReader->readList(true);
for ($i = 0; $i < count($arguments); $i++) {
$arguments[$i] = &$args[$i];
}
break;
case HproseTags::TagError:
$hproseReader->reset();
throw new HproseException($hproseReader->readString(true));
break;
}
}
return $result;
}
public function getFilter() {
return $this->filter;
}
public function setFilter($filter) {
$this->filter = $filter;
}
public function getSimpleMode() {
return $this->simple;
}
public function setSimpleMode($simple = true) {
$this->simple = $simple;
}
public function __call($function, $arguments) {
return $this->invoke($function, $arguments);
}
public function __get($name) {
return new HproseProxy($this, $name . '_');
}
}
class HproseProxy {
private $client;
private $namespace;
public function __construct($client, $namespace = '') {
$this->client = $client;
$this->namespace = $namespace;
}
public function __call($function, $arguments) {
$function = $this->namespace . $function;
return $this->client->invoke($function, $arguments);
}
public function __get($name) {
return new HproseProxy($this->client, $this->namespace . $name . '_');
}
}
?>