web-client.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #include <thread>
  2. #include <memory>
  3. #include <ev++.h>
  4. #include "zio.h"
  5. #include "log.h"
  6. #include "zloop.h"
  7. #include "web-client.h"
  8. struct web_client_http:ev::io
  9. {
  10. char*m_buff;
  11. int m_pos,m_size;
  12. int m_fd,m_status;
  13. web_client_http()
  14. {
  15. m_fd=-1;
  16. m_pos=0;
  17. m_size=4096;
  18. m_buff=(char*)malloc(m_size);
  19. }
  20. int connect_tcp(const char*ip,int port)
  21. {
  22. int fd=zio::build_stream();
  23. if(zio::connect(fd,"127.0.0.1",4000))
  24. {
  25. zio::close(fd);
  26. return m_fd=-1;
  27. }
  28. zio::setiobuf(fd,16<<10,16<<10);
  29. zio::setblocking(fd,false);
  30. return m_fd=fd;
  31. }
  32. int connect_ws(const char*ip,int port)
  33. {
  34. const char*fmt=
  35. "GET / HTTP/1.1\n"
  36. "Connection:Upgrade\n"
  37. "Host:127.0.0.1:8088\n"
  38. "Sec-WebSocket-Extensions:x-webkit-deflate-frame\n"
  39. "Sec-WebSocket-Key:puVOuWb7rel6z2AVZBKnfw==\n"
  40. "Sec-WebSocket-Version:13\n"
  41. "Upgrade:webet\n";
  42. if(connect_tcp(ip,port)<0)
  43. return -1;
  44. int len=strlen(fmt);
  45. if(len!=zio::writev(m_fd,fmt,len))
  46. {
  47. close();
  48. return -1;
  49. }
  50. for(;;)
  51. {
  52. len=zio::read(m_fd,m_buff+m_pos,m_size-m_pos);
  53. if(len==-1)
  54. {
  55. close();
  56. return -1;
  57. }
  58. if(len==-2)
  59. continue;
  60. m_pos+=len;
  61. m_buff[m_pos]=0;
  62. if(strstr(m_buff,"\n\n"))
  63. break;
  64. }
  65. log_info("http debug:%s",m_buff);
  66. }
  67. void close()
  68. {
  69. zio::close(m_fd);
  70. }
  71. ~web_client_http()
  72. {
  73. close();
  74. ::free(m_buff);
  75. }
  76. };
  77. struct web_client_net_impl: web_client,zloop_base
  78. {
  79. std::unique_ptr<std::thread> m_thread;
  80. std::mutex m_mutex;
  81. std::list<std::string> m_req_list;
  82. ev::timer m_check_timer;
  83. std::string m_ip;
  84. int m_port;
  85. web_client_net_impl ()
  86. {
  87. m_thread.reset(new std::thread(std::bind(&web_client_net_impl::run,this)));
  88. }
  89. virtual void on_async_0()
  90. {
  91. if(check_stop_flag())
  92. return;
  93. std::list<std::string> async_list;
  94. {
  95. std::unique_lock<std::mutex> _lock(m_mutex);
  96. m_req_list.swap(async_list);
  97. }
  98. on_async(async_list);
  99. }
  100. void on_async(const std::list<std::string>&req)
  101. {
  102. }
  103. void on_check_timer(ev::timer&t,int)
  104. {
  105. }
  106. void run()
  107. {
  108. m_check_timer.set(*this);
  109. m_check_timer.set<web_client_net_impl,&web_client_net_impl::on_check_timer>(this);
  110. m_check_timer.start(0.,1.);
  111. ev::dynamic_loop::run(0);
  112. log_info("web_client_net_impl exit.");
  113. }
  114. void post(const char*m,int len)
  115. {
  116. {
  117. std::unique_lock<std::mutex> _lock(m_mutex);
  118. m_req_list.push_back(std::string(m,len));
  119. }
  120. m_async.send();
  121. }
  122. void stop()
  123. {
  124. async_stop();
  125. m_thread->join();
  126. }
  127. std::atomic<bool> m_start_flag{false};
  128. void start()
  129. {
  130. if(m_start_flag.load())
  131. return;
  132. m_start_flag.store(true);
  133. }
  134. };
  135. web_client*web_client::instance(const char*ip,int port,const char*path)
  136. {
  137. static web_client_net_impl impl;
  138. impl.start();
  139. return &impl;
  140. }