Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dokuwiki
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
BRIC
dokuwiki
Commits
24f112d0
Commit
24f112d0
authored
12 years ago
by
Tom N Harris
Browse files
Options
Downloads
Patches
Plain Diff
Efficiently wait on sockets
parent
288188af
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
inc/HTTPClient.php
+37
-8
37 additions, 8 deletions
inc/HTTPClient.php
with
37 additions
and
8 deletions
inc/HTTPClient.php
+
37
−
8
View file @
24f112d0
...
...
@@ -507,7 +507,8 @@ class HTTPClient {
throw
new
HTTPClientException
(
"Socket disconnected while writing
$message
"
);
// wait for stream ready or timeout
if
(
@
stream_select
(
$sel_r
,
$sel_w
,
$sel_e
,
$this
->
timeout
-
$time_used
)
!==
false
){
self
::
selecttimeout
(
$this
->
timeout
-
$time_used
,
$sec
,
$usec
);
if
(
@
stream_select
(
$sel_r
,
$sel_w
,
$sel_e
,
$sec
,
$usec
)
!==
false
){
// write to stream
$nbytes
=
fwrite
(
$socket
,
substr
(
$data
,
$written
,
4096
));
if
(
$nbytes
===
false
)
...
...
@@ -533,6 +534,11 @@ class HTTPClient {
* @author Tom N Harris <tnharris@whoopdedo.org>
*/
function
_readData
(
$socket
,
$nbytes
,
$message
,
$ignore_eof
=
false
)
{
// select parameters
$sel_r
=
array
(
$socket
);
$sel_w
=
null
;
$sel_e
=
null
;
$r_data
=
''
;
$to_read
=
$nbytes
?
$nbytes
:
4096
;
if
(
$to_read
<
0
)
$to_read
=
-
$to_read
;
...
...
@@ -544,10 +550,16 @@ class HTTPClient {
-
100
);
if
(
!
$ignore_eof
&&
feof
(
$socket
))
throw
new
HTTPClientException
(
"Premature End of File (socket) while reading
$message
"
);
//usleep(1000);
$bytes
=
fread
(
$socket
,
$to_read
);
$r_data
.
=
$bytes
;
$to_read
-=
strlen
(
$bytes
);
// wait for stream ready or timeout
self
::
selecttimeout
(
$this
->
timeout
-
$time_used
,
$sec
,
$usec
);
if
(
@
stream_select
(
$sel_r
,
$sel_w
,
$sel_e
,
$sec
,
$usec
)
!==
false
){
$bytes
=
fread
(
$socket
,
$to_read
);
if
(
$bytes
===
false
)
throw
new
HTTPClientException
(
"Failed reading from socket while reading
$message
"
,
-
100
);
$r_data
.
=
$bytes
;
$to_read
-=
strlen
(
$bytes
);
}
}
while
(
strlen
(
$r_data
)
<
$nbytes
);
return
$r_data
;
}
...
...
@@ -562,6 +574,11 @@ class HTTPClient {
* @author Tom N Harris <tnharris@whoopdedo.org>
*/
function
_readLine
(
$socket
,
$message
)
{
// select parameters
$sel_r
=
array
(
$socket
);
$sel_w
=
null
;
$sel_e
=
null
;
$r_data
=
''
;
do
{
$time_used
=
$this
->
_time
()
-
$this
->
start
;
...
...
@@ -571,8 +588,12 @@ class HTTPClient {
-
100
);
if
(
feof
(
$socket
))
throw
new
HTTPClientException
(
"Premature End of File (socket) while reading
$message
"
);
usleep
(
1000
);
$r_data
.
=
fgets
(
$socket
,
1024
);
// wait for stream ready or timeout
self
::
selecttimeout
(
$this
->
timeout
-
$time_used
,
$sec
,
$usec
);
if
(
@
stream_select
(
$sel_r
,
$sel_w
,
$sel_e
,
$sec
,
$usec
)
!==
false
){
$r_data
=
fgets
(
$socket
,
1024
);
}
}
while
(
!
preg_match
(
'/\n$/'
,
$r_data
));
return
$r_data
;
}
...
...
@@ -597,11 +618,19 @@ class HTTPClient {
/**
* Return current timestamp in microsecond resolution
*/
function
_time
(){
static
function
_time
(){
list
(
$usec
,
$sec
)
=
explode
(
" "
,
microtime
());
return
((
float
)
$usec
+
(
float
)
$sec
);
}
/**
* Calculate seconds and microseconds
*/
static
function
selecttimeout
(
$time
,
&
$sec
,
&
$usec
){
$sec
=
floor
(
$time
);
$usec
=
(
int
)((
$time
-
$sec
)
*
1000000
);
}
/**
* convert given header string to Header array
*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment