OpenSIPS 3.1 开发手册(六)-- BIN Interface API

https://www.opensips.org/Documentation/Development-Manual

15.  BIN Interface API

        Binary Internal Interface是一种OpenSIPS 核心接口,它在单个OpenSIPS 实例之间提供一种高效的通信方式。

        这在无法将实时数据(比如说dialog数据)简单写入数据库时就非常有利,因为故障迁移需要几分钟才能完成。可以通过BIN接口,把所有事件相关的运行数据备份到备用OpenSIPS 实例。BIN接口功能声明在 bin_interface.h文件中.

        接口使用需要两个步骤:

  • 在主服务器上创建并发送新事件
  • 在备用服务器上接收并处理事件

        以下方法用于创建和发送新事件:

/**
 * bin_init - begins the construction of a new binary packet (header part):
 *
 * +-------------------+------------------------------------------------------+
 * |  8-byte HEADER    |                 BODY                max 65535 bytes  |
 * +-------------------+------------------------------------------------------+
 * | PK_MARKER |  CRC  | LEN | MOD_NAME | CMD | LEN | FIELD | LEN | FIELD |...|
 * +-------------------+------------------------------------------------------+
 *
 * @param: { LEN, MOD_NAME } + CMD
 */
int bin_init(str *mod_name, int cmd_type)

/*
 * copies the given string at the 'cpos' position in the buffer
 * allows null strings (NULL content or NULL param)
 *
 * @return: 0 on success
 */
int bin_push_str(const str *info)

/*
 * adds a new integer value at the 'cpos' position in the buffer
 *
 * @return: 0 on success
 */
int bin_push_int(int info)

/**
 * bin_send - computes the checksum of the current packet and then
 * sends the packet over UDP to the @dest destination
 *
 * @return: number of bytes sent, or -1 on error
 */
int bin_send(union sockaddr_union *dest)

        在接收侧,开发者首先注册回调函数,它将在接收特定类型的BIN消息时触发:

/**
 * bin_register_cb - registers a module handler for specific packets
 * @mod_name: used to classify the incoming packets
 * @cb:       the handler function, called once for each matched packet
 *
 * @return:   0 on success
 */
int bin_register_cb(char *mod_name, void (*cb)(int))

        只有mod_name类别的BIN消息会触发注册的回调函数,回调函数也会收到消息包,以便区分不同的事件(比如说eg. create, update, delete, etc )。然后,你需要使用pop方法提取消息包的内容:

/*
 * pops an str from the current position in the buffer
 * @info:   pointer to store the result
 *
 * @return: 0 on success
 *
 * Note: The pointer returned in @info str is only valid for the duration of
 *       the callback. Don't forget to copy the info into a safe buffer!
 */
int bin_pop_str(str *info)

/*
 * pops an integer value from the current position in the buffer
 * @info:   pointer to store the result
 *
 * @return: 0 on success
 */
int bin_pop_int(void *info)

        https://www.opensips.org/Documentation/Interface-Binary-2-2这个页面专注于解释如何配置OpenSIPS 的BNI。

猜你喜欢

转载自blog.csdn.net/yetyongjin/article/details/106619167