communication_socket_base.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. #include "communication_socket_base.h"
  2. #include "../tool/proto_tool.h"
  3. Communication_socket_base::Communication_socket_base()
  4. {
  5. m_communication_statu = COMMUNICATION_UNKNOW;
  6. mp_receive_data_thread = NULL;
  7. mp_analysis_data_thread = NULL;
  8. mp_send_data_thread = NULL;
  9. mp_encapsulate_data_thread = NULL;
  10. }
  11. Communication_socket_base::~Communication_socket_base()
  12. {
  13. communication_uninit();
  14. }
  15. //初始化 通信 模块。如下三选一
  16. Error_manager Communication_socket_base::communication_init()
  17. {
  18. LOG(INFO) << " ---Communication_socket_base::communication_init() run--- "<< this;
  19. return communication_init_from_protobuf(COMMUNICATION_PARAMETER_PATH);
  20. }
  21. //初始化 通信 模块。从文件读取
  22. Error_manager Communication_socket_base::communication_init_from_protobuf(std::string prototxt_path)
  23. {
  24. Communication_proto::Communication_parameter_all t_communication_parameter_all;
  25. if(! proto_tool::read_proto_param(prototxt_path,t_communication_parameter_all) )
  26. {
  27. return Error_manager(COMMUNICATION_READ_PROTOBUF_ERROR,MINOR_ERROR,
  28. "Communication_socket_base read_proto_param failed");
  29. }
  30. return communication_init_from_protobuf(t_communication_parameter_all);
  31. }
  32. //初始化 通信 模块。从protobuf读取
  33. Error_manager Communication_socket_base::communication_init_from_protobuf(Communication_proto::Communication_parameter_all& communication_parameter_all)
  34. {
  35. LOG(INFO) << " ---Communication_socket_base::communication_init_from_protobuf() run--- "<< this;
  36. Error_manager t_error;
  37. if ( communication_parameter_all.communication_parameters().has_bind_string() )
  38. {
  39. t_error = communication_bind(communication_parameter_all.communication_parameters().bind_string());
  40. if ( t_error != Error_code::SUCCESS )
  41. {
  42. return t_error;
  43. }
  44. }
  45. std::cout << "communication_parameter_all.communication_parameters().connect_string_vector_size() " <<
  46. communication_parameter_all.communication_parameters().connect_string_vector_size()<< std::endl;
  47. for(int i=0;i<communication_parameter_all.communication_parameters().connect_string_vector_size();++i)
  48. {
  49. t_error = communication_connect( communication_parameter_all.communication_parameters().connect_string_vector(i) );
  50. if ( t_error != Error_code::SUCCESS )
  51. {
  52. return t_error;
  53. }
  54. }
  55. //启动通信, run thread
  56. communication_run();
  57. return Error_code::SUCCESS;
  58. }
  59. //初始化
  60. Error_manager Communication_socket_base::communication_init(std::string bind_string, std::vector<std::string>& connect_string_vector)
  61. {
  62. LOG(INFO) << " ---Communication_socket_base::communication_init() run--- "<< this;
  63. Error_manager t_error;
  64. t_error = communication_bind(bind_string);
  65. if ( t_error != Error_code::SUCCESS )
  66. {
  67. return t_error;
  68. }
  69. t_error = communication_connect(connect_string_vector);
  70. if ( t_error != Error_code::SUCCESS )
  71. {
  72. return t_error;
  73. }
  74. //启动通信, run thread
  75. communication_run();
  76. return Error_code::SUCCESS;
  77. }
  78. //bind
  79. Error_manager Communication_socket_base::communication_bind(std::string bind_string)
  80. {
  81. Error_manager t_error;
  82. int t_socket_result;
  83. //m_socket 自己作为一个服务器, 绑定一个端口
  84. t_socket_result = m_socket.bind(bind_string);
  85. if ( t_socket_result <0 )
  86. {
  87. return Error_manager(Error_code::COMMUNICATION_BIND_ERROR, Error_level::MINOR_ERROR,
  88. " m_socket.bind error ");
  89. }
  90. LOG(INFO) << " ---Communication_socket_base::communication_bind() bind:: "<< bind_string << " " << this;
  91. return Error_code::SUCCESS;
  92. }
  93. //connect
  94. Error_manager Communication_socket_base::communication_connect(std::vector<std::string>& connect_string_vector)
  95. {
  96. Error_manager t_error;
  97. for (auto iter = connect_string_vector.begin(); iter != connect_string_vector.end(); ++iter)
  98. {
  99. t_error = communication_connect(*iter);
  100. if ( t_error != Error_code::SUCCESS )
  101. {
  102. return t_error;
  103. }
  104. }
  105. return Error_code::SUCCESS;
  106. }
  107. //connect
  108. Error_manager Communication_socket_base::communication_connect(std::string connect_string)
  109. {
  110. Error_manager t_error;
  111. int t_socket_result;
  112. //m_socket 和远端通信, 连接远端服务器的端口
  113. t_socket_result = m_socket.connect(connect_string);
  114. if ( t_socket_result <0 )
  115. {
  116. return Error_manager(Error_code::COMMUNICATION_CONNECT_ERROR, Error_level::MINOR_ERROR,
  117. " m_socket.connect error ");
  118. }
  119. LOG(INFO) << " ---Communication_socket_base::communication_connect() connect:: "<< connect_string << " " << this;
  120. return Error_code::SUCCESS;
  121. }
  122. //启动通信, run thread
  123. Error_manager Communication_socket_base::communication_run()
  124. {
  125. m_communication_statu = COMMUNICATION_READY;
  126. //启动4个线程。
  127. //接受线程默认循环, 内部的nn_recv进行等待, 超时1ms
  128. m_receive_condition.reset(false, true, false);
  129. mp_receive_data_thread = new std::thread(&Communication_socket_base::receive_data_thread, this);
  130. //解析线程默认等待, 需要接受线程去唤醒, 超时1ms, 超时后主动遍历m_receive_data_list
  131. m_analysis_data_condition.reset(false, false, false);
  132. mp_analysis_data_thread = new std::thread(&Communication_socket_base::analysis_data_thread, this);
  133. //发送线程默认循环, 内部的wait_and_pop进行等待,
  134. m_send_data_condition.reset(false, true, false);
  135. mp_send_data_thread = new std::thread(&Communication_socket_base::send_data_thread, this);
  136. //封装线程默认等待, ...., 超时1ms, 超时后主动 封装心跳和状态信息,
  137. m_encapsulate_data_condition.reset(false, false, false);
  138. mp_encapsulate_data_thread = new std::thread(&Communication_socket_base::encapsulate_data_thread, this);
  139. return Error_code::SUCCESS;
  140. }
  141. //反初始化 通信 模块。
  142. Error_manager Communication_socket_base::communication_uninit()
  143. {
  144. //终止list,防止 wait_and_pop 阻塞线程。
  145. m_receive_data_list.termination_list();
  146. m_send_data_list.termination_list();
  147. //杀死4个线程,强制退出
  148. if (mp_receive_data_thread)
  149. {
  150. m_receive_condition.kill_all();
  151. }
  152. if (mp_analysis_data_thread)
  153. {
  154. m_analysis_data_condition.kill_all();
  155. }
  156. if (mp_send_data_thread)
  157. {
  158. m_send_data_condition.kill_all();
  159. }
  160. if (mp_encapsulate_data_thread)
  161. {
  162. m_encapsulate_data_condition.kill_all();
  163. }
  164. //回收4个线程的资源
  165. if (mp_receive_data_thread)
  166. {
  167. mp_receive_data_thread->join();
  168. delete mp_receive_data_thread;
  169. mp_receive_data_thread = NULL;
  170. }
  171. if (mp_analysis_data_thread)
  172. {
  173. mp_analysis_data_thread->join();
  174. delete mp_analysis_data_thread;
  175. mp_analysis_data_thread = 0;
  176. }
  177. if (mp_send_data_thread)
  178. {
  179. mp_send_data_thread->join();
  180. delete mp_send_data_thread;
  181. mp_send_data_thread = NULL;
  182. }
  183. if (mp_encapsulate_data_thread)
  184. {
  185. mp_encapsulate_data_thread->join();
  186. delete mp_encapsulate_data_thread;
  187. mp_encapsulate_data_thread = NULL;
  188. }
  189. //清空list
  190. m_receive_data_list.clear_and_delete();
  191. m_send_data_list.clear_and_delete();
  192. m_communication_statu = COMMUNICATION_UNKNOW;
  193. m_socket.close();
  194. return Error_code::SUCCESS;
  195. }
  196. //mp_receive_data_thread 接受线程执行函数,
  197. //receive_data_thread 内部线程负责接受消息
  198. void Communication_socket_base::receive_data_thread()
  199. {
  200. LOG(INFO) << " Communication_socket_base::receive_data_thread start "<< this;
  201. //通信接受线程, 负责接受socket消息, 并存入 m_receive_data_list
  202. while (m_receive_condition.is_alive())
  203. {
  204. m_receive_condition.wait();
  205. if ( m_receive_condition.is_alive() )
  206. {
  207. std::this_thread::yield();
  208. std::unique_lock<std::mutex> lk(m_mutex);
  209. //flags为1, 非阻塞接受消息, 如果接收到消息, 那么接受数据长度大于0
  210. std::string t_receive_string = m_socket.recv<std::string>(1);
  211. if ( t_receive_string.size()>0 )
  212. {
  213. //如果这里接受到了消息, 在这提前解析消息最前面的Base_msg (消息公共内容), 用于后续的check
  214. message::Base_msg t_base_msg;
  215. if( t_base_msg.ParseFromString(t_receive_string) )
  216. {
  217. //第一次解析之后转化为, Communication_message, 自定义的通信消息格式
  218. Communication_message * tp_communication_message = new Communication_message;
  219. tp_communication_message->reset(t_base_msg, t_receive_string);
  220. //检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
  221. if ( check_msg(tp_communication_message) == SUCCESS )
  222. {
  223. bool is_push = m_receive_data_list.push(tp_communication_message);
  224. //push成功之后, tp_communication_message内存的管理权限交给链表, 如果失败就要回收内存
  225. if ( is_push )
  226. {
  227. //唤醒解析线程一次,
  228. m_analysis_data_condition.notify_all(false, true);
  229. }
  230. else
  231. {
  232. // push失败, 就要回收内存
  233. delete(tp_communication_message);
  234. tp_communication_message = NULL;
  235. // return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  236. // " m_receive_data_list.push error ");
  237. }
  238. }
  239. else
  240. {
  241. delete(tp_communication_message);
  242. tp_communication_message = NULL;
  243. }
  244. }
  245. //解析失败, 就当做什么也没发生, 认为接收消息无效,
  246. }
  247. //没有接受到消息, 返回空字符串
  248. }
  249. }
  250. LOG(INFO) << " Communication_socket_base::receive_data_thread end "<< this;
  251. return;
  252. }
  253. //检查消息是否有效, 主要检查消息类型和接受者, 判断这条消息是不是给我的.
  254. Error_manager Communication_socket_base::check_msg(Communication_message* p_msg)
  255. {
  256. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 判断这条消息是不是给我的.
  257. //子类重载时, 增加自己模块的判断逻辑, 以后再写.
  258. if ( p_msg->get_message_type() == Communication_message::Message_type::eLocate_request_msg
  259. && p_msg->get_receiver() == Communication_message::Communicator::eMeasurer )
  260. {
  261. return Error_code::SUCCESS;
  262. }
  263. else
  264. {
  265. //认为接受人
  266. return Error_code::INVALID_MESSAGE;
  267. }
  268. }
  269. //mp_analysis_data_thread 解析线程执行函数,
  270. //analysis_data_thread 内部线程负责解析消息
  271. void Communication_socket_base::analysis_data_thread()
  272. {
  273. LOG(INFO) << " Communication_socket_base::analysis_data_thread start "<< this;
  274. //通信解析线程, 负责巡检m_receive_data_list, 并解析和处理消息
  275. while (m_analysis_data_condition.is_alive())
  276. {
  277. bool t_pass_flag = m_analysis_data_condition.wait_for_millisecond(1000);
  278. if ( m_analysis_data_condition.is_alive() )
  279. {
  280. std::this_thread::yield();
  281. //如果解析线程被主动唤醒, 那么就表示 收到新的消息, 那就遍历整个链表
  282. if ( t_pass_flag )
  283. {
  284. analysis_receive_list();
  285. }
  286. //如果解析线程超时通过, 那么就定时处理链表残留的消息,
  287. else
  288. {
  289. analysis_receive_list();
  290. }
  291. }
  292. }
  293. LOG(INFO) << " Communication_socket_base::analysis_data_thread end "<< this;
  294. return;
  295. }
  296. //循环接受链表, 解析消息,
  297. Error_manager Communication_socket_base::analysis_receive_list()
  298. {
  299. Error_manager t_error;
  300. if ( m_receive_data_list.m_termination_flag )
  301. {
  302. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  303. " Communication_socket_base::analysis_receive_list error ");
  304. }
  305. else
  306. {
  307. std::unique_lock<std::mutex> lk(m_receive_data_list.m_mutex);
  308. for (auto iter = m_receive_data_list.m_data_list.begin(); iter != m_receive_data_list.m_data_list.end(); )
  309. {
  310. Communication_message* tp_msg = **iter;
  311. if ( tp_msg == NULL )
  312. {
  313. iter = m_receive_data_list.m_data_list.erase(iter);
  314. //注:erase 删除当前 iter 之后返回下一个节点,当前的 iter 无效化,
  315. }
  316. else
  317. {
  318. //检查消息是否可以被处理
  319. t_error = check_executer(tp_msg);
  320. if ( t_error == SUCCESS)
  321. {
  322. //处理消息
  323. t_error = execute_msg(tp_msg);
  324. // if ( t_error )
  325. // {
  326. // //执行结果不管
  327. // }
  328. // else
  329. // {
  330. // //执行结果不管
  331. // }
  332. delete(tp_msg);
  333. tp_msg = NULL;
  334. iter = m_receive_data_list.m_data_list.erase(iter);
  335. //注:erase 删除当前 iter 之后返回下一个节点,当前的 iter 无效化,
  336. }
  337. else if( t_error == COMMUNICATION_EXCUTER_IS_BUSY)
  338. {
  339. //处理器正忙, 那就不做处理, 直接处理下一个
  340. //注:这条消息就被保留了下来, wait_for_millisecond 超时通过之后, 会循环检查残留的消息.
  341. iter++;
  342. }
  343. else //if( t_error == COMMUNICATION_ANALYSIS_TIME_OUT )
  344. {
  345. //超时了就直接删除
  346. delete(tp_msg);
  347. tp_msg = NULL;
  348. iter = m_receive_data_list.m_data_list.erase(iter);
  349. //注:erase 删除当前 iter 之后返回下一个节点,当前的 iter 无效化,
  350. //注:消息删除之后, 不需要发送答复消息, 发送方也会有超时处理的, 只有 execute_msg 里面可以答复消息
  351. }
  352. }
  353. }
  354. }
  355. return Error_code::SUCCESS;
  356. }
  357. //检查执行者的状态, 判断能否处理这条消息, 需要子类重载
  358. Error_manager Communication_socket_base::check_executer(Communication_message* p_msg)
  359. {
  360. //检查对应模块的状态, 判断是否可以处理这条消息
  361. //同时也要判断是否超时, 超时返回 COMMUNICATION_ANALYSIS_TIME_OUT
  362. //如果处理器正在忙别的, 那么返回 COMMUNICATION_EXCUTER_IS_BUSY
  363. if ( p_msg->is_over_time() )
  364. {
  365. return Error_code::COMMUNICATION_ANALYSIS_TIME_OUT;
  366. }
  367. else
  368. {
  369. bool executer_is_ready = false;
  370. //通过 p_msg->get_message_type() 和 p_msg->get_receiver() 找到处理模块的实例对象, 查询执行人是否可以处理这条消息
  371. //这里子类重载时, 增加判断逻辑, 以后再写.
  372. executer_is_ready = true;
  373. std::cout << "Communication_socket_base::check_msg p_buf = " << p_msg->get_message_buf() << std::endl;
  374. std::cout << "Communication_socket_base::check_msg size = " << p_msg->get_message_buf().size() << std::endl;
  375. if ( executer_is_ready )
  376. {
  377. std::cout << "executer_is_ready , " << std::endl;
  378. return Error_code::SUCCESS;
  379. }
  380. else
  381. {
  382. std::cout << "executer_is_busy , " << std::endl;
  383. return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  384. }
  385. }
  386. return Error_code::SUCCESS;
  387. }
  388. //处理消息
  389. Error_manager Communication_socket_base::execute_msg(Communication_message* p_msg)
  390. {
  391. //先将 p_msg 转化为 对应的格式, 使用对应模块的protobuf来二次解析
  392. // 不能一直使用 Communication_message* p_msg, 这个是要销毁的
  393. //然后处理这个消息, 就是调用对应模块的 execute 接口函数
  394. //执行结果不管, 如果需要答复, 那么对应模块 在自己内部 封装一条消息发送即可.
  395. //子类重载, 需要完全重写, 以后再写.
  396. //注注注注注意了, 本模块只是用来做通信,
  397. //在做处理消息的时候, 可能会调用执行者的接口函数,
  398. //这里不应该长时间阻塞或者处理复杂的逻辑,
  399. //请执行者另开线程来处理任务.
  400. std::cout << "Communication_socket_base::excute_msg p_buf = " << p_msg->get_message_buf() << std::endl;
  401. std::cout << "Communication_socket_base::excute_msg size = " << p_msg->get_message_buf().size() << std::endl;
  402. return Error_code::SUCCESS;
  403. }
  404. //mp_send_data_thread 发送线程执行函数,
  405. //send_data_thread 内部线程负责发送消息
  406. void Communication_socket_base::send_data_thread()
  407. {
  408. LOG(INFO) << " Communication_socket_base::send_data_thread start "<< this;
  409. //通信发送线程, 负责巡检m_send_data_list, 并发送消息
  410. while (m_send_data_condition.is_alive())
  411. {
  412. m_send_data_condition.wait();
  413. if ( m_send_data_condition.is_alive() )
  414. {
  415. std::this_thread::yield();
  416. Communication_message* tp_msg = NULL;
  417. //这里 wait_and_pop 会使用链表内部的 m_data_cond 条件变量来控制等待,
  418. //封装线程使用push的时候, 会唤醒线程并通过等待, 此时 m_send_data_condition 是一直通过的.
  419. //如果需要退出, 那么就要 m_send_data_list.termination_list(); 和 m_send_data_condition.kill_all();
  420. bool is_pop = m_send_data_list.wait_and_pop(tp_msg);
  421. if ( is_pop )
  422. {
  423. if ( tp_msg != NULL )
  424. {
  425. std::unique_lock<std::mutex> lk(m_mutex);
  426. m_socket.send(tp_msg->get_message_buf());
  427. delete(tp_msg);
  428. tp_msg = NULL;
  429. }
  430. }
  431. else
  432. {
  433. //没有取出, 那么应该就是 m_termination_flag 结束了
  434. // return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  435. // " Communication_socket_base::send_data_thread() error ");
  436. }
  437. }
  438. }
  439. LOG(INFO) << " Communication_socket_base::send_data_thread end "<< this;
  440. return;
  441. }
  442. //mp_encapsulate_data_thread 封装线程执行函数,
  443. //encapsulate_data_thread 内部线程负责封装消息
  444. void Communication_socket_base::encapsulate_data_thread()
  445. {
  446. LOG(INFO) << " Communication_socket_base::encapsulate_data_thread start "<< this;
  447. //通信封装线程, 负责定时封装消息, 并存入 m_send_data_list
  448. while (m_encapsulate_data_condition.is_alive())
  449. {
  450. bool t_pass_flag = m_encapsulate_data_condition.wait_for_millisecond(1000);
  451. if ( m_encapsulate_data_condition.is_alive() )
  452. {
  453. std::this_thread::yield();
  454. //如果封装线程被主动唤醒, 那么就表示 需要主动发送消息,
  455. if ( t_pass_flag )
  456. {
  457. //主动发送消息,
  458. }
  459. //如果封装线程超时通过, 那么就定时封装心跳和状态信息
  460. else
  461. {
  462. encapsulate_send_data();
  463. }
  464. }
  465. }
  466. LOG(INFO) << " Communication_socket_base::encapsulate_data_thread end "<< this;
  467. return;
  468. }
  469. //定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
  470. Error_manager Communication_socket_base::encapsulate_send_data()
  471. {
  472. char buf[256] = {0};
  473. static unsigned int t_heartbeat = 0;
  474. sprintf(buf, "Communication_socket_base, heartbeat = %d\0\0\0, test\0", t_heartbeat);
  475. t_heartbeat++;
  476. Communication_message* tp_msg = new Communication_message(buf, 100);
  477. bool is_push = m_send_data_list.push(tp_msg);
  478. if ( is_push == false )
  479. {
  480. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  481. " Communication_socket_base::encapsulate_msg error ");
  482. }
  483. else
  484. {
  485. delete(tp_msg);
  486. tp_msg = NULL;
  487. }
  488. return Error_code::SUCCESS;
  489. }
  490. //封装消息, 需要子类重载
  491. Error_manager Communication_socket_base::encapsulate_msg(std::string message)
  492. {
  493. Communication_message* tp_msg = new Communication_message(message);
  494. bool is_push = m_send_data_list.push(tp_msg);
  495. if ( is_push == false )
  496. {
  497. delete(tp_msg);
  498. tp_msg = NULL;
  499. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  500. " Communication_socket_base::encapsulate_msg error ");
  501. }
  502. return Error_code::SUCCESS;
  503. }
  504. //封装消息, 需要子类重载
  505. Error_manager Communication_socket_base::encapsulate_msg(Communication_message* p_msg)
  506. {
  507. Communication_message* tp_msg = new Communication_message(*p_msg);
  508. bool is_push = m_send_data_list.push(tp_msg);
  509. if ( is_push == false )
  510. {
  511. delete(tp_msg);
  512. tp_msg = NULL;
  513. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  514. " Communication_socket_base::encapsulate_msg error ");
  515. }
  516. return Error_code::SUCCESS;
  517. }