PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/src/network/SimpleBackend.h

http://uftt.googlecode.com/
C Header | 183 lines | 133 code | 48 blank | 2 comment | 0 complexity | c11815e852ef194b77f0e91f502211f7 MD5 | raw file
  1. #ifndef SIMPLE_BACKEND_H
  2. #define SIMPLE_BACKEND_H
  3. #include <set>
  4. #include <boost/shared_ptr.hpp>
  5. #include <boost/asio.hpp>
  6. #include <boost/signal.hpp>
  7. #include <boost/function.hpp>
  8. #include <boost/foreach.hpp>
  9. #include <boost/system/error_code.hpp>
  10. #include "../net-asio/asio_file_stream.h"
  11. #include "../net-asio/asio_http_request.h"
  12. #include "../net-asio/asio_dgram_conn.h"
  13. #include "../net-asio/ipaddr_watcher.h"
  14. #include "INetModule.h"
  15. #include "Misc.h"
  16. #include "../Types.h"
  17. #include "../UFTTSettings.h"
  18. #include "../UFTTCore.h"
  19. #include "../util/asio_timer_oneshot.h"
  20. typedef dgram::conn_service<boost::asio::ip::udp> UDPConnService;
  21. typedef dgram::conn<boost::asio::ip::udp> UDPConn;
  22. struct UDPSockInfo {
  23. boost::asio::ip::udp::socket& sock;
  24. boost::asio::ip::udp::endpoint bcst_ep;
  25. bool is_v4;
  26. bool is_main;
  27. UDPSockInfo(boost::asio::ip::udp::socket& nsock, bool is_v4_)
  28. : sock(nsock), is_v4(is_v4_), is_main(false)
  29. {};
  30. };
  31. typedef boost::shared_ptr<UDPSockInfo> UDPSockInfoRef;
  32. class ConnectionBase;
  33. typedef boost::shared_ptr<ConnectionBase> ConnectionBaseRef;
  34. //class SimpleTCPConnection;
  35. typedef boost::shared_ptr<class SimpleTCPConnection> SimpleTCPConnectionRef;
  36. typedef boost::shared_ptr<class UDPSemiConnection> UDPSemiConnectionRef;
  37. const boost::asio::ip::udp::endpoint uftt_bcst_ep;
  38. const UDPSockInfoRef uftt_bcst_if;
  39. class SimpleBackend: public INetModule {
  40. private:
  41. enum {
  42. UDPPACK_QUERY_SHARE = 1,
  43. UDPPACK_REPLY_SHARE,
  44. UDPPACK_REPLY_UPDATE,
  45. UDPPACK_QUERY_PEER,
  46. UDPPACK_REPLY_PEER,
  47. UDPPACK_REPLY_UPDATE_NEW,
  48. };
  49. UFTTCore* core;
  50. boost::asio::io_service& service;
  51. std::map<boost::asio::ip::address, UDPSockInfoRef> udpsocklist;
  52. boost::asio::ip::tcp::acceptor tcplistener_v4;
  53. boost::asio::ip::tcp::acceptor tcplistener_v6;
  54. std::vector<ConnectionBaseRef> conlist;
  55. int udpfails;
  56. uint32 mid;
  57. ipv4_watcher watcher_v4;
  58. ipv6_watcher watcher_v6;
  59. boost::scoped_ptr<UDPConnService> udp_conn_service;
  60. #define UDP_BUF_SIZE (2048)
  61. uint8 recv_buf_v4[UDP_BUF_SIZE];
  62. uint8 recv_buf_v6[UDP_BUF_SIZE];
  63. boost::asio::ip::udp::endpoint recv_peer_v4;
  64. boost::asio::ip::udp::endpoint recv_peer_v6;
  65. boost::asio::ip::udp::socket udp_sock_v4;
  66. boost::asio::ip::udp::socket udp_sock_v6;
  67. UDPSockInfoRef udp_info_v4;
  68. UDPSockInfoRef udp_info_v6;
  69. std::set<boost::asio::ip::udp::endpoint> foundpeers;
  70. std::set<boost::asio::ip::udp::endpoint> trypeers;
  71. bool clearpeers;
  72. boost::asio::deadline_timer peerfindertimer;
  73. boost::posix_time::ptime lastpeerquery;
  74. boost::posix_time::ptime prevpeerquery;
  75. boost::asio::deadline_timer stun_timer;
  76. boost::asio::deadline_timer stun_timer2;
  77. boost::asio::ip::udp::endpoint stun_server;
  78. boost::asio::ip::udp::endpoint stun_endpoint;
  79. bool stun_server_found;
  80. int stun_retries;
  81. void start_peerfinder();
  82. void handle_peerfinder_query(const boost::system::error_code& e, boost::shared_ptr<boost::asio::http_request> request, int prog);
  83. void handle_peerfinder_timer(const boost::system::error_code& e);
  84. void start_udp_receive(UDPSockInfoRef si, uint8* recv_buf, boost::asio::ip::udp::endpoint* recv_peer);
  85. std::set<uint32> parseVersions(uint8* base, uint start, uint len);
  86. std::set<uint32> parseVersions(uint8* base, uint start, uint len, uint* end);
  87. void handle_discovery_packet(UDPSockInfoRef si, uint8* recv_buf, boost::asio::ip::udp::endpoint* recv_peer, std::size_t len, uint32 version);
  88. void handle_stun_packet(UDPSockInfoRef si, uint8* recv_buf, boost::asio::ip::udp::endpoint* recv_peer, std::size_t len);
  89. void handle_rudp_packet(UDPSockInfoRef si, uint8* recv_buf, boost::asio::ip::udp::endpoint* recv_peer, std::size_t len);
  90. void handle_udp_receive(UDPSockInfoRef si, uint8* recv_buf, boost::asio::ip::udp::endpoint* recv_peer, const boost::system::error_code& e, std::size_t len);
  91. void send_udp_packet_to(boost::asio::ip::udp::socket& sock, const boost::asio::ip::udp::endpoint& ep, boost::asio::const_buffers_1 buf, boost::system::error_code& err, int flags = 0);
  92. void send_udp_packet_to(UDPSockInfoRef si, const boost::asio::ip::udp::endpoint& ep, boost::asio::const_buffers_1 buf, boost::system::error_code& err, int flags = 0);
  93. void send_udp_packet(UDPSockInfoRef si, const boost::asio::ip::udp::endpoint& ep, boost::asio::const_buffers_1 buf, boost::system::error_code& err, int flags = 0);
  94. void send_query(UDPSockInfoRef si, const boost::asio::ip::udp::endpoint& ep);
  95. void send_publish(UDPSockInfoRef si, const boost::asio::ip::udp::endpoint& ep, const std::string& name, int sharever, bool isbuild = false);
  96. void send_publishes(UDPSockInfoRef si, const boost::asio::ip::udp::endpoint& ep, int sharever, bool sendbuilds);
  97. void add_local_share(std::string name);
  98. void stun_new_addr();
  99. void stun_do_check(const boost::system::error_code& e, int retry = 0);
  100. void stun_send_bind(const boost::system::error_code& e);
  101. void new_iface(UDPSockInfoRef si);
  102. void add_ip_addr_ipv4(std::pair<boost::asio::ip::address, boost::asio::ip::address> addrwbcst);
  103. void add_ip_addr_ipv6(boost::asio::ip::address addr);
  104. void del_ip_addr(boost::asio::ip::address addr);
  105. boost::signals::connection attach_progress_handler(const TaskID& tid, const boost::function<void(const TaskInfo&)>& cb);
  106. boost::signal<void(const ShareInfo&)> sig_new_share;
  107. boost::signal<void(const TaskInfo&)> sig_new_task;
  108. UFTTSettingsRef settings;
  109. public:
  110. void print_addr(std::string prefix, boost::asio::ip::address a)
  111. {
  112. std::cout << prefix << a << '\n';
  113. }
  114. SimpleBackend(UFTTCore* core_);
  115. void download_share(const ShareID& sid, const ext::filesystem::path& dlpath);
  116. void dl_connect_handle(const boost::system::error_code& e, ConnectionBaseRef conn, std::string name, ext::filesystem::path dlpath, uint32 version);
  117. void start_tcp_accept(boost::asio::ip::tcp::acceptor* tcplistener);
  118. void handle_tcp_accept(boost::asio::ip::tcp::acceptor* tcplistener, SimpleTCPConnectionRef newconn, const boost::system::error_code& e);
  119. void start_udp_accept(UDPConnService* cservice);
  120. void handle_udp_accept(UDPConnService* cservice, UDPSemiConnectionRef newconn, const boost::system::error_code& e);
  121. public:
  122. // Implementation of IBackend interface
  123. virtual SignalConnection connectSigAddShare(const boost::function<void(const ShareInfo&)>&);
  124. virtual SignalConnection connectSigDelShare(const boost::function<void(const ShareID&)>&);
  125. virtual SignalConnection connectSigNewTask(const boost::function<void(const TaskInfo&)>&);
  126. virtual SignalConnection connectSigTaskStatus(const TaskID& tid, const boost::function<void(const TaskInfo&)>&);
  127. virtual void doRefreshShares();
  128. virtual void startDownload(const ShareID& sid, const ext::filesystem::path& path);
  129. virtual void doManualPublish(const std::string& host);
  130. virtual void doManualQuery(const std::string& host);
  131. virtual void notifyAddLocalShare(const LocalShareID& sid);
  132. virtual void notifyDelLocalShare(const LocalShareID& sid);
  133. virtual void setModuleID(uint32 mid);
  134. };
  135. #endif//SIMPLE_BACKEND_H