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。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章