2012-06-02 4 views
13

PHP에서 클라이언트와 서버가 공유 메모리를 통해 통신하고 있습니다. 이제 Boost.Interprocess를 사용하여이 파쇄 메모리 객체에 액세스하고 싶습니다. 어떻게 액세스 할 수 있습니까? server.php :PHP와 부스트 라이브러리 IPC간에 통신하는 방법은 무엇입니까?

function create_image($str){ 
    // Create a blank image and add some text 
    $im = imagecreatetruecolor(300, 20); 
    $text_color = imagecolorallocate($im, 233, 14, 91); 
    $stringBanner=exec("date").$str; 
    imagestring($im, 1, 5, 5, $stringBanner , $text_color); 

    ob_start(); 
    imagejpeg($im); 
    $i = ob_get_contents(); 
    ob_get_clean(); 
    imagedestroy($im); 
    return $i; 
    } 
    echo "\n".__FILE__."\n"; 
    $shm_key = ftok(__FILE__, 't'); 
    echo $shm_key."\n"; 


    $shm_id = shmop_open($shm_key, "a", 0, 0); 
if ($shm_id) { 
    //it is already created 
    shmop_delete($shm_id); 
    shmop_close($shm_id); 
} 
//you need to create it with shmop_open using "c" only 
echo "try to create\n"; 
if(!$shm_id = shmop_open($shm_key, "c", 0777, 1024*4))exit(-1); 


echo "ID ".$shm_id."\n"; 
$i=0; 
for(;;){ 
sleep(1); 
$s="i=".$i++; 
$str=$i; 
$im=serialize(create_image($str)); 

$data=serialize(strlen($im)); 
$shm_bytes_written = shmop_write($shm_id, $data, 0); 
$shm_bytes_written = shmop_write($shm_id, $im, 32); 
echo $shm_bytes_written." bytes is written: ".$s." ID = $shm_id\n"; 
} 

client.php 나는이 메모리 영역을 얻을 향상을 제공해야 키의 어떤 종류의

<?php 
$shm_key =1946222626;// ftok(__FILE__, 't'); 
$shm_id = shmop_open(
     $shm_key, "a", 
     0644,1024*4 
     ); 


$s=shmop_size($shm_id); 
$data = unserialize(
     shmop_read($shm_id, 0, 
      31) 
     ); 

$im = unserialize(
     shmop_read($shm_id, 32, 
       $data) 
     ); 
    // Set the content type header - in this case image/jpeg 
header('Content-Type: image/jpeg'); 
// Output the image 
echo $im; 

?

boost_client.cpp

#include <boost/interprocess/shared_memory_object.hpp> 
#include <iostream> 
#include "sys/msg.h" 

int main() 
{ 
    int msqid; 
    key_t key; 
    char f[]="??????"; 
    int mid; 

    //key = ftok(, 't'); 
    //msqid = msgget(key, 0666 | IPC_CREAT); 

    std::cout<<msqid<<std::endl; 
    boost::interprocess::shared_memory_object 
    shdmem(boost::interprocess::open_or_create, 
      f,//"shmem_server", 
      boost::interprocess::read_write); 
    shdmem.truncate(1024); 
    std::cout << shdmem.get_name() << std::endl; 
    boost::interprocess::offset_t size; 
    if (shdmem.get_size(size)) 
    std::cout << size << std::endl; 
} 

편집 : 그럼 난 부스트 IPC 라이브러리 문서에서 해결책을 찾을

:

XSI_KEY based example from boost Docs

+0

PHP 확장은 * System V IPC 키 *를 사용하므로 어떤 것을 사용합니까? – hakre

+0

예, 물론 그 장면 뒤에 숨어 있습니다. – Arman

+0

"장면 뒤에 숨겨진"이란 무엇을 의미합니까? – hakre

답변

1

내가 전문가가 아니에요 무엇을 당신이하고있는 일이지만, 내가 당신의 질문과 지식에서 읽은 것에서, 나는 그 순수한 IPC의 것을 버리고 감쌌다. ZMQ으로 가십시오 (필요한 모든 언어로 래퍼를 찾을 수 있습니다). 이러한 종류의 문제를 해결하고 IPC 나 더 일반적인 TCP 소켓을 통해 실행할 수있는 단일 API를 제공하기위한 것입니다.