communication_socket_base.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. nnxx::message t_msg = m_socket.recv(1);
  211. if ( t_msg.size()>0 )
  212. {
  213. Binary_buf * tp_binary_buf = new Binary_buf( (char*)(t_msg.data()), t_msg.size() );
  214. // std::cout << tp_binary_buf->get_buf() << std::endl;
  215. bool is_push = m_receive_data_list.push(tp_binary_buf);
  216. // if ( is_push == false )
  217. // {
  218. // return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  219. // " m_receive_data_list.push error ");
  220. // }
  221. //唤醒解析线程一次,
  222. m_analysis_data_condition.notify_all(false, true);
  223. }
  224. }
  225. }
  226. LOG(INFO) << " Communication_socket_base::receive_data_thread end "<< this;
  227. return;
  228. }
  229. //mp_analysis_data_thread 解析线程执行函数,
  230. //analysis_data_thread 内部线程负责解析消息
  231. void Communication_socket_base::analysis_data_thread()
  232. {
  233. LOG(INFO) << " Communication_socket_base::analysis_data_thread start "<< this;
  234. //通信解析线程, 负责巡检m_receive_data_list, 并解析和处理消息
  235. while (m_analysis_data_condition.is_alive())
  236. {
  237. bool t_pass_flag = m_analysis_data_condition.wait_for_millisecond(1000);
  238. if ( m_analysis_data_condition.is_alive() )
  239. {
  240. std::this_thread::yield();
  241. //如果解析线程被主动唤醒, 那么就表示 收到新的消息, 那就遍历整个链表
  242. if ( t_pass_flag )
  243. {
  244. analysis_receive_list();
  245. }
  246. //如果解析线程超时通过, 那么就定时处理链表残留的消息,
  247. else
  248. {
  249. analysis_receive_list();
  250. }
  251. }
  252. }
  253. LOG(INFO) << " Communication_socket_base::analysis_data_thread end "<< this;
  254. return;
  255. }
  256. //循环接受链表, 解析消息,
  257. Error_manager Communication_socket_base::analysis_receive_list()
  258. {
  259. Error_manager t_error;
  260. if ( m_receive_data_list.m_termination_flag )
  261. {
  262. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  263. " Communication_socket_base::analysis_receive_list error ");
  264. }
  265. else
  266. {
  267. std::unique_lock<std::mutex> lk(m_receive_data_list.m_mutex);
  268. for (auto iter = m_receive_data_list.m_data_list.begin(); iter != m_receive_data_list.m_data_list.end(); )
  269. {
  270. Binary_buf* tp_buf = **iter;
  271. if ( tp_buf == NULL )
  272. {
  273. iter = m_receive_data_list.m_data_list.erase(iter);
  274. //注:erase 删除当前 iter 之后返回下一个节点,当前的 iter 无效化,
  275. }
  276. else
  277. {
  278. //检查消息是否可以被解析
  279. t_error = check_msg(tp_buf);
  280. if ( t_error == SUCCESS)
  281. {
  282. //处理消息
  283. t_error = excute_msg(tp_buf);
  284. // if ( t_error )
  285. // {
  286. // //执行结果不管
  287. // }
  288. // else
  289. // {
  290. // //执行结果不管
  291. // }
  292. delete(tp_buf);
  293. tp_buf = NULL;
  294. iter = m_receive_data_list.m_data_list.erase(iter);
  295. //注:erase 删除当前 iter 之后返回下一个节点,当前的 iter 无效化,
  296. }
  297. else if( t_error == COMMUNICATION_ANALYSIS_TIME_OUT )
  298. {
  299. //超时了就直接删除
  300. delete(tp_buf);
  301. tp_buf = NULL;
  302. iter = m_receive_data_list.m_data_list.erase(iter);
  303. //注:erase 删除当前 iter 之后返回下一个节点,当前的 iter 无效化,
  304. }
  305. else //if( t_error == COMMUNICATION_EXCUTER_IS_BUSY)
  306. {
  307. //处理器正忙, 那就不做处理, 直接处理下一个
  308. //注:这条消息就被保留了下来, wait_for_millisecond 超时通过之后, 会循环检查残留的消息.
  309. iter++;
  310. }
  311. }
  312. }
  313. }
  314. return Error_code::SUCCESS;
  315. }
  316. //检查消息是否可以被解析
  317. Error_manager Communication_socket_base::check_msg(Binary_buf* p_buf)
  318. {
  319. //检查对应模块的状态, 判断是否可以处理这条消息
  320. //同时也要判断是否超时, 超时返回 COMMUNICATION_ANALYSIS_TIME_OUT
  321. //如果处理器正在忙别的, 那么返回 COMMUNICATION_EXCUTER_IS_BUSY
  322. //......................
  323. std::cout << "Communication_socket_base::check_msg p_buf = " << p_buf->get_buf() << std::endl;
  324. std::cout << "Communication_socket_base::check_msg size = " << p_buf->get_length() << std::endl;
  325. // return Error_code::COMMUNICATION_ANALYSIS_TIME_OUT;
  326. // return Error_code::COMMUNICATION_EXCUTER_IS_BUSY;
  327. return Error_code::SUCCESS;
  328. }
  329. //处理消息
  330. Error_manager Communication_socket_base::excute_msg(Binary_buf* p_buf)
  331. {
  332. //先将 p_buf 转化为 对应的格式, 不能一直使用 p_buf, 和这个是要销毁的
  333. //然后处理这个消息, 就是调用对应模块的 excute 接口函数
  334. //执行结果不管, 如果需要答复, 那么对应模块 在自己内部 封装一条消息发送即可.
  335. std::cout << "Communication_socket_base::excute_msg p_buf = " << p_buf->get_buf() << std::endl;
  336. std::cout << "Communication_socket_base::excute_msg size = " << p_buf->get_length() << std::endl;
  337. return Error_code::SUCCESS;
  338. }
  339. //mp_send_data_thread 发送线程执行函数,
  340. //send_data_thread 内部线程负责发送消息
  341. void Communication_socket_base::send_data_thread()
  342. {
  343. LOG(INFO) << " Communication_socket_base::send_data_thread start "<< this;
  344. //通信发送线程, 负责巡检m_send_data_list, 并发送消息
  345. while (m_send_data_condition.is_alive())
  346. {
  347. m_send_data_condition.wait();
  348. if ( m_send_data_condition.is_alive() )
  349. {
  350. std::this_thread::yield();
  351. Binary_buf* tp_buf = NULL;
  352. //这里 wait_and_pop 会使用链表内部的 m_data_cond 条件变量来控制等待,
  353. //封装线程使用push的时候, 会唤醒线程并通过等待, 此时 m_send_data_condition 是一直通过的.
  354. //如果需要退出, 那么就要 m_send_data_list.termination_list(); 和 m_send_data_condition.kill_all();
  355. bool is_pop = m_send_data_list.wait_and_pop(tp_buf);
  356. if ( is_pop )
  357. {
  358. if ( tp_buf != NULL )
  359. {
  360. std::unique_lock<std::mutex> lk(m_mutex);
  361. m_socket.send(tp_buf->get_buf(), tp_buf->get_length(), 0);
  362. delete(tp_buf);
  363. tp_buf = NULL;
  364. }
  365. }
  366. else
  367. {
  368. //没有取出, 那么应该就是 m_termination_flag 结束了
  369. // return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  370. // " Communication_socket_base::send_data_thread() error ");
  371. }
  372. }
  373. }
  374. LOG(INFO) << " Communication_socket_base::send_data_thread end "<< this;
  375. return;
  376. }
  377. //mp_encapsulate_data_thread 封装线程执行函数,
  378. //encapsulate_data_thread 内部线程负责封装消息
  379. void Communication_socket_base::encapsulate_data_thread()
  380. {
  381. LOG(INFO) << " Communication_socket_base::encapsulate_data_thread start "<< this;
  382. //通信封装线程, 负责定时封装消息, 并存入 m_send_data_list
  383. while (m_encapsulate_data_condition.is_alive())
  384. {
  385. bool t_pass_flag = m_encapsulate_data_condition.wait_for_millisecond(1000);
  386. if ( m_encapsulate_data_condition.is_alive() )
  387. {
  388. std::this_thread::yield();
  389. //如果封装线程被主动唤醒, 那么就表示 需要主动发送消息,
  390. if ( t_pass_flag )
  391. {
  392. //主动发送消息,
  393. }
  394. //如果封装线程超时通过, 那么就定时封装心跳和状态信息
  395. else
  396. {
  397. encapsulate_send_data();
  398. }
  399. }
  400. }
  401. LOG(INFO) << " Communication_socket_base::encapsulate_data_thread end "<< this;
  402. return;
  403. }
  404. //定时封装发送消息, 一般为心跳和状态信息, 需要子类重载
  405. Error_manager Communication_socket_base::encapsulate_send_data()
  406. {
  407. char buf[256] = {0};
  408. static unsigned int t_heartbeat = 0;
  409. sprintf(buf, "Communication_socket_base, heartbeat = %d\0\0\0", t_heartbeat);
  410. t_heartbeat++;
  411. Binary_buf* tp_buf = new Binary_buf(buf, strlen(buf)+1);//+1是为了保证发送了结束符, 方便打印
  412. bool is_push = m_send_data_list.push(tp_buf);
  413. if ( is_push == false )
  414. {
  415. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  416. " Communication_socket_base::encapsulate_msg error ");
  417. }
  418. return Error_code::SUCCESS;
  419. }
  420. //封装消息, 需要子类重载
  421. Error_manager Communication_socket_base::encapsulate_msg(Binary_buf* p_buf)
  422. {
  423. Binary_buf * tp_buf = new Binary_buf(*p_buf);
  424. bool is_push = m_send_data_list.push(p_buf);
  425. if ( is_push == false )
  426. {
  427. return Error_manager(Error_code::CONTAINER_IS_TERMINATE, Error_level::MINOR_ERROR,
  428. " Communication_socket_base::encapsulate_msg error ");
  429. }
  430. return Error_code::SUCCESS;
  431. }