| 1 |
<?php |
| 2 |
|
| 3 |
namespace Safe; |
| 4 |
|
| 5 |
use Safe\Exceptions\SemException; |
| 6 |
|
| 7 |
/** |
| 8 |
* Checks whether the message queue key exists. |
| 9 |
* |
| 10 |
* @param int $key Queue key. |
| 11 |
* @throws SemException |
| 12 |
* |
| 13 |
*/ |
| 14 |
function msg_queue_exists(int $key): void |
| 15 |
{ |
| 16 |
error_clear_last(); |
| 17 |
$result = \msg_queue_exists($key); |
| 18 |
if ($result === false) { |
| 19 |
throw SemException::createFromPhpError(); |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* msg_receive will receive the first message from the |
| 26 |
* specified queue of the type specified by |
| 27 |
* desiredmsgtype. |
| 28 |
* |
| 29 |
* @param resource $queue Message queue resource handle |
| 30 |
* @param int $desiredmsgtype If desiredmsgtype is 0, the message from the front |
| 31 |
* of the queue is returned. If desiredmsgtype is |
| 32 |
* greater than 0, then the first message of that type is returned. |
| 33 |
* If desiredmsgtype is less than 0, the first |
| 34 |
* message on the queue with a type less than or equal to the |
| 35 |
* absolute value of desiredmsgtype will be read. |
| 36 |
* If no messages match the criteria, your script will wait until a suitable |
| 37 |
* message arrives on the queue. You can prevent the script from blocking |
| 38 |
* by specifying MSG_IPC_NOWAIT in the |
| 39 |
* flags parameter. |
| 40 |
* @param int|null $msgtype The type of the message that was received will be stored in this |
| 41 |
* parameter. |
| 42 |
* @param int $maxsize The maximum size of message to be accepted is specified by the |
| 43 |
* maxsize; if the message in the queue is larger |
| 44 |
* than this size the function will fail (unless you set |
| 45 |
* flags as described below). |
| 46 |
* @param mixed $message The received message will be stored in message, |
| 47 |
* unless there were errors receiving the message. |
| 48 |
* @param bool $unserialize If set to |
| 49 |
* TRUE, the message is treated as though it was serialized using the |
| 50 |
* same mechanism as the session module. The message will be unserialized |
| 51 |
* and then returned to your script. This allows you to easily receive |
| 52 |
* arrays or complex object structures from other PHP scripts, or if you |
| 53 |
* are using the WDDX serializer, from any WDDX compatible source. |
| 54 |
* |
| 55 |
* If unserialize is FALSE, the message will be |
| 56 |
* returned as a binary-safe string. |
| 57 |
* @param int $flags The optional flags allows you to pass flags to the |
| 58 |
* low-level msgrcv system call. It defaults to 0, but you may specify one |
| 59 |
* or more of the following values (by adding or ORing them together). |
| 60 |
* |
| 61 |
* Flag values for msg_receive |
| 62 |
* |
| 63 |
* |
| 64 |
* |
| 65 |
* MSG_IPC_NOWAIT |
| 66 |
* If there are no messages of the |
| 67 |
* desiredmsgtype, return immediately and do not |
| 68 |
* wait. The function will fail and return an integer value |
| 69 |
* corresponding to MSG_ENOMSG. |
| 70 |
* |
| 71 |
* |
| 72 |
* |
| 73 |
* MSG_EXCEPT |
| 74 |
* Using this flag in combination with a |
| 75 |
* desiredmsgtype greater than 0 will cause the |
| 76 |
* function to receive the first message that is not equal to |
| 77 |
* desiredmsgtype. |
| 78 |
* |
| 79 |
* |
| 80 |
* MSG_NOERROR |
| 81 |
* |
| 82 |
* If the message is longer than maxsize, |
| 83 |
* setting this flag will truncate the message to |
| 84 |
* maxsize and will not signal an error. |
| 85 |
* |
| 86 |
* |
| 87 |
* |
| 88 |
* |
| 89 |
* |
| 90 |
* @param int|null $errorcode If the function fails, the optional errorcode |
| 91 |
* will be set to the value of the system errno variable. |
| 92 |
* @throws SemException |
| 93 |
* |
| 94 |
*/ |
| 95 |
function msg_receive($queue, int $desiredmsgtype, ?int &$msgtype, int $maxsize, &$message, bool $unserialize = true, int $flags = 0, ?int &$errorcode = null): void |
| 96 |
{ |
| 97 |
error_clear_last(); |
| 98 |
$result = \msg_receive($queue, $desiredmsgtype, $msgtype, $maxsize, $message, $unserialize, $flags, $errorcode); |
| 99 |
if ($result === false) { |
| 100 |
throw SemException::createFromPhpError(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* msg_remove_queue destroys the message queue specified |
| 107 |
* by the queue. Only use this function when all |
| 108 |
* processes have finished working with the message queue and you need to |
| 109 |
* release the system resources held by it. |
| 110 |
* |
| 111 |
* @param resource $queue Message queue resource handle |
| 112 |
* @throws SemException |
| 113 |
* |
| 114 |
*/ |
| 115 |
function msg_remove_queue($queue): void |
| 116 |
{ |
| 117 |
error_clear_last(); |
| 118 |
$result = \msg_remove_queue($queue); |
| 119 |
if ($result === false) { |
| 120 |
throw SemException::createFromPhpError(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
/** |
| 126 |
* msg_send sends a message of type |
| 127 |
* msgtype (which MUST be greater than 0) to |
| 128 |
* the message queue specified by queue. |
| 129 |
* |
| 130 |
* @param resource $queue Message queue resource handle |
| 131 |
* @param int $msgtype The type of the message (MUST be greater than 0) |
| 132 |
* @param mixed $message The body of the message. |
| 133 |
* |
| 134 |
* If serialize set to FALSE is supplied, |
| 135 |
* MUST be of type: string, integer, float |
| 136 |
* or bool. In other case a warning will be issued. |
| 137 |
* @param bool $serialize The optional serialize controls how the |
| 138 |
* message is sent. serialize |
| 139 |
* defaults to TRUE which means that the message is |
| 140 |
* serialized using the same mechanism as the session module before being |
| 141 |
* sent to the queue. This allows complex arrays and objects to be sent to |
| 142 |
* other PHP scripts, or if you are using the WDDX serializer, to any WDDX |
| 143 |
* compatible client. |
| 144 |
* @param bool $blocking If the message is too large to fit in the queue, your script will wait |
| 145 |
* until another process reads messages from the queue and frees enough |
| 146 |
* space for your message to be sent. |
| 147 |
* This is called blocking; you can prevent blocking by setting the |
| 148 |
* optional blocking parameter to FALSE, in which |
| 149 |
* case msg_send will immediately return FALSE if the |
| 150 |
* message is too big for the queue, and set the optional |
| 151 |
* errorcode to MSG_EAGAIN, |
| 152 |
* indicating that you should try to send your message again a little |
| 153 |
* later on. |
| 154 |
* @param int|null $errorcode If the function fails, the optional errorcode will be set to the value of the system errno variable. |
| 155 |
* @throws SemException |
| 156 |
* |
| 157 |
*/ |
| 158 |
function msg_send($queue, int $msgtype, $message, bool $serialize = true, bool $blocking = true, ?int &$errorcode = null): void |
| 159 |
{ |
| 160 |
error_clear_last(); |
| 161 |
$result = \msg_send($queue, $msgtype, $message, $serialize, $blocking, $errorcode); |
| 162 |
if ($result === false) { |
| 163 |
throw SemException::createFromPhpError(); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
/** |
| 169 |
* msg_set_queue allows you to change the values of the |
| 170 |
* msg_perm.uid, msg_perm.gid, msg_perm.mode and msg_qbytes fields of the |
| 171 |
* underlying message queue data structure. |
| 172 |
* |
| 173 |
* Changing the data structure will require that PHP be running as the same |
| 174 |
* user that created the queue, owns the queue (as determined by the |
| 175 |
* existing msg_perm.xxx fields), or be running with root privileges. |
| 176 |
* root privileges are required to raise the msg_qbytes values above the |
| 177 |
* system defined limit. |
| 178 |
* |
| 179 |
* @param resource $queue Message queue resource handle |
| 180 |
* @param array $data You specify the values you require by setting the value of the keys |
| 181 |
* that you require in the data array. |
| 182 |
* @throws SemException |
| 183 |
* |
| 184 |
*/ |
| 185 |
function msg_set_queue($queue, array $data): void |
| 186 |
{ |
| 187 |
error_clear_last(); |
| 188 |
$result = \msg_set_queue($queue, $data); |
| 189 |
if ($result === false) { |
| 190 |
throw SemException::createFromPhpError(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
/** |
| 196 |
* sem_acquire by default blocks (if necessary) until the |
| 197 |
* semaphore can be acquired. A process attempting to acquire a semaphore which |
| 198 |
* it has already acquired will block forever if acquiring the semaphore would |
| 199 |
* cause its maximum number of semaphore to be exceeded. |
| 200 |
* |
| 201 |
* After processing a request, any semaphores acquired by the process but not |
| 202 |
* explicitly released will be released automatically and a warning will be |
| 203 |
* generated. |
| 204 |
* |
| 205 |
* @param resource $sem_identifier sem_identifier is a semaphore resource, |
| 206 |
* obtained from sem_get. |
| 207 |
* @param bool $nowait Specifies if the process shouldn't wait for the semaphore to be acquired. |
| 208 |
* If set to true, the call will return |
| 209 |
* false immediately if a semaphore cannot be immediately |
| 210 |
* acquired. |
| 211 |
* @throws SemException |
| 212 |
* |
| 213 |
*/ |
| 214 |
function sem_acquire($sem_identifier, bool $nowait = false): void |
| 215 |
{ |
| 216 |
error_clear_last(); |
| 217 |
$result = \sem_acquire($sem_identifier, $nowait); |
| 218 |
if ($result === false) { |
| 219 |
throw SemException::createFromPhpError(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
/** |
| 225 |
* sem_get returns an id that can be used to |
| 226 |
* access the System V semaphore with the given key. |
| 227 |
* |
| 228 |
* A second call to sem_get for the same key |
| 229 |
* will return a different semaphore identifier, but both |
| 230 |
* identifiers access the same underlying semaphore. |
| 231 |
* |
| 232 |
* If key is 0, a new private semaphore |
| 233 |
* is created for each call to sem_get. |
| 234 |
* |
| 235 |
* @param int $key |
| 236 |
* @param int $max_acquire The number of processes that can acquire the semaphore simultaneously |
| 237 |
* is set to max_acquire. |
| 238 |
* @param int $perm The semaphore permissions. Actually this value is |
| 239 |
* set only if the process finds it is the only process currently |
| 240 |
* attached to the semaphore. |
| 241 |
* @param int $auto_release Specifies if the semaphore should be automatically released on request |
| 242 |
* shutdown. |
| 243 |
* @return resource Returns a positive semaphore identifier on success. |
| 244 |
* @throws SemException |
| 245 |
* |
| 246 |
*/ |
| 247 |
function sem_get(int $key, int $max_acquire = 1, int $perm = 0666, int $auto_release = 1) |
| 248 |
{ |
| 249 |
error_clear_last(); |
| 250 |
$result = \sem_get($key, $max_acquire, $perm, $auto_release); |
| 251 |
if ($result === false) { |
| 252 |
throw SemException::createFromPhpError(); |
| 253 |
} |
| 254 |
return $result; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
/** |
| 259 |
* sem_release releases the semaphore if it |
| 260 |
* is currently acquired by the calling process, otherwise |
| 261 |
* a warning is generated. |
| 262 |
* |
| 263 |
* After releasing the semaphore, sem_acquire |
| 264 |
* may be called to re-acquire it. |
| 265 |
* |
| 266 |
* @param resource $sem_identifier A Semaphore resource handle as returned by |
| 267 |
* sem_get. |
| 268 |
* @throws SemException |
| 269 |
* |
| 270 |
*/ |
| 271 |
function sem_release($sem_identifier): void |
| 272 |
{ |
| 273 |
error_clear_last(); |
| 274 |
$result = \sem_release($sem_identifier); |
| 275 |
if ($result === false) { |
| 276 |
throw SemException::createFromPhpError(); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
|
| 281 |
/** |
| 282 |
* sem_remove removes the given semaphore. |
| 283 |
* |
| 284 |
* After removing the semaphore, it is no longer accessible. |
| 285 |
* |
| 286 |
* @param resource $sem_identifier A semaphore resource identifier as returned |
| 287 |
* by sem_get. |
| 288 |
* @throws SemException |
| 289 |
* |
| 290 |
*/ |
| 291 |
function sem_remove($sem_identifier): void |
| 292 |
{ |
| 293 |
error_clear_last(); |
| 294 |
$result = \sem_remove($sem_identifier); |
| 295 |
if ($result === false) { |
| 296 |
throw SemException::createFromPhpError(); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
/** |
| 302 |
* shm_put_var inserts or updates the |
| 303 |
* variable with the given |
| 304 |
* variable_key. |
| 305 |
* |
| 306 |
* Warnings (E_WARNING level) will be issued if |
| 307 |
* shm_identifier is not a valid SysV shared memory |
| 308 |
* index or if there was not enough shared memory remaining to complete your |
| 309 |
* request. |
| 310 |
* |
| 311 |
* @param resource $shm_identifier A shared memory resource handle as returned by |
| 312 |
* shm_attach |
| 313 |
* @param int $variable_key The variable key. |
| 314 |
* @param mixed $variable The variable. All variable types |
| 315 |
* that serialize supports may be used: generally |
| 316 |
* this means all types except for resources and some internal objects |
| 317 |
* that cannot be serialized. |
| 318 |
* @throws SemException |
| 319 |
* |
| 320 |
*/ |
| 321 |
function shm_put_var($shm_identifier, int $variable_key, $variable): void |
| 322 |
{ |
| 323 |
error_clear_last(); |
| 324 |
$result = \shm_put_var($shm_identifier, $variable_key, $variable); |
| 325 |
if ($result === false) { |
| 326 |
throw SemException::createFromPhpError(); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
/** |
| 332 |
* Removes a variable with a given variable_key |
| 333 |
* and frees the occupied memory. |
| 334 |
* |
| 335 |
* @param resource $shm_identifier The shared memory identifier as returned by |
| 336 |
* shm_attach |
| 337 |
* @param int $variable_key The variable key. |
| 338 |
* @throws SemException |
| 339 |
* |
| 340 |
*/ |
| 341 |
function shm_remove_var($shm_identifier, int $variable_key): void |
| 342 |
{ |
| 343 |
error_clear_last(); |
| 344 |
$result = \shm_remove_var($shm_identifier, $variable_key); |
| 345 |
if ($result === false) { |
| 346 |
throw SemException::createFromPhpError(); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
/** |
| 352 |
* shm_remove removes the shared memory |
| 353 |
* shm_identifier. All data will be destroyed. |
| 354 |
* |
| 355 |
* @param resource $shm_identifier The shared memory identifier as returned by |
| 356 |
* shm_attach |
| 357 |
* @throws SemException |
| 358 |
* |
| 359 |
*/ |
| 360 |
function shm_remove($shm_identifier): void |
| 361 |
{ |
| 362 |
error_clear_last(); |
| 363 |
$result = \shm_remove($shm_identifier); |
| 364 |
if ($result === false) { |
| 365 |
throw SemException::createFromPhpError(); |
| 366 |
} |
| 367 |
} |
| 368 |
|