Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
wxh_internal_api
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
杨培培
wxh_internal_api
Commits
440e7462
Commit
440e7462
authored
Dec 31, 2019
by
杨培
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持 hyeprf jsonrpc-tcp-length-check 协议。
parent
f6f39318
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
6 deletions
+46
-6
Client.php
src/base/Client.php
+3
-2
JsonRpc.php
src/base/JsonRpc.php
+43
-4
No files found.
src/base/Client.php
View file @
440e7462
...
@@ -210,9 +210,10 @@ class Client
...
@@ -210,9 +210,10 @@ class Client
if
(
!
isset
(
$connections
[
$instance
->
rpcServer
]))
{
if
(
!
isset
(
$connections
[
$instance
->
rpcServer
]))
{
$srv
=
parse_url
(
$instance
->
rpcServer
);
$srv
=
parse_url
(
$instance
->
rpcServer
);
$conn
=
new
Hyperf
JsonRpc
([
$conn
=
new
JsonRpc
([
'host'
=>
$srv
[
'host'
],
'host'
=>
$srv
[
'host'
],
'port'
=>
$srv
[
'port'
]
'port'
=>
$srv
[
'port'
],
'mode'
=>
JsonRpc
::
MODE_FRAME
]);
]);
if
(
!
$conn
->
connect
())
{
if
(
!
$conn
->
connect
())
{
...
...
src/base/
Hyperf
JsonRpc.php
→
src/base/JsonRpc.php
View file @
440e7462
...
@@ -10,9 +10,24 @@ namespace internalApi\base;
...
@@ -10,9 +10,24 @@ namespace internalApi\base;
use
RuntimeException
;
use
RuntimeException
;
class
HyperfJsonRpc
/**
* Hyperf JsonRpc 客户端
*/
class
JsonRpc
{
{
/**
* 普通换行符文本协议
* 实现简单
*/
const
MODE_EOF
=
0
;
/**
* 包头(包体长度)+包体的传输协议
* 效率高,使用前确认 frame_head_length 的值
*/
const
MODE_FRAME
=
1
;
/**
/**
* Holds a boolean indicating whether a connection to the server is
* Holds a boolean indicating whether a connection to the server is
* currently established or not.
* currently established or not.
...
@@ -41,7 +56,9 @@ class HyperfJsonRpc
...
@@ -41,7 +56,9 @@ class HyperfJsonRpc
'host'
=>
'127.0.0.1'
,
'host'
=>
'127.0.0.1'
,
'port'
=>
11300
,
'port'
=>
11300
,
'timeout'
=>
1
,
'timeout'
=>
1
,
'logger'
=>
null
'logger'
=>
null
,
'mode'
=>
self
::
MODE_EOF
,
'frame_head_length'
=>
4
];
];
$this
->
_config
=
$config
+
$defaults
;
$this
->
_config
=
$config
+
$defaults
;
}
}
...
@@ -136,7 +153,12 @@ class HyperfJsonRpc
...
@@ -136,7 +153,12 @@ class HyperfJsonRpc
throw
new
RuntimeException
(
$message
);
throw
new
RuntimeException
(
$message
);
}
}
$data
.=
"
\r\n
"
;
if
(
$this
->
_config
[
'mode'
]
==
self
::
MODE_FRAME
)
{
$data
=
pack
(
'N'
,
strlen
(
$data
))
.
$data
;
}
else
{
$data
.=
"
\r\n
"
;
}
return
fwrite
(
$this
->
_connection
,
$data
,
strlen
(
$data
));
return
fwrite
(
$this
->
_connection
,
$data
,
strlen
(
$data
));
}
}
...
@@ -152,7 +174,24 @@ class HyperfJsonRpc
...
@@ -152,7 +174,24 @@ class HyperfJsonRpc
$message
=
'No connection found while reading data from socket.'
;
$message
=
'No connection found while reading data from socket.'
;
throw
new
RuntimeException
(
$message
);
throw
new
RuntimeException
(
$message
);
}
}
return
stream_get_line
(
$this
->
_connection
,
16384
,
"
\r\n
"
);
if
(
$this
->
_config
[
'mode'
]
==
self
::
MODE_FRAME
)
{
$data
=
''
;
$length
=
$current
=
0
;
while
(
$length
==
0
||
$current
<
$length
)
{
$data
.=
fread
(
$this
->
_connection
,
$length
==
0
?
16384
:
$length
-
$current
);
$current
=
strlen
(
$data
);
if
(
$current
<
$this
->
_config
[
'frame_head_length'
])
{
continue
;
}
elseif
(
$length
==
0
)
{
$unpack
=
unpack
(
'Nlen'
,
substr
(
$data
,
0
,
$this
->
_config
[
'frame_head_length'
]));
$length
=
$unpack
[
'len'
]
+
$this
->
_config
[
'frame_head_length'
];
//package length
}
}
return
substr
(
$data
,
$this
->
_config
[
'frame_head_length'
]);
}
else
{
return
stream_get_line
(
$this
->
_connection
,
16384
,
"
\r\n
"
);
}
}
}
public
function
invoke
(
$method
,
$params
,
$id
=
null
)
public
function
invoke
(
$method
,
$params
,
$id
=
null
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment