znet.cpp 7.9 KB

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