Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial support for persistent connection locking (using WinCache).

  • Loading branch information...
commit 06e6ab0db6c615104426800b8bdf742d5c8fffab 1 parent 99d888f
Vasil Rangelov boenrobot authored
Showing with 86 additions and 2 deletions.
  1. +85 −1 src/PEAR2/Net/Transmitter/TcpClient.php
  2. +1 −1  tests/phpunit.xml
86 src/PEAR2/Net/Transmitter/TcpClient.php
View
@@ -44,6 +44,10 @@ class TcpClient extends NetworkStream
* @var string The error message of the last error on the socket.
*/
protected $error_str = null;
+
+ protected $persistentId = null;
+
+ protected $persistentHandler = null;
/**
* Creates a new connection with the specified options.
@@ -83,12 +87,22 @@ public function __construct($host, $port, $persist = false,
}
try {
+ $uri = "tcp://{$host}:{$port}/{$key}";
parent::__construct(
@stream_socket_client(
- "tcp://{$host}:{$port}/{$key}", $this->error_no,
+ $uri, $this->error_no,
$this->error_str, $timeout, $flags, $context
)
);
+ $this->persistentId
+ = str_replace(
+ array('!' , '|', '/', '\\', '<', '>', '?', '*', '"'),
+ array('~!', '!', '!', '!' , '!', '!', '!', '!', '!'),
+ __NAMESPACE__ . '\TcpClient ' . $uri
+ ) . ' ';
+ if (version_compare(phpversion('wincache'), '1.1.0', '>=')) {
+ $this->persistentHandler = 'wincache';
+ }
} catch (\Exception $e) {
throw $this->createException('Failed to initialize socket.', 7);
}
@@ -110,5 +124,75 @@ protected function createException($message, $code = 0)
$message, $code, null, $this->error_no, $this->error_str
);
}
+
+ protected function lock($key)
+ {
+ if ($this->persist) {
+ switch($this->persistentHandler) {
+ case 'wincache':
+ return wincache_lock($this->persistentId . $key);
+ default:
+ throw $this->createException(
+ 'Make sure WinCache is enabled.', 8
+ );
+ }
+ }
+ return true;
+ }
+
+ protected function unlock($key)
+ {
+ if ($this->persist) {
+ switch($this->persistentHandler) {
+ case 'wincache':
+ return wincache_unlock($this->persistentId . $key);
+ default:
+ throw $this->createException(
+ 'Make sure WinCache is enabled.', 8
+ );
+ }
+ }
+ return true;
+ }
+
+ public function receive($length, $what = 'data')
+ {
+ if ($this->lock('r')) {
+ $result = parent::receive($length, $what);
+ $this->unlock('r');
+ return $result;
+ } else {
+ throw $this->createException(
+ 'Unable to obtain receiving lock', 9
+ );
+ }
+ }
+
+ public function receiveStream(
+ $length, FilterCollection $filters = null, $what = 'stream data'
+ ) {
+ if ($this->lock('r')) {
+ $result = parent::receiveStream($length, $filters, $what);
+ $this->unlock('r');
+ return $result;
+ } else {
+ throw $this->createException(
+ 'Unable to obtain receiving lock', 9
+ );
+ }
+ }
+
+ public function send($contents, $offset = null, $length = null)
+ {
+ if ($this->lock('w')) {
+ $result = parent::send($contents, $offset, $length);
+ $this->unlock('w');
+ return $result;
+ } else {
+ throw $this->createException(
+ 'Unable to obtain sending lock', 10
+ );
+ }
+ }
}
2  tests/phpunit.xml
View
@@ -30,7 +30,7 @@
<!--
A hostname which doesn't provide a serive on any port present here.
-->
- <const name="SILENT_HOSTNAME" value="192.168.0.1" />
+ <const name="SILENT_HOSTNAME" value="::1" />
<!--
A port which doesn't provide a serive on any hostname present here.
-->
Please sign in to comment.
Something went wrong with that request. Please try again.