1
0

znet.cpp 7.5 KB

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