• 基于eXosip2实现的客户端和服务端


    一、源码下载

    Index of /download/exosip2/,这里选择libeXosip2-3.5.0.tar.gz和libosip2-3.5.0.tar.gz

    二、osip2库编译

    1. cd libosip2-3.5.0/
    2. ./configure --enable-static
    3. make
    4. make install

    三、eXosip2库编译

     
    
    1. cd libeXosip2-3.5.0/
    2. ./configure --enable-static
    3. make
    4. make install

    四、eXosip2客户端

    1. #include <iostream>
    2. #include <string>
    3. #include <sstream>
    4. #include <osipparser2/osip_message.h>
    5. #include <osipparser2/osip_parser.h>
    6. #include <osipparser2/osip_port.h>
    7. #include <eXosip2/eXosip.h>
    8. #include <eXosip2/eX_setup.h>
    9. #include <eXosip2/eX_register.h>
    10. #include <eXosip2/eX_options.h>
    11. #include <eXosip2/eX_message.h>
    12. #include <arpa/inet.h>
    13. #include <sys/types.h>
    14. #include <sys/socket.h>
    15. using namespace std;
    16. #define LISTEN_ADDR ("192.168.50.57")
    17. #define UACPORT ("5061")
    18. #define UACPORTINT (5061)
    19. #define UACCODE ("100110000201000000")
    20. #define UACPWD ("12345")
    21. #define UAS_ADDR ("192.168.50.57")
    22. #define UAS_PORT ("5060")
    23. #define EXPIS 300
    24. static int iCurrentStatus;
    25. static int iHandle = -1;
    26. class CSipFromToHeader
    27. {
    28. public:
    29. CSipFromToHeader()
    30. {
    31. }
    32. ~CSipFromToHeader()
    33. {
    34. }
    35. void SetHeader(string addrCod, string addrI, string addrPor)
    36. {
    37. addrCode = addrCod;
    38. addrIp = addrI;
    39. addrPort = addrPor;
    40. }
    41. string GetFormatHeader()
    42. {
    43. std::stringstream stream;
    44. stream << "sip: " << addrCode << "@" << addrIp << ":" << addrPort;
    45. return stream.str();
    46. }
    47. string GetCode()
    48. {
    49. std::stringstream stream;
    50. stream << addrCode;
    51. return stream.str();
    52. }
    53. string GetAddr()
    54. {
    55. std::stringstream stream;
    56. stream << addrIp;
    57. return stream.str();
    58. }
    59. string GetPort()
    60. {
    61. std::stringstream stream;
    62. stream << addrPort;
    63. return stream.str();
    64. }
    65. private:
    66. string addrCode;
    67. string addrIp;
    68. string addrPort;
    69. };
    70. class CContractHeader: public CSipFromToHeader
    71. {
    72. public:
    73. CContractHeader()
    74. {
    75. }
    76. ~CContractHeader()
    77. {
    78. }
    79. void SetContractHeader(string addrCod, string addrI, string addrPor)
    80. {
    81. SetHeader(addrCod, addrI, addrPor);
    82. }
    83. string GetContractFormatHeader()
    84. {
    85. std::stringstream stream;
    86. stream << " << GetCode() << "@" << GetAddr() << ":" << GetPort()
    87. << ">";
    88. return stream.str();
    89. }
    90. };
    91. int SendRegister(int& registerId, CSipFromToHeader &from, CSipFromToHeader &to,
    92. CContractHeader &contact, const string& userName, const string& pwd,
    93. const int expires, int iType)
    94. {
    95. cout << "=============================================" << endl;
    96. if (iType == 0)
    97. {
    98. cout << "注册请求信息:" << endl;
    99. }
    100. else if (iType == 1)
    101. {
    102. cout << "刷新注册信息:" << endl;
    103. }
    104. else
    105. {
    106. cout << "注销信息:" << endl;
    107. }
    108. cout << "registerId " << registerId << endl;
    109. cout << "from " << from.GetFormatHeader() << endl;
    110. cout << "to " << to.GetFormatHeader() << endl;
    111. cout << "contact" << contact.GetContractFormatHeader() << endl;
    112. cout << "userName" << userName << endl;
    113. cout << "pwd" << pwd << endl;
    114. cout << "expires" << expires << endl;
    115. cout << "=============================================" << endl;
    116. static osip_message_t *regMsg = 0;
    117. int ret;
    118. ::eXosip_add_authentication_info(userName.c_str(), userName.c_str(),
    119. pwd.c_str(), "MD5", NULL);
    120. eXosip_lock();
    121. if (0 == registerId)
    122. {
    123. registerId = ::eXosip_register_build_initial_register(
    124. from.GetFormatHeader().c_str(), to.GetFormatHeader().c_str(),
    125. contact.GetContractFormatHeader().c_str(), expires, &regMsg);
    126. if (registerId <= 0)
    127. {
    128. return -1;
    129. }
    130. }
    131. else
    132. {
    133. ret = ::eXosip_register_build_register(registerId, expires, &regMsg);
    134. if (ret != OSIP_SUCCESS)
    135. {
    136. return ret;
    137. }
    138. if (expires == 0)
    139. {
    140. osip_contact_t *contact = NULL;
    141. char tmp[128];
    142. osip_message_get_contact(regMsg, 0, &contact);
    143. {
    144. sprintf(tmp, ";expires=0",
    145. contact->url->username, contact->url->host,
    146. contact->url->port);
    147. }
    148. //osip_contact_free(contact);
    149. //reset contact header
    150. osip_list_remove(&regMsg->contacts, 0);
    151. osip_message_set_contact(regMsg, tmp);
    152. osip_message_set_header(regMsg, "Logout-Reason", "logout");
    153. }
    154. }
    155. ret = ::eXosip_register_send_register(registerId, regMsg);
    156. if (ret != OSIP_SUCCESS)
    157. {
    158. registerId = 0;
    159. }eXosip_unlock();
    160. return ret;
    161. }
    162. //注册
    163. void Register()
    164. {
    165. if (iCurrentStatus == 1)
    166. {
    167. cout << "当前已经注册" << endl;
    168. return;
    169. }
    170. CSipFromToHeader stFrom;
    171. stFrom.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
    172. CSipFromToHeader stTo;
    173. stTo.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
    174. CContractHeader stContract;
    175. stContract.SetContractHeader(UACCODE, LISTEN_ADDR, UACPORT);
    176. //发送注册信息
    177. int registerId = 0;
    178. if (0 > SendRegister(registerId, stFrom, stTo, stContract, UACCODE, UACPWD,
    179. 3000, 0))
    180. {
    181. cout << "发送注册失败" << endl;
    182. return;
    183. }
    184. iCurrentStatus = 1;
    185. iHandle = registerId;
    186. }
    187. //刷新注册
    188. void RefreshRegister()
    189. {
    190. if (iCurrentStatus == 0)
    191. {
    192. cout << "当前未注册,不允许刷新" << endl;
    193. return;
    194. }
    195. CSipFromToHeader stFrom;
    196. stFrom.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
    197. CSipFromToHeader stTo;
    198. stTo.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
    199. CContractHeader stContract;
    200. stContract.SetContractHeader(UACCODE, LISTEN_ADDR, UACPORT);
    201. //发送注册信息
    202. if (0 > SendRegister(iHandle, stFrom, stTo, stContract, UACCODE, UACPWD,
    203. 3000, 1))
    204. {
    205. cout << "发送刷新注册失败" << endl;
    206. return;
    207. }
    208. }
    209. //注销
    210. void UnRegister()
    211. {
    212. if (iCurrentStatus == 0)
    213. {
    214. cout << "当前未注册,不允许注销" << endl;
    215. return;
    216. }
    217. CSipFromToHeader stFrom;
    218. stFrom.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
    219. CSipFromToHeader stTo;
    220. stTo.SetHeader(UACCODE, UAS_ADDR, UAS_PORT);
    221. CContractHeader stContract;
    222. stContract.SetContractHeader(UACCODE, LISTEN_ADDR, UACPORT);
    223. //发送注册信息
    224. 269 if (0 > SendRegister( iHandle, stFrom, stTo, stContract, UACCODE, UACPWD,
    225. 0, 2))
    226. {
    227. cout << "发送注销失败" << endl;
    228. return;
    229. }
    230. iCurrentStatus = 0;
    231. iHandle = -1;
    232. }
    233. static void help()
    234. {
    235. const char
    236. *b =
    237. "-------------------------------------------------------------------------------\n"
    238. "SIP Library test process - uac v 1.0 (June 13, 2014)\n\n"
    239. "SIP UAC端 注册,刷新注册,注销实现\n\n"
    240. "Author: 程序人生\n\n"
    241. "博客地址:http://blog.csdn.net/hiwubihe QQ:1269122125\n\n"
    242. "-------------------------------------------------------------------------------\n"
    243. "\n"
    244. " 0:Register\n"
    245. " 1:RefreshRegister\n"
    246. " 2:UnRegister\n"
    247. " 3:clear scream\n"
    248. " 4:exit\n"
    249. "-------------------------------------------------------------------------------\n"
    250. "\n";
    251. fprintf(stderr, b, strlen(b));
    252. cout << "please select method :";
    253. }
    254. //服务处理线程
    255. void *serverHandle(void *pUser)
    256. {
    257. sleep(3);
    258. help();
    259. char ch = getchar();
    260. getchar();
    261. while (1)
    262. {
    263. switch (ch)
    264. {
    265. case '0':
    266. //注册
    267. Register();
    268. break;
    269. case '1':
    270. //刷新注册
    271. RefreshRegister();
    272. break;
    273. case '2':
    274. //注销
    275. UnRegister();
    276. break;
    277. case '3':
    278. if (system("clear") < 0)
    279. {
    280. cout << "clear scream error" << endl;
    281. exit(1);
    282. }
    283. break;
    284. case '4':
    285. cout << "exit sipserver......" << endl;
    286. getchar();
    287. exit(0);
    288. default:
    289. cout << "select error" << endl;
    290. break;
    291. }
    292. cout << "press any key to continue......" << endl;
    293. getchar();
    294. help();
    295. ch = getchar();
    296. getchar();
    297. }
    298. return NULL;
    299. }
    300. //事件处理线程
    301. void *eventHandle(void *pUser)
    302. {
    303. eXosip_event_t* osipEventPtr = (eXosip_event_t*) pUser;
    304. switch (osipEventPtr->type)
    305. {
    306. //需要继续验证REGISTER是什么类型
    307. case EXOSIP_REGISTRATION_SUCCESS:
    308. case EXOSIP_REGISTRATION_FAILURE:
    309. {
    310. cout<<"收到状态码:"<<osipEventPtr->response->status_code<<"报文"<<endl;
    311. if(osipEventPtr->response->status_code == 401)
    312. {
    313. cout<<"发送鉴权报文"<<endl;
    314. }
    315. else if(osipEventPtr->response->status_code == 200)
    316. {
    317. cout<<"接收成功"<<endl;
    318. }
    319. else
    320. {}
    321. }
    322. break;
    323. default:
    324. cout << "The sip event type that not be precessed.the event "
    325. "type is : " << osipEventPtr->type << endl;
    326. break;
    327. }
    328. eXosip_event_free(osipEventPtr);
    329. return NULL;
    330. }
    331. int main()
    332. {
    333. iCurrentStatus = 0;
    334. //库处理结果
    335. int result = OSIP_SUCCESS;
    336. //初始化库
    337. if (OSIP_SUCCESS != (result = eXosip_init()))
    338. {
    339. printf("eXosip_init failure.\n");
    340. return 1;
    341. }
    342. cout << "eXosip_init success." << endl;
    343. eXosip_set_user_agent(NULL);
    344. //监听
    345. if (OSIP_SUCCESS != eXosip_listen_addr(IPPROTO_UDP, NULL, UACPORTINT,
    346. AF_INET, 0))
    347. {
    348. printf("eXosip_listen_addr failure.\n");
    349. return 1;
    350. }
    351. //设置监听网卡
    352. if (OSIP_SUCCESS != eXosip_set_option(
    353. EXOSIP_OPT_SET_IPV4_FOR_GATEWAY,
    354. LISTEN_ADDR))
    355. {
    356. return -1;
    357. }
    358. //开启服务线程
    359. pthread_t pthser;
    360. if (0 != pthread_create(&pthser, NULL, serverHandle, NULL))
    361. {
    362. printf("创建主服务失败\n");
    363. return -1;
    364. }
    365. //事件用于等待
    366. eXosip_event_t* osipEventPtr = NULL;
    367. //开启事件循环
    368. while (true)
    369. {
    370. //等待事件 0的单位是秒,500是毫秒
    371. osipEventPtr = ::eXosip_event_wait(0, 200);
    372. //处理eXosip库默认处理
    373. {
    374. usleep(500 * 1000);
    375. eXosip_lock();
    376. //一般处理401/407采用库默认处理
    377. eXosip_default_action(osipEventPtr);
    378. eXosip_unlock();
    379. }
    380. //事件空继续等待
    381. if (NULL == osipEventPtr)
    382. {
    383. continue;
    384. }
    385. //开启线程处理事件并在事件处理完毕将事件指针释放
    386. pthread_t pth;
    387. if (0 != pthread_create(&pth, NULL, eventHandle, (void*) osipEventPtr))
    388. {
    389. printf("创建线程处理事件失败\n");
    390. continue;
    391. }
    392. osipEventPtr = NULL;
    393. }
    394. }

      g++  test_cli.cpp -o test_cli -I/usr/local/include -L/usr/local/lib -losipparser2 -losip2 -lpthread -leXosip2

    四、eXosip2服务端 

    1. #include <string.h>
    2. #include <iostream>
    3. #include <string>
    4. #include <sstream>
    5. #include <osipparser2/osip_message.h>
    6. #include <osipparser2/osip_parser.h>
    7. #include <osipparser2/osip_port.h>
    8. #include <eXosip2/eXosip.h>
    9. #include <eXosip2/eX_setup.h>
    10. #include <eXosip2/eX_register.h>
    11. #include <eXosip2/eX_options.h>
    12. #include <eXosip2/eX_message.h>
    13. #include <arpa/inet.h>
    14. #include <sys/types.h>
    15. #include <sys/socket.h>
    16. using namespace std;
    17. #define LISTEN_ADDR ("192.168.50.57")
    18. #define UASPORT (5060)
    19. //该系数是由UAS维护的,UAS在接收到UAC的未鉴权报文后,给UAC回复401,在该报文中必须要带相关认证系数和认证方法
    20. //UAS赋值的认证随机数
    21. #define NONCE "9bd055"
    22. //UAS默认加密算法
    23. #define ALGORITHTHM "MD5"
    24. eXosip_t *m_exosip_t;
    25. //SIP From头部
    26. class CSipFromHeader
    27. {
    28. public:
    29. CSipFromHeader()
    30. {
    31. }
    32. ~CSipFromHeader()
    33. {
    34. }
    35. void SetHeader(string addrCod, string addrI, string addrPor)
    36. {
    37. addrCode = addrCod;
    38. addrIp = addrI;
    39. addrPort = addrPor;
    40. }
    41. string GetFormatHeader()
    42. {
    43. std::stringstream stream;
    44. stream << " << addrCode << "@" << addrIp << ":" << addrPort
    45. << ">";
    46. return stream.str();
    47. }
    48. //主机名称
    49. string GetRealName()
    50. {
    51. std::stringstream stream;
    52. stream << addrIp;
    53. return stream.str();
    54. }
    55. private:
    56. string addrCode;
    57. string addrIp;
    58. string addrPort;
    59. };
    60. //SIP Contract头部
    61. class CContractHeader: public CSipFromHeader
    62. {
    63. public:
    64. CContractHeader()
    65. {
    66. }
    67. ~CContractHeader()
    68. {
    69. }
    70. void SetContractHeader(string addrCod, string addrI, string addrPor,
    71. int expire)
    72. {
    73. SetHeader(addrCod, addrI, addrPor);
    74. expires = expire;
    75. }
    76. string GetContractFormatHeader(bool bExpires)
    77. {
    78. if (!bExpires)
    79. {
    80. return GetFormatHeader();
    81. }
    82. else
    83. {
    84. string sTmp = GetFormatHeader();
    85. std::stringstream stream;
    86. stream << ";" << "expires=" << expires;
    87. sTmp += stream.str();
    88. return sTmp;
    89. }
    90. }
    91. private:
    92. int expires;
    93. };
    94. struct SipContextInfo
    95. {
    96. //Sip层返回的请求的标志 响应时返回即可
    97. int sipRequestId;
    98. //维护一次注册
    99. string callId;
    100. //消息所属的功能方法名字符串
    101. string method;
    102. //地址编码@域名或IP地址:连接端口,例如sip:1111@127.0.0.1:5060
    103. CSipFromHeader from;
    104. //地址编码@域名或IP地址:连接端口,例如sip:1111@127.0.0.1:5060
    105. CSipFromHeader proxy;
    106. //地址编码@域名或IP地址:连接端口,例如sip:1111@127.0.0.1:5060
    107. CContractHeader contact;
    108. //消息内容,一般为DDCP消息体XML文档,或者具体协议帧要求的其他字符串文本
    109. string content;
    110. //响应状态信息
    111. string status;
    112. //超时,时间单位为秒
    113. int expires;
    114. };
    115. struct SipAuthInfo
    116. {
    117. //平台主机名
    118. string digestRealm;
    119. //平台提供的随机数
    120. string nonce;
    121. //用户名
    122. string userName;
    123. //密码
    124. string response;
    125. //“sip:平台地址”,不需要uac赋值
    126. string uri;
    127. //加密算法MD5
    128. string algorithm;
    129. };
    130. struct sipRegisterInfo
    131. {
    132. SipContextInfo baseInfo;
    133. SipAuthInfo authInfo;
    134. bool isAuthNull;
    135. };
    136. void parserRegisterInfo(osip_message_t*request, int iReqId,
    137. sipRegisterInfo &regInfo)
    138. {
    139. std::stringstream stream;
    140. regInfo.baseInfo.method = request->sip_method;
    141. regInfo.baseInfo.from.SetHeader(request->from->url->username,
    142. request->from->url->host, request->from->url->port);
    143. regInfo.baseInfo.proxy.SetHeader(request->to->url->username,
    144. request->to->url->host, request->to->url->port);
    145. //获取expires
    146. osip_header_t* header = NULL;
    147. {
    148. osip_message_header_get_byname(request, "expires",
    149. 0, &header);
    150. if (NULL != header && NULL != header->hvalue)
    151. {
    152. regInfo.baseInfo.expires = atoi(header->hvalue);
    153. }
    154. }
    155. //contact字段
    156. osip_contact_t* contact = NULL;
    157. osip_message_get_contact(request, 0, &contact);
    158. if (NULL != contact)
    159. {
    160. regInfo.baseInfo.contact.SetContractHeader(contact->url->username,
    161. contact->url->host, contact->url->port,
    162. regInfo.baseInfo.expires);
    163. }
    164. //注册返回 由发送方维护的请求ID 接收方接收后原样返回即可
    165. regInfo.baseInfo.sipRequestId = iReqId;
    166. //CALL_ID
    167. {
    168. stream.str("");
    169. stream << request->call_id->number;
    170. regInfo.baseInfo.callId = stream.str();
    171. }
    172. //解析content消息
    173. osip_body_t * body = NULL;
    174. osip_message_get_body(request, 0, &body);
    175. if (body != NULL)
    176. {
    177. stream.str("");
    178. stream << body->body;
    179. regInfo.baseInfo.content = stream.str();
    180. }
    181. //鉴权信息
    182. osip_authorization_t* authentication = NULL;
    183. {
    184. osip_message_get_authorization(request, 0, &authentication);
    185. if (NULL == authentication)
    186. {
    187. regInfo.isAuthNull = true;
    188. }
    189. else
    190. {
    191. regInfo.isAuthNull = false;
    192. stream.str("");
    193. stream << authentication->username;
    194. regInfo.authInfo.userName = stream.str();
    195. stream.str("");
    196. stream << authentication->algorithm;
    197. regInfo.authInfo.algorithm = stream.str();
    198. stream.str("");
    199. stream << authentication->realm;
    200. regInfo.authInfo.digestRealm = stream.str();
    201. stream.str("");
    202. stream << authentication->nonce;
    203. regInfo.authInfo.nonce = stream.str();
    204. stream.str("");
    205. stream << authentication->response;
    206. regInfo.authInfo.response = stream.str();
    207. stream.str("");
    208. stream << authentication->uri;
    209. regInfo.authInfo.uri = stream.str();
    210. }
    211. }
    212. authentication = NULL;
    213. }
    214. //打印接收到的响应报文
    215. void printRegisterPkt( sipRegisterInfo&info)
    216. {
    217. cout<<"接收到报文:"<<endl;
    218. cout<<"=============================================="
    219. "=================="<<endl;
    220. cout << "method:" << info.baseInfo.method << endl;
    221. cout << "from: " << info.baseInfo.from.GetFormatHeader() << endl;
    222. cout << "to:" << info.baseInfo.proxy.GetFormatHeader() << endl;
    223. cout << "contact:" << info.baseInfo.contact.GetContractFormatHeader(false)
    224. << endl;
    225. //注册返回 由发送方维护的请求ID 接收方接收后原样返回即可
    226. cout << "sipRequestId:" << info.baseInfo.sipRequestId << endl;
    227. //CALL_ID
    228. cout << "CallId:" << info.baseInfo.callId << endl;
    229. //解析content消息
    230. cout << "body:" << info.baseInfo.content << endl;
    231. //获取expires
    232. cout << "expires:" << info.baseInfo.expires << endl;
    233. //鉴权信息
    234. if (info.isAuthNull)
    235. {
    236. cout << "当前报文未提供鉴权信息!!!" << endl;
    237. }
    238. else
    239. {
    240. cout << "当前报文鉴权信息如下:" << endl;
    241. cout << "username:" << info.authInfo.userName << endl;
    242. cout << "algorithm:" << info.authInfo.algorithm << endl;
    243. cout << "Realm:" << info.authInfo.digestRealm << endl;
    244. cout << "nonce:" << info.authInfo.nonce << endl;
    245. cout << "response:" << info.authInfo.response << endl;
    246. cout << "uri:" << info.authInfo.uri << endl;
    247. }
    248. cout<<"=================================================="
    249. "=============="<<endl;
    250. return;
    251. }
    252. void sendRegisterAnswer( sipRegisterInfo&info)
    253. {
    254. osip_message_t* answer = NULL;
    255. int iStatus;
    256. if (info.isAuthNull)
    257. {
    258. iStatus = 401;
    259. }
    260. else
    261. {
    262. iStatus = 200;
    263. }
    264. eXosip_lock(m_exosip_t);
    265. {
    266. int result = ::eXosip_message_build_answer(m_exosip_t, info.baseInfo.sipRequestId,
    267. iStatus, &answer);
    268. if (iStatus == 401)
    269. {
    270. //由SIP库生成认证方法和认证参数发送客户端
    271. std::stringstream stream;
    272. string nonce=NONCE;
    273. string algorithm=ALGORITHTHM;
    274. stream << "Digest realm=\"" << info.baseInfo.from.GetRealName()
    275. << "\",nonce=\"" << nonce
    276. << "\",algorithm=" << algorithm;
    277. osip_message_set_header(answer, "WWW-Authenticate",
    278. stream.str().c_str());
    279. cout<<"======================================================="
    280. "========="<<endl;
    281. cout<<"发送401报文"<<endl;
    282. cout<<"========================================================"
    283. "========"<<endl;
    284. }
    285. else if (iStatus == 200)
    286. {
    287. osip_message_set_header(answer, "Contact",
    288. info.baseInfo.contact.GetContractFormatHeader(true).c_str());
    289. cout<<"========================================================="
    290. "======="<<endl;
    291. cout<<"发送200报文"<<endl;
    292. cout<<"=========================================================="
    293. "======"<<endl;
    294. //string_t b = ";expires=600";
    295. //osip_message_set_header(answer, "Contact", b.c_str());
    296. }
    297. else
    298. {
    299. //Do nothing
    300. }
    301. if (OSIP_SUCCESS != result)
    302. {
    303. ::eXosip_message_send_answer(m_exosip_t, info.baseInfo.sipRequestId, 400, NULL);
    304. }
    305. else
    306. {
    307. //发送消息体
    308. ::eXosip_message_send_answer(m_exosip_t, info.baseInfo.sipRequestId, iStatus,
    309. answer);
    310. }
    311. if (0 == info.baseInfo.expires)
    312. {
    313. eXosip_register_remove(m_exosip_t, info.baseInfo.sipRequestId);
    314. }
    315. }
    316. eXosip_unlock(m_exosip_t);
    317. }
    318. void OnRegister(eXosip_event_t *osipEvent)
    319. {
    320. sipRegisterInfo regInfo;
    321. parserRegisterInfo(osipEvent->request, osipEvent->tid, regInfo);
    322. //打印报文
    323. printRegisterPkt(regInfo);
    324. //发送应答报文
    325. sendRegisterAnswer(regInfo);
    326. }
    327. int main()
    328. {
    329. int result = OSIP_SUCCESS;
    330. m_exosip_t = eXosip_malloc();
    331. // init exosip.
    332. if (OSIP_SUCCESS != (result = eXosip_init(m_exosip_t)))
    333. {
    334. printf("eXosip_init failure.\n");
    335. return 1;
    336. }
    337. cout << "eXosip_init success." << endl;
    338. //
    339. // if (null_ptr != this->receiveSipMessageCallback || null_ptr
    340. // != this->sendSipMessageCallback)
    341. // {
    342. // if (OSIP_SUCCESS != (result = ::eXosip_set_cbsip_message(
    343. // &Sip::MessageCallback)))
    344. // {
    345. // return;
    346. // }
    347. // }
    348. eXosip_set_user_agent(m_exosip_t,NULL);
    349. if (OSIP_SUCCESS != eXosip_listen_addr(m_exosip_t, IPPROTO_UDP, NULL, UASPORT, AF_INET,
    350. 0))
    351. {
    352. printf("eXosip_listen_addr failure.\n");
    353. return 1;
    354. }
    355. if (OSIP_SUCCESS != eXosip_set_option(m_exosip_t,
    356. EXOSIP_OPT_SET_IPV4_FOR_GATEWAY,
    357. LISTEN_ADDR))
    358. {
    359. return -1;
    360. }
    361. //开启循环消息,实际应用中可以开启多线程同时接收信号
    362. eXosip_event_t* osipEventPtr = NULL;
    363. while (true)
    364. {
    365. // Wait the osip event.
    366. osipEventPtr = ::eXosip_event_wait(m_exosip_t, 0, 200);// 0的单位是秒,500是毫秒
    367. // If get nothing osip event,then continue the loop.
    368. if (NULL == osipEventPtr)
    369. {
    370. continue;
    371. }
    372. // 事件处理
    373. switch (osipEventPtr->type)
    374. {
    375. //需要继续验证REGISTER是什么类型
    376. //case EXOSIP_REGISTRATION_NEW:
    377. //OnRegister(osipEventPtr);
    378. //break;
    379. case EXOSIP_MESSAGE_NEW:
    380. {
    381. if (!strncmp(osipEventPtr->request->sip_method, "REGISTER",
    382. strlen("REGISTER")))
    383. {
    384. OnRegister(osipEventPtr);
    385. }
    386. else if (!strncmp(osipEventPtr->request->sip_method, "MESSAGE",
    387. strlen("MESSAGE")))
    388. {
    389. //
    390. }
    391. }
    392. break;
    393. default:
    394. cout
    395. << "The sip event type that not be precessed.the event "
    396. "type is : "
    397. << osipEventPtr->type;
    398. break;
    399. }
    400. //ProcessSipEvent(this->osipEventPtrParam);
    401. eXosip_event_free(osipEventPtr);
    402. osipEventPtr = NULL;
    403. }
    404. return 0;
    405. }

    g++ test_srv.cpp -o test_srv -I/usr/local/include -L/usr/local/lib -losipparser2 -losip2 -lpthread -leXosip2 

  • 相关阅读:
    蓝牙资讯|Q2中国蓝牙耳机市场发布,搭载苹果Find My的蓝牙耳机正逐步推出
    VBA 输出到CMD控制台显示暨更新当前行显示
    Java集合类ArrayList的应用-杨辉三角的前n行
    Node.js实现WebSocket
    【Linux-Windows】千兆网口以及千兆网线
    【Electron将HTML项目打包成桌面应用exe文件】
    有汇源上下界最大流和最小流
    H5页面获取用户位置信息方案及测试流程
    PTL电子标签助力仓储行业转型升级提升拣货效率降低误差率
    2023-09-14力扣每日一题
  • 原文地址:https://blog.csdn.net/thehunters/article/details/133297399