一个基于 exosip 库的 UAC 和 UAS 的代码整理

2024-02-10 18:58
文章标签 代码 整理 uac exosip uas

本文主要是介绍一个基于 exosip 库的 UAC 和 UAS 的代码整理,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://ordinarysky.cn/?p=164

———– UAC 代理客户端的代码整理 —————

/** 
* 一个使用了 osip 和 eXosip 库的 UAC 代理客户端的演示程序 
* 
* - 只是简单的演示了使用了 osip 和 eXosip2 库的 UAC 代理客户端的如下几个功能: 
* * i 发起呼叫 INVITE 请求 
* * h 挂断会话 
* * s 执行方法 INFO 
* * m 执行方法 MESSAGE* 
* 编 译:g++ -I/usr/local/include -L/usr/local/lib ua_client.cpp -o ua_client -leXosip2 -losip2 -losipparser2 -lpthread 
* 
*/ 
#include <osip2/osip_mt.h> 
#include <eXosip2/eXosip.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <sys/types.h> 
#include <iostream> 
#include <string> 
using namespace std; 
int main(int argc, char* argv[]) 
{ eXosip_event_t *je; osip_message_t *reg = NULL; osip_message_t *invite = NULL; osip_message_t *ack = NULL; osip_message_t *info = NULL; osip_message_t *message = NULL; 
int call_id, dialog_id; 
int i,flag; 
int flag1 = 1; 
int id; 
string strIdentity = "sip:136@133.37.55.136"; 
string strRegisterer = "sip:133.37.55.136:5060"; // server ip 
string strSrcCall = "sip:136@133.37.55.136"; 
string strDestCall = "sip:136@133.37.55.136:5060"; // server ip 
char command; 
char tmp[4096]; 
char localip[128]; 
string strHelp = string("\n\t--> 命令字符 功能描述 <--\n\n") 
+ "\t\tr 向服务器注册\n" 
+ "\t\tc 取消注册\n" 
+ "\t\ti 发起呼叫请求\n" 
+ "\t\th 挂断\n" 
+ "\t\tq 退出程序\n" 
+ "\t\ts 执行方法 INFO\n" 
+ "\t\tm 执行方法 MESSAGE\n" 
+ "\t\te 帮助\n\n"; 
cout << strHelp; 
string strMsg; i = eXosip_init (); 
if (i != 0) 
{ 
cout << "\t--> Couldn't initialize eXosip! <--\n"; 
return -1; 
} 
else 
{ 
cout << "\t--> eXosip_init successfully! <-- \n\n"; 
} i = eXosip_listen_addr (IPPROTO_UDP, NULL, 5061, AF_INET, 0); 
if (i != 0) 
{ eXosip_quit (); 
cerr << "\n\t--> Couldn't initialize transport layer! <-- \n\n"; 
return -1; 
} flag = 1; 
while (flag) 
{ 
cout << "请输入一个命令字符:\t"; 
cin >> command; 
switch (command) 
{ 
case 'r': 
cout << "\n\t--> This modal isn't commpleted! \n" << endl; 
break; 
case 'i': // 初始化的 INVITE 请求 i = eXosip_call_build_initial_invite (&invite, strDestCall.c_str(), strSrcCall.c_str(), 
NULL, 
"This is a call for a conversation"); 
if (i != 0) 
{ 
cout << "\n --> Intial INVITE failed! <-- \n"; 
break; 
} 
// 符合 SDP 格式, 其中属性 a 是自定义格式,也就是说可以存放自己的信息,  
// 但是只能是两列,比如帐户信息 
// 但是经测试,格式: v o t必不可少,原因未知,估计是协议栈在传输时需要检查的 strMsg = string("v=0\r\n") 
+ "o=anonymous 0 0 IN IP4 0.0.0.0\r\n" 
+ "t=1 10\r\n" 
+ "a=username:bluesea\r\n" 
+ "a=password:123456\r\n"; osip_message_set_body (invite, strMsg.c_str(), strMsg.length()); osip_message_set_content_type (invite, "application/sdp"); 
// 这里使用了锁机制以保证同步 eXosip_lock (); i = eXosip_call_send_initial_invite (invite); eXosip_unlock (); flag1 = 1; 
while (flag1) 
{ je = eXosip_event_wait (0, 200); 
if (je == NULL) 
{ 
cout << "\n\t--> No response or the time is over! <--\n" << endl; 
break; 
} 
switch (je->type) 
{ 
case EXOSIP_CALL_INVITE: 
cout << "\n\t--> a new invite reveived! <--\n" << endl; 
break; 
// announce processing by a remote app 
case EXOSIP_CALL_PROCEEDING: 
cout << "\n\t--> proceeding! <--\n" << endl; 
break; 
// announce ringback 
case EXOSIP_CALL_RINGING: 
cout << "\n\t--> ringing! <--\n" 
<< "\n\tcall_id is " << je->cid 
<< ", dialog_id is " << je->did << endl; 
break; 
// 收到请求,表示连接成功,下面发送回复确认 
case EXOSIP_CALL_ANSWERED: 
cout << "\n\t--> ok! connected! <--\n" << endl; call_id = je->cid; dialog_id = je->did; 
cout << "\n\tcall_id is " << je->cid 
<< ", dialog_id is " << je->did << endl; eXosip_call_build_ack (je->did, &ack); eXosip_call_send_ack (je->did, ack); flag1 = 0; 
break; 
case EXOSIP_CALL_CLOSED: 
cout << "\n\t--> the other sid closed! <--\n" << endl; 
break; 
case EXOSIP_CALL_ACK: 
cout << "\n\t--> ACK received! <--\n" << endl; 
break; 
default: 
cout << "\n\t--> other response!\n" <<endl; 
break; 
} eXosip_event_free (je); 
} 
break; 
case 'h': 
cout << "\n\t--> Holded ! \n" << endl; eXosip_lock (); eXosip_call_terminate (call_id, dialog_id); eXosip_unlock (); 
break; 
case 'c': 
cout << "\n\t--> This modal isn't commpleted! \n" << endl; 
break; 
case 's': 
// 传输 INFO 方法 eXosip_call_build_info (dialog_id, &info); snprintf (tmp , 4096, "hello,bluesea"); osip_message_set_body (info, tmp, strlen(tmp)); 
// 格式可以任意设定, text/plain 代表文本信息 osip_message_set_content_type (info, "text/plain"); eXosip_call_send_request (dialog_id, info); 
break; 
case 'm': 
// 传输 MESSAGE方法,也就是即时消息, 
// 和 INFO 方法相比,主要区别,是 MESSAGE 不用建立连接,直接传输信息, 
// 而 INFO 必须在建立 INVITE 的基础上传输。 
cout << "\n\t--> the mothed :MESSAGE \n" << endl; eXosip_message_build_request (&message, 
"MESSAGE", strDestCall.c_str(), strSrcCall.c_str(), 
NULL); strMsg = "message: hello bluesea!"; osip_message_set_body (message, strMsg.c_str(), strMsg.length()); 
// 假设格式是xml osip_message_set_content_type (message, "text/xml"); eXosip_message_send_request (message); 
break; 
case 'q': eXosip_quit (); 
cout << "\n\t--> Exit the setup! \n" << endl;; flag = 0; 
break; 
case 'e': 
cout << strHelp << endl; 
break; 
default: 
cout << "\n\t--> 不支持的命令 <--\n" << endl; 
break; 
} 
} 
return 0; 
}




 ———– UAS 代理服务器端的代码整理 —————

/** 
* 一个使用了 osip 和 eXosip 库的 UAS 代理服务端的演示程序 
* 
* - 只是简单的演示了使用了 osip 和 eXosip2 库的 UAS 代理服务端的如下几个功能: 
* 
* 编 译:g++ -I/usr/local/include -L/usr/local/lib ua_server.cpp -o ua_server -leXosip2 -losip2 -losipparser2 -lpthread 
* 
*/ 
#include <eXosip2/eXosip.h> 
#include <netinet/in.h> 
#include <sys/socket.h> 
#include <sys/types.h> 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 
int main() 
{ eXosip_event_t *je = NULL; osip_message_t *ack = NULL; osip_message_t *invite = NULL; osip_message_t *answer = NULL; sdp_message_t *remote_sdp = NULL; 
int call_id, dialog_id; 
int i,j; 
int id; 
char *sour_call = "sip:136@133.37.55.136"; 
char *dest_call = "sip:136@133.37.55.136:5061"; //client ip/port 
char command; 
char tmp[4096]; 
char localip[128]; 
int pos = 0; 
// 初始化 sip i = eXosip_init (); 
if (i != 0) 
{ 
cerr << "\n\t--> Can't initialize eXosip!\n"; 
return -1; 
} 
else 
{ 
cout << "\n\t--> eXosip_init successfully!\n"; 
} i = eXosip_listen_addr (IPPROTO_UDP, NULL, 5060, AF_INET, 0); 
if (i != 0) 
{ eXosip_quit (); 
cerr << "\n\t--> eXosip_listen_addr error! Couldn't initialize transport layer!\n"; 
} 
for(;;) 
{ 
// 侦听是否有消息到来 je = eXosip_event_wait (0, 50); 
// 协议栈带有此语句,具体作用未知 eXosip_lock (); eXosip_default_action (je); eXosip_automatic_refresh (); eXosip_unlock (); 
if (je == NULL) // 没有接收到消息,继续 
{ 
continue; 
} 
switch (je->type) 
{ 
case EXOSIP_MESSAGE_NEW: // 新的消息到来 
cout << "\n\t*** EXOSIP_MESSAGE_NEW!\n" << endl; 
if (MSG_IS_MESSAGE (je->request)) // 如果接收到的消息类型是 MESSAGE 
{ 
{ osip_body_t *body; osip_message_get_body (je->request, 0, &body); 
cout << "I get the msg is: " << body->body << endl; 
} 
// 按照规则,需要回复 OK 信息 eXosip_message_build_answer (je->tid, 200, &answer); eXosip_message_send_answer (je->tid, 200, answer); 
} 
break; 
case EXOSIP_CALL_INVITE: // INVITE 请求消息 
// 得到接收到消息的具体信息 
cout << "\n\tReceived a INVITE msg from " << je->request->req_uri->host 
<< " : " << je->request->req_uri->port 
<< ", username is " << je->request->req_uri->username << endl; 
// 得到消息体,认为该消息就是 SDP 格式. remote_sdp = eXosip_get_remote_sdp (je->did); call_id = je->cid; dialog_id = je->did; eXosip_lock (); eXosip_call_send_answer (je->tid, 180, NULL); i = eXosip_call_build_answer (je->tid, 200, &answer); 
if (i != 0) 
{ 
cout << "\n\t--> This request msg is invalid! Cann't response!\n" << endl; eXosip_call_send_answer (je->tid, 400, NULL); 
} 
else 
{ snprintf (tmp, 4096, 
"v=0\r\n" 
"o=anonymous 0 0 IN IP4 0.0.0.0\r\n" 
"t=1 10\r\n" 
"a=username:rainfish\r\n" 
"a=password:123\r\n"); 
// 设置回复的SDP消息体,下一步计划分析消息体 
// 没有分析消息体,直接回复原来的消息,这一块做的不好。 osip_message_set_body (answer, tmp, strlen(tmp)); osip_message_set_content_type (answer, "application/sdp"); eXosip_call_send_answer (je->tid, 200, answer); 
cout << "\n\t--> send 200 over!" << endl; 
} eXosip_unlock (); 
// 显示出在 sdp 消息体中的 attribute 的内容,里面计划存放我们的信息 
cout << "\n\t--> The INFO is :\n" ; 
while (!osip_list_eol ( &(remote_sdp->a_attributes), pos)) 
{ sdp_attribute_t *at; 
//这里解释了为什么在SDP消息体中属性a里面存放必须是两列 at = (sdp_attribute_t *) osip_list_get ( &remote_sdp->a_attributes, pos); 
cout << "\n\t" << at->a_att_field 
<< " : " << at->a_att_value << endl; pos ++; 
} 
break; 
case EXOSIP_CALL_ACK: 
cout << "\n\t--> ACK recieved!\n" << endl; 
// printf ("the cid is %s, did is %s\n", je->did, je->cid);  
break; 
case EXOSIP_CALL_CLOSED: 
cout << "\n\t--> the remote hold the session!\n" << endl; 
// eXosip_call_build_ack(dialog_id, &ack); 
// eXosip_call_send_ack(dialog_id, ack);  i = eXosip_call_build_answer (je->tid, 200, &answer); 
if (i != 0) 
{ 
printf ("This request msg is invalid!Cann't response!\n"); eXosip_call_send_answer (je->tid, 400, NULL); 
} 
else 
{ eXosip_call_send_answer (je->tid, 200, answer); 
cout << "\n\t--> bye send 200 over!\n"; 
} 
break; 
case EXOSIP_CALL_MESSAGE_NEW: 
cout << "\n\t*** EXOSIP_CALL_MESSAGE_NEW\n" << endl; 
if (MSG_IS_INFO(je->request) ) // 如果传输的是 INFO 方法 
{ eXosip_lock (); i = eXosip_call_build_answer (je->tid, 200, &answer); 
if (i == 0) 
{ eXosip_call_send_answer (je->tid, 200, answer); 
} eXosip_unlock (); 
{ osip_body_t *body; osip_message_get_body (je->request, 0, &body); 
cout << "the body is " << body->body << endl; 
} 
} 
break; 
default: 
cout << "\n\t--> Could not parse the msg!\n" << endl; 
} 
} 
return 0; 
}



这篇关于一个基于 exosip 库的 UAC 和 UAS 的代码整理的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/697838

相关文章

利用Python调试串口的示例代码

《利用Python调试串口的示例代码》在嵌入式开发、物联网设备调试过程中,串口通信是最基础的调试手段本文将带你用Python+ttkbootstrap打造一款高颜值、多功能的串口调试助手,需要的可以了... 目录概述:为什么需要专业的串口调试工具项目架构设计1.1 技术栈选型1.2 关键类说明1.3 线程模

Python Transformers库(NLP处理库)案例代码讲解

《PythonTransformers库(NLP处理库)案例代码讲解》本文介绍transformers库的全面讲解,包含基础知识、高级用法、案例代码及学习路径,内容经过组织,适合不同阶段的学习者,对... 目录一、基础知识1. Transformers 库简介2. 安装与环境配置3. 快速上手示例二、核心模

Java的栈与队列实现代码解析

《Java的栈与队列实现代码解析》栈是常见的线性数据结构,栈的特点是以先进后出的形式,后进先出,先进后出,分为栈底和栈顶,栈应用于内存的分配,表达式求值,存储临时的数据和方法的调用等,本文给大家介绍J... 目录栈的概念(Stack)栈的实现代码队列(Queue)模拟实现队列(双链表实现)循环队列(循环数组

使用Java将DOCX文档解析为Markdown文档的代码实现

《使用Java将DOCX文档解析为Markdown文档的代码实现》在现代文档处理中,Markdown(MD)因其简洁的语法和良好的可读性,逐渐成为开发者、技术写作者和内容创作者的首选格式,然而,许多文... 目录引言1. 工具和库介绍2. 安装依赖库3. 使用Apache POI解析DOCX文档4. 将解析

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python实现全能手机虚拟键盘的示例代码

《使用Python实现全能手机虚拟键盘的示例代码》在数字化办公时代,你是否遇到过这样的场景:会议室投影电脑突然键盘失灵、躺在沙发上想远程控制书房电脑、或者需要给长辈远程协助操作?今天我要分享的Pyth... 目录一、项目概述:不止于键盘的远程控制方案1.1 创新价值1.2 技术栈全景二、需求实现步骤一、需求

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

jupyter代码块没有运行图标的解决方案

《jupyter代码块没有运行图标的解决方案》:本文主要介绍jupyter代码块没有运行图标的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录jupyter代码块没有运行图标的解决1.找到Jupyter notebook的系统配置文件2.这时候一般会搜索到

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La