znet.cpp 8.6 KB

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