2010-05-07

用脚本语言开发网游 – C整合Python

Views: 17384 | Add Comments

像因特网的Web系统一样, 几乎所有的大规模软件系统, 都会使用脚本语言. 在网络游戏服务器的开发中, 也会应用脚本语言. 游戏开发常用的脚本语言主要有: Python, Lua. 本文介绍在C语言中嵌入Python支持, C语言和Python语言通过API进行交互.

任何两种语言要整合, 首先要解决通信问题. C要和Python进行通信, 要进行数据结构转换, 把C的数据结构转为Python的对象, 和把Python对象转换为C数据结构.

下面对代码实例进行讲解, 代码忽略和错误处理.

#include <python2.5/Python.h>

void test1();
void test2();

PyObject *globals

int main(int argc, char **argv){
	PyObject *m, *obj1, *pobj;
	PyObject *request, *response;
	int i;

	Py_Initialize();

变量声明, 以及调用初始化函数 Py_Initialize().

	m = PyImport_AddModule("__main__");
	globals = PyModule_GetDict(m);

获取全局变量的字典表(哈希表). 在 Python 脚本中, 该字典表的键名就是全局变量的的名字. 例如, 全局变量字典表中有一项记录 'a' => '123', 那么, 在 Python 脚本中, 变量 a 的值是 '123'. 该字典表就是 C 语言和 Python 语言进行通信的关键.

	request = PyDict_New();
	response = PyDict_New();

	PyDict_SetItemString(globals, "request", request);
	PyDict_SetItemString(globals, "response", response);

	obj1 = PyInt_FromLong(1);
	PyDict_SetItemString(request, "count", obj1);

在本代码例子中, 字典表 request 用于 C 向 Python 传递数据, response 用于 Python 脚本向 C 程序传递数据. 使用两个字典并不是必须的, 可以只用一个. request 中有一项键名为 'count' 的记录, 相当于向 Python 传递了一个名为 'count' 的参数.

	/* http://www.benegg.com */
	for(i = 0; i < 2; i++){
		test1();
		pobj = PyDict_GetItemString(response, "code");
		printf("code: %s\n", PyString_AsString(pobj));

		test2();
		pobj = PyDict_GetItemString(response, "code");
		printf("code: %s\n", PyString_AsString(pobj));

		printf("---\n");
	}

Python 脚本中返回名为 'code' 的响应参数, 在 C 程序中将该参数的值打印出来.

	Py_DECREF(obj1);
	Py_DECREF(request);
	Py_DECREF(response);

out:
	Py_Finalize();
	return 0;
}

使用宏 Py_DECREF 减少对象的引用计数, 以便 Python 进行垃圾收集. 最后调用 Py_Finalize() 释放资源.

void test1(){
	FILE *fp;
	fp = fopen("benegg.com.py", "r");
	PyRun_SimpleFile(fp, filename);
	fclose(fp);
}

PyRun_SimpleFile() 函数执行一个 Python 脚本文件.

void test2(){
	FILE *fp;
	PyObject *co;
	char *buf;
	long size;

	fp = fopen("benegg.com.py", "r");
	fseek(fp, 0, SEEK_END);
	size = ftell(fp);
	rewind(fp);

	buf = malloc(size + 1);
	fread(buf, 1, size, fp);
	buf[size] = '\0';

	PyRun_SimpleString(buf);

因为 PyRun_SimpleFile() 每执行一次都要读取一次文件, 所以可以把文件内容读取到字符缓冲中, 然后多次执行 PyRun_SimpleString(), 避免磁盘 IO 造成性能消耗.

	co = Py_CompileString(buf, "benegg.com", Py_file_input);
	PyEval_EvalCode((PyCodeObject *)co, globals, globals);

不过, 虽然 PyRun_SimpleString() 避免了磁盘 IO, 然后还是会在每一次执行的时候解析文本代码. 这时, 可以把代码编译成一个 PyCodeObject 对象, 然后调用 PyEval_EvalCode() 执行, 避免了多次解析造成的性能消耗.

	Py_DECREF(co);
out:
	free(buf);
	fclose(fp);
}

Python 脚本代码如下:

print "test1 - %d" % request['count']
request['count'] += 1
response['code'] = "test" + str(request['count'])

编译 C 代码:

# http://www.benegg.com
gcc -g -Wall -o c test.c -L/usr/lib/python2.5/config -lpython2.5

注意, 将路径改为你机器上的实际路径. 执行 ./c, 可以看到, 执行的结果是:

test1 - 1
code: test2
test1 - 2
test1 - 3
code: test4
---
test1 - 4
code: test5
test1 - 5
test1 - 6
code: test7

原发在: http://www.benegg.com/game/archives/51.html

Related posts:

  1. 用C语法来写Python代码
  2. PyPy – 吞下自己尾巴的小蟒蛇
  3. 通过 HTTP POST 发送二进制数据
  4. Cpy是如何打败Python的
  5. 开始学习 Python
Posted by ideawu at 2010-05-07 12:00:06

Leave a Comment