Osip源代码框架10--osip字段小结

osip

OSIP(Open Source Implementation of SIP)是一个开源的SIP(Session Initiation Protocol)协议栈,主要用于开发VoIP(Voice over Internet Protocol)应用程序。

主要功能

  • SIP消息解析和构建:OSIP提供了丰富的API来解析和构建SIP消息。
  • 事务管理:支持UAC(User Agent Client)、UAS(User Agent Server)等不同角色下的事务处理。
  • 状态机:内置的状态机帮助开发者更好地管理和维护SIP对话的状态。
  • 扩展性:允许用户自定义头字段和其他SIP元素,以便满足特定应用的需求。

osip主体

结构体
struct osip
{
   
   
    void*							application_context;		//User defined Pointer
	// 用户定义的指针,允许开发者存储自定义的应用程序上下文数据
	
    //list of transactions for ict, ist, nict, nist
    osip_list_t						osip_ict_transactions;		//list of ict transactions
    // 客户端事务(Invite Client Transaction, ICT)的列表, 
    // 包含多个osip_transaction_t对象,每个对象表示一个具体的ICT
    osip_list_t						osip_ist_transactions;		//list of ist transactions
    // 服务器事务(Invite Server Transaction, IST)的列表
    //包含多个osip_transaction_t对象,每个对象表示一个具体的IST
    osip_list_t						osip_nict_transactions;		//list of nict transaction
     //处理除INVITE之外的所有其他类型的SIP请求(如REGISTER、SUBSCRIBE等
	
    osip_list_t						osip_nist_transactions;		//list of nist transactions
     // 用于处理接收到的除INVITE之外的所有其他类型的SIP请求。
    osip_list_t						ixt_retransmissions;		//list of ixt elements
	// 管理重新传输的消息元素的列表
    osip_message_cb_t				msg_callbacks[OSIP_MESSAGE_CALLBACK_COUNT]; 
    osip_kill_transaction_cb_t		kill_callbacks[OSIP_KILL_CALLBACK_COUNT];
    osip_transport_error_cb_t		tp_error_callbacks[OSIP_TRANSPORT_ERROR_CALLBACK_COUNT];
	
    int (*cb_send_message)(osip_transaction_t*, osip_message_t*, char*, int, int, int);
	
#if defined(HAVE_DICT_DICT_H)
    dict*							osip_ict_hastable;        //htable of ict transactions
    dict*							osip_ist_hastable;		  //htable of ist transactions
    dict*							osip_nict_hastable;       //htable of nict transactions
    dict*							osip_nist_hastable;       //htable of nist transactions
#endif
}

回调函数:
1 消息回调 (osip_message_cb_t):
功能: 这些回调函数用于处理接收到的各种类型的SIP消息(如INVITE、ACK、BYE等)。每当OSIP库接收到一个新的SIP消息时,它会根据消息类型调用相应的回调函数。
作用: 开发者可以通过实现这些回调函数来定义如何处理不同的SIP消息,从而实现业务逻辑。
2 事务终止回调 (osip_kill_transaction_cb_t):
功能: 这些回调函数用于处理事务的终止。每当OSIP库决定终止一个事务时,它会调用相应的回调函数来清理资源或执行其他必要的操作。
作用: 开发者可以通过实现这些回调函数来确保资源得到正确的释放,并执行任何必要的清理工作。
3 传输层错误回调 (osip_transport_error_cb_t):
功能: 这些回调函数用于处理传输层错误。每当OSIP库检测到传输层错误(如网络连接失败、超时等)时,它会调用相应的回调函数来处理这些错误。
作用: 开发者可以通过实现这些回调函数来应对各种传输层问题,例如重试机制、日志记录等。
4 发送消息回调 (cb_send_message):
功能: 这个回调函数用于发送SIP消息。每当OSIP库需要发送一个SIP消息时,它会调用这个回调函数来完成实际的发送操作。
作用: 开发者可以通过实现这个回调函数来自定义消息的发送方式,例如使用特定的传输协议(TCP、UDP等),或者添加额外的日志记录。

code main函数
int main() {
   
   
    osip_t *osip;
    osip_init(&osip, OSIP_LOG_LEVEL_INFO);

    // 设置消息回调函数
    osip_message_cb_t msg_callbacks[OSIP_MESSAGE_CALLBACK_COUNT];
    for (int i = 0; i < OSIP_MESSAGE_CALLBACK_COUNT; i++) {
   
   
        msg_callbacks[i] = NULL;
    }
    msg_callbacks[OSIP_INVITE] = handle_invite;

    osip->msg_callbacks = msg_callbacks;

    // 模拟接收一个INVITE请求
    const char *invite_request_str =
        "INVITE sip:user@example.com SIP/2.0\r\n"
        "Via: SIP/2.0/UDP localhost:5060;branch=z9hG4bK77ef4c2312983.1\r\n"
        "Max-Forwards: 70\r\n"
        "To: \"User\" <sip:user@example.com>\r\n"
        "From: \"Caller\" <sip:caller@localhost>;tag=12345\r\n"
        "Call-ID: unique-id@localhost\r\n"
        "CSeq: 1 INVITE\r\n"
        "Content-Length: 0\r\n\r\n";

    osip_message_t *invite_request;
    osip_message_init(&invite_request);
    osip_message_parse(invite_request, invite_request_str, strlen(invite_request_str));

    // 创建Invite Client Transaction (ICT)
    osip_transaction_t *transaction;
    osip_ict_new(&transaction, invite_request, osip);

    // 初始化事件
    osip_event_t *event;
    osip_event_init(&event, OSIP_ICT_NEW, transaction, invite_request, NULL, NULL);

    // 处理事件
    osip_handle_events(osip);

    // 清理资源
    osip_message_free(invite_request);
    osip_transaction_free(transaction);
    osip_shutdown(osip);

    return 0;
}

状态机转化

结构体
struct _transition_t
{
   
   
	state_t					state;
	type_t					type;
	void (*method) (void *, void *);
	struct _transition_t*	next;
	struct _transition_t*	parent;
};

struct _transition_t结构体用于描述有限状态机中的一个状态转换。每个转换由以下几个部分组成:

  • 当前状态 (state_t state): 标识转换前的状态。
  • 事件类型 (type_t type): 标识触发转换的事件。
  • 处理方法 (void (*method) (void *, void *)): 指向处理转换的方法,包含具体的业务逻辑。
  • 下一个转换 (struct _transition_t* next): 指向下一个转换,形成链表或树状结构。
  • 父转换 (struct _transition_t* parent): 指向父转换,用于跟踪层次关系。
code状态转换
#include <stdio.h>
#include <stdlib.h>

// 定义状态类型
typedef enum {
   
   
    STATE_IDLE,
    STATE_RUNNING,
    STATE_PAUSED,
    STATE_STOPPED
} state_t;

// 定义事件类型
typedef enum {
   
   
    EVENT_START,
    EVENT_PAUSE,
    EVENT_RESUME,
    EVENT_STOP
} type_t;

// 定义过渡结构体
struct _transition_t {
   
   
    state_t state;
    type_t type;
    void (*method)(void *, void *);
    struct _transition_t *next;
    struct _transition_t *parent;
};

// 状态转换方法的原型
void transition_method_start(void *fsm_instance, void *event_data);
void transition_method_pause(void *fsm_instance, void *event_data);
void transition_method_resume(void *fsm_instance, void *event_data);
void transition_method_stop(void *fsm_instance, void *event_data);

// 模拟的状态机实例
typedef struct {
   
   
    state_t current_state;
} fsm_instance_t;

// 状态转换方法的具体实现
void transition_method_start(void *fsm_instance, void *event_data) {
   
   
    ((fsm_instance_t *)fsm_instance)->current_state = STATE_RUNNING;
    printf("Transition from IDLE to RUNNING\n");
}

void transition_method_pause(void *fsm_instance, void *event_data) {
   
   
    ((fsm_instance_t *)fsm_instance)->current_state = STATE_PAUSED;
    printf("Transition from RUNNING to PAUSED\n");
}

void transition_method_resume(void *fsm_instance, void *event_data) {
   
   
    ((fsm_instance_t *)fsm_instance)->current_state = STATE_RUNNING;
    printf("Transition from PAUSED to RUNNING\n");
}

void transition_method_stop(void *fsm_instance, void *event_data) {
   
   
    ((fsm_instance_t *)fsm_instance)->current_state = STATE_STOPPED;
    printf("Transition from %d to STOPPED\n", ((fsm_instance_t *)fsm_instance)->current_state);
}

// 查找并执行状态转换
void process_event(fsm_instance_t *fsm_instance, type_t event_type, struct _transition_t *transitions) {
   
   
    struct _transition_t *current = transitions;
    while (current != NULL) {
   
   
        if (current->state == fsm_instance->current_state && current->type == event_type) {
   
   
            current->method(fsm_instance, NULL);
            return;
        }
        current = current->next;
    }
    printf("No valid transition found for state %d and event %d\n", fsm_instance->current_state, event_type);
}

int main() {
   
   
    // 初始化状态机实例
    fsm_instance_t fsm_instance;
    fsm_instance.current_state = STATE_IDLE;

    // 创建状态转换链表
    struct _transition_t *transitions = NULL;

    // 添加状态转换
    struct _transition_t *start_transition = malloc(sizeof(struct _tr
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

八月的雨季997

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值