質問

Platform: CentOS or linux based

I want to build a C++ shared library and it is activated once and it will be like in standby mode and ready to be called by other applications. It is like a COM+ in windows. And I would like to call the C++ library in two ways.

  1. within the same computer, and the process is in-process. So that the communication speed is fast.

  2. (not necessary, but prefer) the shared library can be called using RPC or by other machine within the LAN. e.g. like DCOM

building PHP extension using c++ is a bit messy, I want a connection between PHP and c++ be more "language/platform independent" and yet it has fast speed(like DCOM).

Is there anything solutions something like this?

Research I did: 1. SWIG is a bit close to what I said, but I'm not sure if if it can "stand-by" and I'm not sure if it is in-process communication. 2. no Cobra, I don't use java.

UPDATE

reasons I'm asking this. Currently I'm using PHP, IIS (FastCGI), COM+ and C# on windows and they are working perfectly. But slowly I want to migrate to Linux because of the cost of using Microsoft products. The software cost a lot and C#'s efficiency is slower than native C++ on Linux. But there is nothing like COM+ on Linux....

役に立ちましたか?

解決

On Unix systems, there is no such thing as a shared library that keeps active in memory, even when the programs that use it have all stopped. If this is indeed what you're looking for, you will have to develop a specific server application that runs "forever", and to which client programs can connect and send instructions.

The communication between the client and server normally happens over a network connection, and not within the same process. You could use shared memory if setting up a network connection is indeed a no-go. It reduces the amount of data that has to be copied between the client and server process - but it is a solution that you do not see very often. Network communication (or local sockets) are very common to communicate between client and servers.

Very popular applications to store data are of course databases. If the main goal of your forever-running "shared library" would be to centralize a sort of cache, you could just as well use a database.

With SWIG you indeed have the option to program in C and export the C functions to PHP. But you're right, such a solution is not persistent. Every time that the PHP engine starts, your shared library is re-initialized too. And if multiple instances of the PHP engine are started, multiple instances of your shared library are started too - each with their own data space (although with shared memory you can ensure that all these instances have access to the same data).

An alternative for SWIG would be the PHP-CPP library. This library also allows you to build PHP extensions using C++, and has more expression power than SWIG. But the same limitations apply here: it is not persistent.

By the way, once a PHP extension is loaded, it will be used to serve hundreds or even thousands of pageviews before the extension is unloaded. This means that you can optimize your extension to keep certain data in memory. Not forever - but at least for a long time.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top