znet.cpp 8.3 KB

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