znet.cpp 7.9 KB

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