znet.cpp 8.4 KB

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