znet.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. #include <log.h>
  2. #include <unistd.h>
  3. #include <signal.h>
  4. #include <stdio.h>
  5. #include <list>
  6. #include <vector>
  7. #include <set>
  8. #include <thread>
  9. #include <mutex>
  10. #include <atomic>
  11. #include <algorithm>
  12. #include <fstream>
  13. #include <zio.h>
  14. #include <znet.h>
  15. #include <zloop.h>
  16. #include <clock.h>
  17. #include <worker.h>
  18. #include "config_file.h"
  19. #include "crc.h"
  20. extern config_file config;
  21. struct client_ex:client
  22. {
  23. virtual void on_notify()=0;
  24. virtual void close_impl()=0;
  25. };
  26. struct io_context: zloop<std::shared_ptr<client>> ,service_handle
  27. {
  28. private:
  29. service_callback&m_serv;
  30. ev::timer m_timer;
  31. public:
  32. std::vector<std::shared_ptr<client>> m_thread_clts;
  33. io_context(service_callback&serv)
  34. :m_serv(serv)
  35. ,m_timer(*this)
  36. {
  37. m_thread_clts.reserve(2048);
  38. m_timer.set<io_context,&io_context::on_timer>(this);
  39. m_timer.start(20,1);
  40. }
  41. virtual ~io_context()
  42. {
  43. }
  44. void boardcast(const std::vector<char>&msg)
  45. {
  46. for(auto i:m_thread_clts)
  47. {
  48. std::vector<char> tmp(msg);
  49. i->send(std::move(tmp));
  50. }
  51. }
  52. void on_timer()
  53. {
  54. m_serv.on_timer();
  55. }
  56. void on_connect(std::shared_ptr<client> clt)
  57. {
  58. if(clt->handle()>=(int)m_thread_clts.size())
  59. {
  60. m_thread_clts.resize(clt->handle()+1);
  61. }
  62. m_thread_clts[clt->handle()]=clt;
  63. m_serv.on_connect(clt);
  64. }
  65. void on_close(std::shared_ptr<client> clt)
  66. {
  67. m_serv.on_close(clt);
  68. m_thread_clts[clt->handle()].reset();
  69. }
  70. void on_send_timeout(std::shared_ptr<client> clt)
  71. {
  72. m_serv.on_send_timeout(clt);
  73. }
  74. void on_recv_timeout(std::shared_ptr<client> clt)
  75. {
  76. m_serv.on_recv_timeout(clt);
  77. }
  78. void on_message(std::shared_ptr<client> clt,const char*data,size_t len)
  79. {
  80. m_serv.on_message(clt,data,len);
  81. }
  82. void close_all()
  83. {
  84. for(auto clt:m_thread_clts)
  85. {
  86. if(!clt)
  87. continue;
  88. ((client_ex*)clt.get())->close_impl();
  89. }
  90. }
  91. void on_async(const std::list<std::shared_ptr<client>>&notify_clts)
  92. {
  93. for(auto&clt:notify_clts)
  94. {
  95. ((client_ex*)clt.get())->on_notify();
  96. }
  97. }
  98. void stop()
  99. {
  100. async_stop();
  101. }
  102. };
  103. struct fd_io:ev::io
  104. {
  105. io_context&m_ic;
  106. int m_fd;
  107. fd_io(io_context&ic,int fd,int ev_flag=EV_READ)
  108. :ev::io(ic)
  109. ,m_ic(ic)
  110. ,m_fd(fd)
  111. {
  112. zio::setblocking(fd,false);
  113. this->set(this);
  114. this->set(m_fd,ev_flag);
  115. this->start();
  116. }
  117. void stop () throw () \
  118. {
  119. ev::io::stop();
  120. }
  121. io_context&context()
  122. {
  123. return m_ic;
  124. }
  125. virtual void operator()(ev::io &w, int)=0;
  126. virtual ~fd_io()
  127. {
  128. stop();
  129. zio::close(m_fd);
  130. }
  131. };
  132. struct stdin_io:fd_io
  133. {
  134. stdin_io(io_context&ic)
  135. :fd_io(ic,0)
  136. {}
  137. virtual void operator()(ev::io &w, int)
  138. {
  139. #if 0
  140. char buf[256];
  141. if(!gets(buf))
  142. return;
  143. if(strchr(buf,'X'))
  144. {
  145. log_info("stdin input 'X', exiting...");
  146. worker::instance()->stop();
  147. stop();
  148. m_ic.stop();
  149. }
  150. #endif
  151. }
  152. };
  153. struct sock_client:fd_io,client_ex
  154. {
  155. io_context&m_ic;
  156. std::string m_name;
  157. std::atomic<bool> m_close_flag{false};
  158. int m_type=1;//site
  159. char *m_b{0};
  160. int m_clen{0};
  161. int m_size{1<<16};
  162. int m_max_package_size{4096};
  163. ev::timer m_recv_timer;
  164. // ev::timer m_send_timer;
  165. std::mutex m_mutex;
  166. std::list<std::vector<char>> m_olist;
  167. std::vector<char> m_obuf;
  168. size_t m_opos=0;
  169. bool m_can_write{false};
  170. sock_client(io_context&ic,const char*name,int fd,int recv_time_out,int max_package_size)
  171. :fd_io(ic,fd,EV_READ|EV_WRITE)
  172. ,m_ic(ic)
  173. ,m_name(name)
  174. ,m_recv_timer(ic)
  175. {
  176. m_max_package_size=max_package_size;
  177. // m_recv_timer.set(ic);
  178. m_recv_timer.set(recv_time_out,0);
  179. m_recv_timer.set<sock_client,&sock_client::on_recv_timeout>(this);
  180. m_recv_timer.start();
  181. // m_send_timer.set(ic);
  182. // m_send_timer.set(5,0);
  183. // m_send_timer.set<sock_client,&sock_client::on_send_timeout>(this);
  184. // m_send_timer.start();
  185. m_b=(char*)malloc(m_size);
  186. }
  187. ~sock_client()
  188. {
  189. free(m_b);
  190. }
  191. int type()
  192. {
  193. return m_type;
  194. }
  195. void close()
  196. {
  197. m_close_flag.store(true);
  198. m_ic.async_request(shared_from_this());
  199. }
  200. void send(std::vector<char>&&b)
  201. {
  202. {
  203. std::unique_lock<std::mutex> _lock(m_mutex);
  204. m_olist.push_back(std::move(b));
  205. }
  206. m_ic.async_request(shared_from_this());
  207. }
  208. void on_send_timeout()
  209. {
  210. m_ic.on_send_timeout(shared_from_this());
  211. }
  212. void on_recv_timeout()
  213. {
  214. m_ic.on_recv_timeout(shared_from_this());
  215. }
  216. std::string name()
  217. {
  218. return m_name;
  219. }
  220. int handle()
  221. {
  222. return m_fd;
  223. }
  224. void grow_buf(int len)
  225. {
  226. if(m_size-m_clen>=len)
  227. return;
  228. int size=m_size;
  229. while(m_size-m_clen<len)
  230. size<<=1;
  231. if(size!=m_size)
  232. {
  233. m_b=(char*)realloc(m_b,m_size=size);
  234. }
  235. }
  236. int read_clt()
  237. {
  238. for(;m_clen<m_size;)
  239. {
  240. int rc=zio::read(m_fd,m_b+m_clen,m_size-m_clen);
  241. if(rc>0)
  242. {
  243. m_clen+=rc;
  244. continue;
  245. }
  246. if(rc==-2)
  247. return 0;
  248. if(rc==0)
  249. {
  250. log_info("socket %d(%s) close by remote",m_fd,m_name.c_str());
  251. }
  252. else if(rc==-1)
  253. {
  254. log_errno("hava a error on socket %d(%s)",m_fd,m_name.c_str());
  255. }
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. void close_impl()
  261. {
  262. m_recv_timer.stop();
  263. // m_send_timer.stop();
  264. fd_io::stop();
  265. m_ic.on_close(shared_from_this());
  266. }
  267. size_t calc_length(uint8_t*b)const
  268. {
  269. return (b[0]<<8)|b[1];
  270. }
  271. int io_read()
  272. {
  273. if(read_clt()<0)
  274. return -1;
  275. int msg_len;
  276. for(;m_clen>=2;)
  277. {
  278. msg_len=calc_length((uint8_t*)m_b)+2;
  279. if(msg_len>m_max_package_size)
  280. return -1;
  281. if(m_clen<msg_len)
  282. break;
  283. logn_bin(1,name().c_str(),m_b,msg_len);//输出二进制日志
  284. if(check_crc(m_b,msg_len))
  285. {
  286. on_message(m_b,msg_len);
  287. }
  288. else
  289. {
  290. log_errno("check_crc_error,socket close... site=%s.",m_name.c_str());
  291. return -1;
  292. }
  293. memmove(m_b,&m_b[msg_len],m_clen-msg_len);
  294. m_clen-=msg_len;
  295. }
  296. return 0;
  297. }
  298. int io_write()
  299. {
  300. if(! m_can_write)
  301. {
  302. set(EV_READ|EV_WRITE);
  303. // m_send_timer.set(5);
  304. // m_send_timer.start();
  305. return 0;
  306. }
  307. for(;;)
  308. {
  309. if(m_obuf.size()==m_opos)
  310. {
  311. std::unique_lock<std::mutex> _lock(m_mutex);
  312. if(m_olist.empty())
  313. break;
  314. m_obuf.swap(m_olist.front());
  315. m_olist.pop_front();
  316. m_opos=0;
  317. }
  318. int left=m_obuf.size()-m_opos;
  319. int rc=zio::write(m_fd,&*m_obuf.begin()+m_opos,left);
  320. if(rc>0)
  321. {
  322. m_opos+=rc;
  323. if(m_opos==m_obuf.size())
  324. continue;
  325. }
  326. else if(rc==0||rc==-2)//缓冲区满
  327. {
  328. m_can_write=false;
  329. set(EV_READ|EV_WRITE);
  330. // m_send_timer.set(5);
  331. // m_send_timer.start();
  332. break;
  333. }
  334. else
  335. {
  336. log_errno("zio::write(%d,ptr,%d)",m_fd,m_obuf.size()-m_opos);
  337. return -1;
  338. }
  339. }
  340. if(m_olist.empty() && m_can_write)
  341. {
  342. set(EV_READ);
  343. // m_send_timer.stop();
  344. }
  345. return 0;
  346. }
  347. void operator()(ev::io &w, int flag)
  348. {
  349. if(flag & EV_WRITE)
  350. {
  351. log_debug("socket %d(%s) can write,flag=%d." ,m_fd,m_name.c_str(),flag);
  352. m_can_write=true;
  353. // m_send_timer.stop();
  354. if(io_write()<0)
  355. {
  356. close_impl();
  357. return;
  358. }
  359. }
  360. if(flag & EV_READ)
  361. {
  362. // log_debug("socket %d(%s) can read,flag=%d." ,m_fd,m_name.c_str(),flag);
  363. // zclock c;
  364. if(io_read()<0)
  365. {
  366. close_impl();
  367. return;
  368. }
  369. // log_info("use time %d ms.",c.count_us());
  370. m_recv_timer.set(5);
  371. }
  372. }
  373. bool check_crc(void *b0,size_t mlen)
  374. {
  375. uint8_t*b=(uint8_t*)b0;
  376. uint16_t crc=b[mlen-2];
  377. crc<<=8;
  378. crc|=b[mlen-1];
  379. uint16_t ccrc=do_crc((unsigned char*)b+2,mlen-4);
  380. return ccrc==crc;
  381. }
  382. void on_message(const char*m,int mlen)
  383. {
  384. m_ic.on_message(shared_from_this(),m,mlen);
  385. }
  386. void on_notify()
  387. {
  388. if(m_close_flag)
  389. {
  390. close_impl();
  391. return;
  392. }
  393. io_write();
  394. }
  395. };
  396. struct sock_listen: fd_io
  397. {
  398. sock_listen(io_context&ic,int fd):fd_io(ic,fd){}
  399. int recv_time_out=config.get("service.recv_timeout",30);
  400. int max_package_size=config.get("service.max_package",2048);
  401. void operator()(ev::io &w, int)
  402. {
  403. char name[32];
  404. int fd=zio::accept(m_fd,name);
  405. if(fd<0)
  406. return;
  407. zio::setiobuf(fd,32<<10,32<<10);
  408. m_ic.on_connect(std::make_shared<sock_client>(m_ic,name,fd,recv_time_out,max_package_size));
  409. }
  410. };
  411. struct signal_w:ev::sig
  412. {
  413. io_context&m_ic;
  414. signal_w(io_context&ic, int s)
  415. :ev::sig(ic)
  416. ,m_ic(ic)
  417. {
  418. //this->set(m_ic);
  419. this->set(this);
  420. this->set(s);
  421. this->start();
  422. }
  423. void operator()(ev::sig &w, int s)
  424. {
  425. log_info("recved signal %d",s);
  426. worker::instance()->stop();
  427. stop();
  428. m_ic.stop();
  429. }
  430. };
  431. struct main_loop:io_context
  432. {
  433. main_loop(service_callback&sc)
  434. :io_context(sc)
  435. {
  436. }
  437. virtual int run(int port)
  438. {
  439. int fd=zio::listen_on(port);
  440. if(fd<0)
  441. {
  442. log_errno("try listen_on %d",port);
  443. return -1;
  444. }
  445. sock_listen _1(*this,fd);
  446. // stdin_io _2(*this);
  447. block_sig(SIGPIPE);
  448. signal_w sint(*this,SIGINT),term(*this,SIGTERM);
  449. ev::dynamic_loop::run(0);
  450. _1.stop();
  451. close_all();
  452. return 0;
  453. }
  454. void block_sig(int sig)
  455. {
  456. sigset_t signal_mask;
  457. sigemptyset(&signal_mask);
  458. sigaddset(&signal_mask, sig);
  459. if(pthread_sigmask(SIG_BLOCK, &signal_mask, NULL) == -1)
  460. {
  461. log_errno("block signal:%d",sig);
  462. }
  463. }
  464. };
  465. service_handle*service_handle::instance(service_callback*sc)
  466. {
  467. static main_loop _impl(*sc);
  468. sc->set_handle(&_impl);
  469. return &_impl;
  470. }