PHP communicates with C (or other languages) through message queues, complete code

Source: Internet
Author: User

<? Php
/*
* Class msg
* Use for communication between php and php;
* Create:
* Author: leixun (lein_urg@163.com)
* Version 1-
*/
Class msg {
Private $ id;
Private $ msg_id;
Private $ _ serialize = true;
/**
* @ Param $ _ id ID
*/
Public function msg ($ _ id, $ _ serialize = true ){
If (! Function_exists ('msg _ get_queue '))
{
Die ('msg queue function not installed, Reconfigure PHP with -- enable-sysvmsg <br/> ');
}
$ This-> id = $ _ id;
$ This-> msg_id = msg_get_queue ($ _ id );
$ This-> _ serialize =$ _ serialize;
If ($ this-> msg_id = false ){
Die (basename (_ FILE _). '->'. _ LINE _. ': Unable to create message quee ');
}
}
/**
* @ Data to send
* @ Type message type
*/
Public function send ($ data, $ type = 1, $ blocking = false)
{
If (! Msg_send ($ this-> msg_id, $ type, $ data, $ this-> _ serialize, $ blocking, $ msg_err ))
{
Return "Msg not sent because $ msg_err \ n ";
}
Return true;
}
 
/**
* @ Param $ type message type
* @ Param $ maxsize The maximum size of message to be accepted,
*/
Public function receive ($ no_wait = true, $ type = 1, $ maxsize = 1024)
{
$ Rs = msg_receive ($ this-> msg_id, $ type, $ type, $ maxsize, $ message, $ this-> _ serialize, $ no_wait? MSG_IPC_NOWAIT: NULL, $ errorcode );
If ($ rs)
Return $ message;
Else
Return false;
}
 
Public function remove ()
{
Msg_remove_queue ($ this-> msg_id );
}
}
[Php] view plaincopy
<? Php
Define ('base _ path', dirname (_ FILE _); // msg_write.php
Include (base_path. '/msg. php ');
$ Msg = new msg (1, false );
$ Msg1 = new msg (2, false );
If ($ argv [1] = 'del ') $ msg-> remove ();


$ Str = 'there are no user contributed notes for this page .';
While (1 ){
$ Data = substr ($ str, 0, rand (18, 25 ));
$ Msg-> send (rand (). $ data, rand (1, 10 ));
Echo $ data. "-> sent \ n ";
Echo 'get: '. $ msg1-> receive (false, 0). chr (10 );
Sleep (3 );
// Usleep (10000 );
}
Echo 'done ';
C, gcc-g-o m msg. c-lpthread;
[Cpp] view plaincopy
# Include <stdio. h>
# Include <errno. h>
 
# Include <stdlib. h>
# Include <fcntl. h>
# Include <string. h>
# Include <unistd. h>
# Include <sys/types. h>
# Include <sys/ipc. h>
# Include <sys/msg. h>
# Define MAX_MSG_LEN 512.
Static int php_msg =-1;
Static int php_msg1 =-1;
Static int running = 1;
Static void * php_msg_handler_thread (void * arg );
Static int msg_send (int msg_id, int fd, char * data );
Struct msg_st {
Long mtype;
Char mtext [MAX_MSG_LEN];
};
Void logst (struct msg_st some_data );
Int main (int argc, char ** argv)
{
Printf ("go 1 \ n ");
If (php_msg = msgget (key_t) 1,0666 | IPC_CREAT) =-1)
{
Perror ("php_msg create ");
Return 0;
}
If (php_msg1 = msgget (key_t) 2,0666 | IPC_CREAT) =-1)
{
Perror ("php_msg create ");
Return 0;
}
//////////////////////////////////////// /////////////////////////////////////////
Pthread_t php_msg_pthread;
Int rs = pthread_create (& php_msg_pthread, NULL, (void * (*) (void *) php_msg_handler_thread, (void *) NULL );
If (rs! = 0)
{
Perror ("php_msg_pthread create ");
Return 0;
}
Pthread_join (php_msg_pthread, NULL );
Return 0;
}
Static void * php_msg_handler_thread (void * arg)
{
Struct msg_st php_data;
Printf ("sizeof (struct msg_st) = % d \ n", sizeof (struct msg_st ));
Int msg_to_recevie = 0;
Char * data;
Data = malloc (MAX_MSG_LEN );
Char * pre = "You told me :";
While (running ){
If (msgrcv (php_msg, (void *) & php_data, MAX_MSG_LEN, msg_to_recevie, 0) =-1)
{
Perror ("msgrcv ");
If (errno = E2BIG)
{
If (msgctl (php_msg, IPC_RMID, 0) =-1)
{
Fprintf (stderr, "msgctl (IPC_RMID) failed \ n ");
}
}
Else if (errno = EINVAL)
{
Sleep (1 );
}
} Else {
Printf ("recevier mssage: % s, type = % d, msg_to_recevie = % d; \ n", php_data.mtext, php_data.mtype, msg_to_recevie );
Memset (data, '\ 0', MAX_MSG_LEN );
Memcpy (data, pre, strlen (pre ));
Memcpy (data + strlen (pre), php_data.mtext, strlen (php_data.mtext ));
Msg_send (php_msg1, 2, data );
Bzero (php_data.mtext, strlen (php_data.mtext ));
}
// Break;
}
Free (data );
}
Static int msg_send (int msg_id, int fd, char * data)
{
Struct msg_st * some_data;
Some_data = malloc (sizeof (struct msg_st ));
Memcpy (some_data-> mtext, data, strlen (data) + 1 );
Some_data-> mtext [strlen (data)] = '\ 0 ';
Some_data-> mtype = fd;
Printf ("will send % s \ n", & some_data-> mtext );
If (msgsnd (msg_id, (void *) some_data, MAX_MSG_LEN, 0) =-1)
{
Perror ("msgsnd ");
Return 0;
}
Return 1;
}
Void logst (struct msg_st some_data)
{
FILE * fp;
Fp = fopen ("file. dat", "w + ");
If (! Fp)
{
Printf ("open file error! ");
Return;
}
Fwrite (& some_data, sizeof (struct msg_st), 1, fp );
Fclose (fp );
}
Run:./m
Run: php msg_write.php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.