下面是C++编写的driver,具体代码结构解析请参见《Programming Erlang》
#include然后用如下命令编译成共享库:#include "erl_driver.h" using namespace std; struct data{ ErlDrvPort port; }; //PORT打开时执行 static ErlDrvData start(ErlDrvPort port, char *buff){ data* d = (data*)driver_alloc(sizeof(data)); d->port = port; return (ErlDrvData)d; } //PORT关闭时执行 static void stop(ErlDrvData handle){ driver_free((char*)handle); } //ERLANG发送来信息时执行 static void output(ErlDrvData handle, char *buff, int bufflen){ cout << buff << endl; char rs[] = "ok"; data* d = (data*)handle; driver_output(d->port, rs, 2); } ErlDrvEntry driver_entry = { NULL, //F_PTR init N/A start, //L_PTR PORT打开时执行此函数 stop, //F_PTR PORT关闭时执行此函数 output, //F_PTR ERLANG发来信息时执行 NULL, //F_PTR ready_input, called when input descriptor ready to read NULL, //F_PTR ready_output "test", //DRIVER名,OPEN_PORT函数的参数 NULL, //F_PTR finish, called when unloaded NULL, //F_PTR control, port_command callback NULL, //F_PTR timeout, reserved NULL //F_PTR outputv, reserved }; extern "C"{ DRIVER_INIT(test){ return &driver_entry; } }
g++ test.cpp -I/usr/local/lib/erlang/usr/include/ -fPIC -shared -o test.so
Erlang里调用这个共享库的代码如下:
-module(test). -compile(export_all). start() -> ok = erl_ddll:load_driver(".", "test"), Port = open_port({spawn, "test"}, [{packet, 2}]), Port ! {self(), {command, "woo just a test"}}, receive {Port, {data, Data}} -> ok = io:format("[~p/~p] ~p\n", [?MODULE, self(), Data]) end, Port ! {self(), close}.
0 意見