<?php error_reporting(E_ALL);
/**
* Example for sending and receiving Messages via the System V Message Queue
*
* To try this script run it synchron/asynchron twice times. One time with ?typ=send and one time with ?typ=receive
*
* @author Thomas Eimers - Mehrkanal GmbH
*
* This document is distributed in the hope that it will be useful, but without any warranty;
* without even the implied warranty of merchantability or fitness for a particular purpose.
*/
header('Content-Type: text/plain; charset=ISO-8859-1');
echo "Start...\n";
// Create System V Message Queue. Integer value is the number of the Queue
$queue = msg_get_queue(100379);
// Sendoptions
$message='nachricht'; // Transfering Data
$serialize_needed=false; // Must the transfer data be serialized ?
$block_send=false; // Block if Message could not be send (Queue full...) (true/false)
$msgtype_send=1; // Any Integer above 0. It signeds every Message. So you could handle multible message
// type in one Queue.
// Receiveoptions
$msgtype_receive=1; // Whiche type of Message we want to receive ? (Here, the type is the same as the type we send,
// but if you set this to 0 you receive the next Message in the Queue with any type.
$maxsize=100; // How long is the maximal data you like to receive.
$option_receive=MSG_IPC_NOWAIT; // If there are no messages of the wanted type in the Queue continue without wating.
// If is set to NULL wait for a Message.
// Send or receive 20 Messages
for ($i=0;$i<20;$i++) {
sleep(1);
// This one sends
if ($_GET['typ']=='send') {
if(msg_send($queue,$msgtype_send, $message,$serialize_needed, $block_send,$err)===true) {
echo "Message sendet.\n";
} else {
var_dump($err);
}
// This one received
} else {
$queue_status=msg_stat_queue($queue);
echo 'Messages in the queue: '.$queue_status['msg_qnum']."\n";
// WARNUNG: nur weil vor einer Zeile Code noch Nachrichten in der Queue waren, muss das jetzt nciht mehr der Fall sein!
if ($queue_status['msg_qnum']>0) {
if (msg_receive($queue,$msgtype_receive ,$msgtype_erhalten,$maxsize,$daten,$serialize_needed, $option_receive, $err)===true) {
echo "Received data".$daten."\n";
} else {
var_dump($err);
}
}
}
}
?>
msg_receive
(PHP 4 >= 4.3.0, PHP 5)
msg_receive — メッセージキューからメッセージを受信する
説明
msg_receive() は、指定した queue から指定した desiredmsgtype の最初のメッセージを受信します。
パラメータ
- queue
-
- desiredmsgtype
-
desiredmsgtype が 0 の場合、 キューの先頭にあるメッセージが返されます。desiredmsgtype が 0 より大きな値の場合、その型のメッセージのうち一番最初にあるものが返されます。 desiredmsgtype が 0 より小さな値の場合、 desiredmsgtype の絶対値と同じかそれより小さい型のメッセージのうち一番最初にあるものが返されます。 条件を満たすメッセージがない場合は、 条件を満たすメッセージがキューに投入されるまで待ち続けます。 パラメータ flags に MSG_IPC_NOWAIT を指定することで、 ブロックモードではなくすることが可能です。
- msgtype
-
受信したメッセージの型がこのパラメータに保存されます。
- maxsize
-
読み込むメッセージの最大サイズは maxsize で指定します。もしキューにあるメッセージのサイズがこれより大きい場合、 (以下で説明する flags が設定されていない限り) この関数は失敗します。
- message
-
エラーが発生しなければ、 受信したメッセージは message に保存されます。
- unserialize
-
unserialize のデフォルト値は TRUE です。 このパラメータが TRUE に設定されている場合、 メッセージはセッションモジュールと同様の方法でシリアライズされているものとみなされます。 メッセージは元の状態に復元されたうえでスクリプトに返されます。 これにより、配列やオブジェクト構造体のような複雑な形式のデータを他の PHP スクリプトから簡単に受信することが可能となります。 また、もし WDDX シリアライザを使用しているなら、あらゆる WDDX 互換のソースからデータを受け取ることが可能となります。
unserialize が FALSE の場合、 メッセージはバイナリセーフな文字列として返されます。
- flags
-
オプションの flags により、低レベルの msgrcv システムコールにフラグを渡すことが可能です。デフォルト値は 0 ですが、以下の値のうちのいくつかを(値を足すかあるいは論理和ととることで) 指定することが可能です。
msg_receive のフラグの値 MSG_IPC_NOWAIT desiredmsgtype を満たすメッセージが存在しない場合に、 待ち続けずにすぐに結果を返します。 関数は失敗し、MSG_ENOMSG に対応する整数値を返します。 MSG_EXCEPT このフラグを正の desiredmsgtype と組み合わせて使用すると、この関数は desiredmsgtype 以外の型をもつ 最初のメッセージを受信するようになります。 MSG_NOERROR このフラグを設定しておくと、メッセージが maxsize より大きい場合に そのメッセージを maxsize までに切り詰め、 エラーを返しません。 - errorcode
-
エラーが発生した場合は、オプションの errorcode にシステムの errno 値が設定されます。
返り値
成功した場合に TRUE を、失敗した場合に FALSE を返します。
処理が正常に完了すると、メッセージキューデータ構造体は以下のように更新されます。 msg_lrpid には呼び出し元のプロセス ID が 設定され、msg_qnum が 1 減少し、 msg_rtime が現在の時刻に設定されます。
msg_receive
20-Mar-2008 01:49
26-Jan-2008 08:13
continue from my previous note
Showing messages from queue will flip-flop. It means you run once send.php, the message will be shown in terminal ...
... #1. Second time in terminal #2, third terminal #1 and so on.
26-Jan-2008 07:27
Consider this e.g. Linux situation:
<?php
//file send.php
$ip = msg_get_queue(12340);
msg_send($ip,8,"abcd",false,false,$err);
//-----------------------------------------------------
<?php
//file receive.php
$ip = msg_get_queue(12340);
msg_receive($ip,0,$msgtype,4,$data,false,null,$err);
echo "msgtype {$msgtype} data {$data}\n";
msg_receive($ip,0,$msgtype,4,$data,false,null,$err);
echo "msgtype {$msgtype} data {$data}\n";
Now run:
in terminal #1 php5 receive.php
in terminal #2 php5 receive.php
in terminal #3 php5 send.php
Showing messages from queue will flip-flop. It means you run once send.php, the message will be shown in terminal #1. Second run it will be in t#2, third #1 and so on.
10-Apr-2003 07:56
This is meant to be run as your apache user in a terminal, call script in note of msg_send and they will communicate.
#! /usr/bin/env php
<?php
$MSGKEY = 519051; // Message
$msg_id = msg_get_queue ($MSGKEY, 0600);
while (1) {
if (msg_receive ($msg_id, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
if ($msg == 'Quit') break;
echo "$msg\n";
} else {
echo "Received $msg_error fetching message\n";
break;
}
}
msg_remove_queue ($msg_id);
?>