web-client.cpp 2.7 KB

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