znet.cpp 7.9 KB

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