PageRenderTime 46ms CodeModel.GetById 7ms RepoModel.GetById 1ms app.codeStats 0ms

/src/bench/Bench2.cpp

http://uftt.googlecode.com/
C++ | 265 lines | 178 code | 43 blank | 44 comment | 11 complexity | 0bd3da8a520a6493cd0f859f674781a5 MD5 | raw file
  1. #include "../Types.h"
  2. #if 0
  3. #include "../net-asio/asio_ipx.h"
  4. #include "../net-asio/asio_file_stream.h"
  5. #include "../net-asio/ipx_conn.h"
  6. #include <boost/bind.hpp>
  7. #include <boost/date_time/posix_time/posix_time.hpp>
  8. #include "../util/StrFormat.h"
  9. //typedef boost::asio::ip::udp proto;
  10. typedef boost::asio::ipx proto;
  11. using namespace std;
  12. uint64 rnum(0);
  13. uint64 snum(0);
  14. #include <boost/signal.hpp>
  15. #if 0
  16. class UDPHolePunchingSocket {
  17. private:
  18. Handler handler;
  19. QueryUDPHoleAdress* othis;
  20. public:
  21. static boost::asio::ip::address getAdress();
  22. template <typename TheHandler>
  23. static void getAdress(const TheHandler& handler)
  24. {
  25. QueryUDPHoleAdress<TheHandler>* qobj = new QueryUDPHoleAdress<TheHandler>()
  26. }
  27. };
  28. #endif
  29. void timertick(boost::asio::deadline_timer* timer, const boost::system::error_code& e = boost::system::error_code())
  30. {
  31. if (e) return;
  32. cout << "tick: " << snum << " - " << rnum << "\n";
  33. rnum = 0;
  34. snum = 0;
  35. timer->expires_from_now(boost::posix_time::milliseconds(1000));
  36. timer->async_wait(boost::bind(&timertick, timer, _1));
  37. }
  38. proto::endpoint rep;
  39. proto::endpoint sep;
  40. uint8 rbuf[1406];
  41. uint8 sbuf[1406];
  42. void recvpacket(proto::socket* sock, const boost::system::error_code& e = boost::system::error_code(), size_t len = 0)
  43. {
  44. if (e) return;
  45. rnum += len;
  46. sock->async_receive_from(boost::asio::buffer(rbuf), rep,
  47. boost::bind(&recvpacket, sock, _1, _2));
  48. }
  49. void sendpacket(proto::socket* sock, const boost::system::error_code& e = boost::system::error_code(), size_t len = 0)
  50. {
  51. if (e) return;
  52. snum += len;
  53. /*
  54. sock->send_to(boost::asio::buffer(sbuf), sep);
  55. snum += 1406;
  56. sock->send_to(boost::asio::buffer(sbuf), sep);
  57. snum += 1406;
  58. sock->send_to(boost::asio::buffer(sbuf), sep);
  59. snum += 1406;
  60. sock->send_to(boost::asio::buffer(sbuf), sep);
  61. snum += 1406;
  62. sock->send_to(boost::asio::buffer(sbuf), sep);
  63. snum += 1406;
  64. */
  65. sock->async_send_to(boost::asio::buffer(sbuf), sep,
  66. boost::bind(&sendpacket, sock, _1, _2));
  67. }
  68. void listipxif()
  69. {
  70. #if 0
  71. SOCKET sd = WSASocket(AF_IPX, SOCK_DGRAM, NSPROTO_IPX, 0, 0, 0);
  72. if (sd == SOCKET_ERROR) {
  73. cerr << "Failed to get a socket. Error " << WSAGetLastError() <<
  74. endl;
  75. return;
  76. //return rtype(result.begin(), result.end());
  77. }
  78. //INTERFACE_INFO InterfaceList[20];
  79. unsigned long nBytesReturned;
  80. struct _SOCKET_ADDRESS_LIST {
  81. INT iAddressCount;
  82. SOCKET_ADDRESS Address[20];
  83. } SOCKET_ADDRESS_LIST;
  84. if (WSAIoctl(sd, SIO_ADDRESS_LIST_QUERY, 0, 0, &SOCKET_ADDRESS_LIST,
  85. sizeof(SOCKET_ADDRESS_LIST), &nBytesReturned, 0, 0) == SOCKET_ERROR) {
  86. cout << "Failed calling WSAIoctl: error " << WSAGetLastError() <<
  87. endl;
  88. return;// rtype(result.begin(), result.end());
  89. }
  90. int nNumInterfaces = nBytesReturned / sizeof(INTERFACE_INFO);
  91. //cout << "There are " << nNumInterfaces << " interfaces:" << endl;
  92. for (int i = 0; i < SOCKET_ADDRESS_LIST.iAddressCount; ++i) {
  93. boost::asio::ipx::endpoint nep;
  94. if ((int)nep.capacity() <= SOCKET_ADDRESS_LIST.Address[i].iSockaddrLength)
  95. memcpy(nep.data(), &SOCKET_ADDRESS_LIST.Address[i], SOCKET_ADDRESS_LIST.Address[i].iSockaddrLength);
  96. boost::asio::ipx::address naddr = nep.getAddress();
  97. cout << STRFORMAT("%02x:%02x:%02x:%02x", (int)naddr.network[0], (int)naddr.network[1], (int)naddr.network[2], (int)naddr.network[3]);
  98. cout << '-';
  99. cout << STRFORMAT("%02x:%02x:%02x:%02x:%02x:%02x", (int)naddr.node[0], (int)naddr.node[1], (int)naddr.node[2], (int)naddr.node[3], (int)naddr.node[4], (int)naddr.node[5]);
  100. cout << '\n';
  101. /*
  102. if (!(nFlags & IFF_LOOPBACK) && (nFlags & IFF_BROADCAST)) {
  103. for (int i = 0; i < 4; ++i)
  104. ifaddr[i] |= ~nmaddr[i];
  105. boost::asio::ip::address_v4 naddr(ifaddr);
  106. result.insert(naddr);
  107. cout << "broadcast: " << naddr << '\n';
  108. }
  109. */
  110. }
  111. //return 0;
  112. #endif
  113. }
  114. int imain( int argc, char **argv )
  115. {
  116. listipxif();
  117. boost::asio::io_service service;
  118. boost::asio::io_service::work work(service);
  119. services::diskio_service diskio(service);
  120. proto::socket socket(service);
  121. boost::asio::deadline_timer timer(service);
  122. timertick(&timer);
  123. "dd:00:0e:00-00:00:a4:fc:dd:00";
  124. "dd:00:0e:00-00:00:b4:fc:dd:00";
  125. "dd:00:0e:00-00:00:c4:fc:dd:00";
  126. "dd:00:0e:00-00:00:d4:fc:dd:00";
  127. "dd:00:0e:00-00:00:06:00:12:34";
  128. boost::asio::ip::udp::endpoint stunaddr(boost::asio::ip::address::from_string("83.103.82.85"),3478);
  129. boost::asio::ip::udp::endpoint raddr;
  130. boost::asio::ip::udp::socket stunsock(service);
  131. stunsock.open(stunaddr.protocol());
  132. sbuf[ 0] = 0;
  133. sbuf[ 1] = 1;
  134. sbuf[ 2] = 0;
  135. sbuf[ 3] = 8;
  136. sbuf[13] = 11;
  137. sbuf[14] = 12;
  138. sbuf[20] = 0;
  139. sbuf[21] = 3;
  140. sbuf[22] = 0;
  141. sbuf[23] = 4;
  142. sbuf[24] = 0;
  143. sbuf[25] = 0;
  144. sbuf[26] = 0;
  145. sbuf[27] = 6;
  146. // char xx="\x00\x01\x00\x08 \x0B\x0C \x00\x03\x00\x04\x00\x00\x00\x06";
  147. //stunsock.send_to(boost::asio::buffer("\x00\x01\x00\x08 \x0B\x0C \x00\x03\x00\x04\x00\x00\x00\x06", 28), stunaddr);
  148. stunsock.send_to(boost::asio::buffer("\x00\x01\x00\x00 \x0B\x0C ", 20), stunaddr);
  149. stunsock.receive_from(boost::asio::buffer(rbuf), raddr);
  150. cout << "from: " << raddr << '\n';
  151. int len = (rbuf[2] << 8) | (rbuf[3] << 0);
  152. int idx = 20;
  153. while (idx-20 < len) {
  154. int type = 0;
  155. type |= (rbuf[idx++] << 8);
  156. type |= (rbuf[idx++] << 0);
  157. int tlen = 0;
  158. tlen |= (rbuf[idx++] << 8);
  159. tlen |= (rbuf[idx++] << 0);
  160. switch (type) {
  161. case 1: {
  162. cout << "MAPPED-ADDRESS: " << (int)rbuf[idx+4] << '.' << (int)rbuf[idx+5] << '.' << (int)rbuf[idx+6] << '.' << (int)rbuf[idx+7] << ':';
  163. cout << ((rbuf[idx+2] << 8) | (rbuf[idx+3] << 0));
  164. cout << '\n';
  165. }; break;
  166. case 4: {
  167. cout << "SOURCE-ADDRESS: " << (int)rbuf[idx+4] << '.' << (int)rbuf[idx+5] << '.' << (int)rbuf[idx+6] << '.' << (int)rbuf[idx+7] << ':';
  168. cout << ((rbuf[idx+2] << 8) | (rbuf[idx+3] << 0));
  169. cout << '\n';
  170. }; break;
  171. case 5: {
  172. cout << "CHANGED-ADDRESS: " << (int)rbuf[idx+4] << '.' << (int)rbuf[idx+5] << '.' << (int)rbuf[idx+6] << '.' << (int)rbuf[idx+7] << ':';
  173. cout << ((rbuf[idx+2] << 8) | (rbuf[idx+3] << 0));
  174. cout << '\n';
  175. }; break;
  176. default: cout << "type: " << type << '\n';
  177. }
  178. idx += tlen;
  179. }
  180. //boost::asio::ipx::address ipxaddr1 = {{0xdd, 0x00, 0x0e, 0x00}, {0x00, 0x00, 0xa4, 0xfd, 0xdd, 0x00}};
  181. //boost::asio::ipx::address ipxaddr1 = {{0xdd, 0x00, 0x0e, 0x00}, {0x00, 0x00, 0xb4, 0xfd, 0xdd, 0x00}};
  182. //boost::asio::ipx::address ipxaddr1 = {{0xdd, 0x00, 0x0e, 0x00}, {0x00, 0x00, 0xc4, 0xfd, 0xdd, 0x00}};
  183. //boost::asio::ipx::address ipxaddr1 = {{0xdd, 0x00, 0x0e, 0x00}, {0x00, 0x00, 0xd4, 0xfd, 0xdd, 0x00}};
  184. boost::asio::ipx::address ipxaddr1 = {{0xdd, 0x00, 0x0e, 0x00}, {0x00, 0x00, 0x06, 0x00, 0x12, 0x34}};
  185. sep = proto::endpoint(ipxaddr1, 33433);
  186. // ipxaddr = ;
  187. //sep = proto::endpoint(proto::address::broadcast(), 33433);//socket.local_endpoint();
  188. //sep = proto::endpoint(boost::asio::ip::address_v4::broadcast(), 33433);//socket.local_endpoint();
  189. //sep = proto::endpoint(boost::asio::ip::address::from_string("127.0.0.1"), 33433);
  190. //sep = proto::endpoint(boost::asio::ip::address::from_string("192.168.0.255"), 33433);
  191. //sep = proto::endpoint(boost::asio::ip::address::from_string("255.255.255.255"), 33433);
  192. //sep = proto::endpoint(boost::asio::ip::address::from_string("192.168.0.24"), 33433);
  193. //sep = proto::endpoint(boost::asio::ip::address::from_string("192.168.0.42"), 33433);
  194. socket.open(sep.protocol());
  195. //socket.bind(proto::endpoint(ipxaddr1, 33433));
  196. //socket.bind(proto::endpoint(boost::asio::ip::address::from_string("192.168.0.42"), 33433));
  197. //socket.bind(proto::endpoint(proto::address::local(), 33433));
  198. //socket.bind(proto::endpoint(boost::asio::ip::address_v4(), 33433));
  199. socket.set_option(proto::socket::broadcast(true));
  200. recvpacket(&socket);
  201. recvpacket(&socket);
  202. recvpacket(&socket);
  203. recvpacket(&socket);
  204. sendpacket(&socket);
  205. sendpacket(&socket);
  206. sendpacket(&socket);
  207. sendpacket(&socket);
  208. boost::thread srthread(boost::bind(&boost::asio::io_service::run, &service));
  209. string str;
  210. cin >> str;
  211. return 0;
  212. }
  213. #endif
  214. int main( int argc, char **argv ) {
  215. #if 0
  216. try {
  217. return imain(argc, argv);
  218. } catch (std::exception& e) {
  219. cout << "exception: " << e.what() << '\n';
  220. }
  221. #endif
  222. }