Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Communication with C++ Application

Tags:

c++

php

sockets

I have been searching Google for a while, but the problem I am running into is I am not exactly sure what it is I need to be searching for. (Searching for PHP C++ communication doesn't seem to be what I need) I am basically developing a c++ plugin for a game server, and I would like to create a web interface that can pass/pull data to and from the C++ plugin. The game already uses an RCON port for remote administrative access, but I stumbled across a header for the Network interface they use, so I assume I could use this.

My problem is I am not very familiar with using sockets. I assume I will basically need to open a socket in C++ and leave it listening, and then in the PHP, connect to that socket, pass the data, and close it.

Here is the interface... http://www.ampaste.net/m2f6b6dbc

I am mostly just going to be pulling information like current list of connected players, names, and scores. And passing commands to restart the server, shut it down, etc.

Any help would be great, thanks!

like image 462
Josh Renwald Avatar asked Oct 26 '10 05:10

Josh Renwald


2 Answers

You could try Thrift. It was written by the engineers at Facebook, and it's now an Apache project.

Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, Smalltalk, and OCaml.

Link: http://incubator.apache.org/thrift/

In a nutshell it does exactly what you're trying to do. It makes it easy for different languages to communicate with each other. Rather than trying to come up with some socket based protocol for communication, you can call a function in PHP like this:

$game->getScores();

And it automatically plugs into a function named getScores in your C/C++ program. The only drawback is it can be a bit of a pain to configure correctly.

like image 180
mellowsoon Avatar answered Oct 02 '22 19:10

mellowsoon


I'd dare to recommend to use some standard means of distributed components communication, for example, XML RPC. There are libraries for both PHP and C++: http://en.wikipedia.org/wiki/XML-RPC#Implementations

This approach will keep you from reinventing the wheel during communication protocol implementation, and will make further maintenance cheaper.

like image 42
Kel Avatar answered Oct 02 '22 20:10

Kel