From d352b53c5dc14279fc3928379d28fcf50986d27c Mon Sep 17 00:00:00 2001 From: Aldo Armiento Date: Tue, 30 Oct 2012 01:06:52 +0100 Subject: [PATCH] New time interval after each socket write. After each socket write operation now we are sleeping for a time interval. The default time interval is 10ms. To speed up the sending operations, use Zero as parameter but some messages may be lost (closes #2). --- ApnsPHP/Abstract.php | 27 +++++++++++++++++++++++++++ ApnsPHP/Push.php | 1 + sample_push_many.php | 6 ++++++ 3 files changed, 34 insertions(+) diff --git a/ApnsPHP/Abstract.php b/ApnsPHP/Abstract.php index 80ab8d0e..22e53c66 100644 --- a/ApnsPHP/Abstract.php +++ b/ApnsPHP/Abstract.php @@ -44,6 +44,7 @@ abstract class ApnsPHP_Abstract const DEVICE_BINARY_SIZE = 32; /**< @type integer Device token length. */ + const WRITE_INTERVAL = 10000; /**< @type integer Default write interval in micro seconds. */ const CONNECT_RETRY_INTERVAL = 1000000; /**< @type integer Default connect retry interval in micro seconds. */ const SOCKET_SELECT_TIMEOUT = 1000000; /**< @type integer Default socket select timeout in micro seconds. */ @@ -58,6 +59,7 @@ abstract class ApnsPHP_Abstract protected $_sProviderCertificatePassphrase; /**< @type string Provider certificate passphrase. */ protected $_sRootCertificationAuthorityFile; /**< @type string Root certification authority file. */ + protected $_nWriteInterval; /**< @type integer Write interval in micro seconds. */ protected $_nConnectRetryInterval; /**< @type integer Connect retry interval in micro seconds. */ protected $_nSocketSelectTimeout; /**< @type integer Socket select timeout in micro seconds. */ @@ -91,6 +93,7 @@ public function __construct($nEnvironment, $sProviderCertificateFile) $this->_sProviderCertificateFile = $sProviderCertificateFile; $this->_nConnectTimeout = ini_get("default_socket_timeout"); + $this->_nWriteInterval = self::WRITE_INTERVAL; $this->_nConnectRetryInterval = self::CONNECT_RETRY_INTERVAL; $this->_nSocketSelectTimeout = self::SOCKET_SELECT_TIMEOUT; } @@ -184,6 +187,30 @@ public function getCertificateAuthority() return $this->_sRootCertificationAuthorityFile; } + /** + * Set the write interval. + * + * After each socket write operation we are sleeping for this + * time interval. To speed up the sending operations, use Zero + * as parameter but some messages may be lost. + * + * @param $nWriteInterval @type integer Write interval in micro seconds. + */ + public function setWriteInterval($nWriteInterval) + { + $this->_nWriteInterval = (int)$nWriteInterval; + } + + /** + * Get the write interval. + * + * @return @type integer Write interval in micro seconds. + */ + public function getWriteInterval() + { + return $this->_nWriteInterval; + } + /** * Set the connection timeout. * diff --git a/ApnsPHP/Push.php b/ApnsPHP/Push.php index 50257996..f73f3ad1 100644 --- a/ApnsPHP/Push.php +++ b/ApnsPHP/Push.php @@ -181,6 +181,7 @@ public function send() ) ); } + usleep($this->_nWriteInterval); $bError = $this->_updateQueue($aErrorMessage); if ($bError) { diff --git a/sample_push_many.php b/sample_push_many.php index ea22d2ae..d1acb7b9 100644 --- a/sample_push_many.php +++ b/sample_push_many.php @@ -40,6 +40,12 @@ // Set the Root Certificate Autority to verify the Apple remote peer $push->setRootCertificationAuthority('entrust_root_certification_authority.pem'); +// Increase write interval to 100ms (default value is 10ms). +// This is an example value, the 10ms default value is OK in most cases. +// To speed up the sending operations, use Zero as parameter but +// some messages may be lost. +// $push->setWriteInterval(100 * 1000); + // Connect to the Apple Push Notification Service $push->connect();