utp_api.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // vim:set ts=4 sw=4 ai:
  2. /*
  3. * Copyright (c) 2010-2013 BitTorrent, Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <stdio.h>
  24. #include "utp_internal.h"
  25. #include "utp_utils.h"
  26. extern "C" {
  27. const char * utp_callback_names[] = {
  28. "UTP_ON_FIREWALL",
  29. "UTP_ON_ACCEPT",
  30. "UTP_ON_CONNECT",
  31. "UTP_ON_ERROR",
  32. "UTP_ON_READ",
  33. "UTP_ON_OVERHEAD_STATISTICS",
  34. "UTP_ON_STATE_CHANGE",
  35. "UTP_GET_READ_BUFFER_SIZE",
  36. "UTP_ON_DELAY_SAMPLE",
  37. "UTP_GET_UDP_MTU",
  38. "UTP_GET_UDP_OVERHEAD",
  39. "UTP_GET_MILLISECONDS",
  40. "UTP_GET_MICROSECONDS",
  41. "UTP_GET_RANDOM",
  42. "UTP_LOG",
  43. "UTP_SENDTO",
  44. };
  45. const char * utp_error_code_names[] = {
  46. "UTP_ECONNREFUSED",
  47. "UTP_ECONNRESET",
  48. "UTP_ETIMEDOUT",
  49. };
  50. const char *utp_state_names[] = {
  51. NULL,
  52. "UTP_STATE_CONNECT",
  53. "UTP_STATE_WRITABLE",
  54. "UTP_STATE_EOF",
  55. "UTP_STATE_DESTROYING",
  56. };
  57. struct_utp_context::struct_utp_context()
  58. : userdata(NULL)
  59. , current_ms(0)
  60. , last_utp_socket(NULL)
  61. , log_normal(false)
  62. , log_mtu(false)
  63. , log_debug(false)
  64. {
  65. memset(&context_stats, 0, sizeof(context_stats));
  66. memset(callbacks, 0, sizeof(callbacks));
  67. target_delay = CCONTROL_TARGET;
  68. utp_sockets = new UTPSocketHT;
  69. callbacks[UTP_GET_UDP_MTU] = &utp_default_get_udp_mtu;
  70. callbacks[UTP_GET_UDP_OVERHEAD] = &utp_default_get_udp_overhead;
  71. callbacks[UTP_GET_MILLISECONDS] = &utp_default_get_milliseconds;
  72. callbacks[UTP_GET_MICROSECONDS] = &utp_default_get_microseconds;
  73. callbacks[UTP_GET_RANDOM] = &utp_default_get_random;
  74. // 1 MB of receive buffer (i.e. max bandwidth delay product)
  75. // means that from a peer with 200 ms RTT, we cannot receive
  76. // faster than 5 MB/s
  77. // from a peer with 10 ms RTT, we cannot receive faster than
  78. // 100 MB/s. This is assumed to be good enough, since bandwidth
  79. // often is proportional to RTT anyway
  80. // when setting a download rate limit, all sockets should have
  81. // their receive buffer set much lower, to say 60 kiB or so
  82. opt_rcvbuf = opt_sndbuf = 1024 * 1024;
  83. last_check = 0;
  84. }
  85. struct_utp_context::~struct_utp_context() {
  86. delete this->utp_sockets;
  87. }
  88. utp_context* utp_init (int version)
  89. {
  90. assert(version == 2);
  91. if (version != 2)
  92. return NULL;
  93. utp_context *ctx = new utp_context;
  94. return ctx;
  95. }
  96. void utp_destroy(utp_context *ctx) {
  97. assert(ctx);
  98. if (ctx) delete ctx;
  99. }
  100. void utp_set_callback(utp_context *ctx, int callback_name, utp_callback_t *proc) {
  101. assert(ctx);
  102. if (ctx) ctx->callbacks[callback_name] = proc;
  103. }
  104. void* utp_context_set_userdata(utp_context *ctx, void *userdata) {
  105. assert(ctx);
  106. if (ctx) ctx->userdata = userdata;
  107. return ctx ? ctx->userdata : NULL;
  108. }
  109. void* utp_context_get_userdata(utp_context *ctx) {
  110. assert(ctx);
  111. return ctx ? ctx->userdata : NULL;
  112. }
  113. utp_context_stats* utp_get_context_stats(utp_context *ctx) {
  114. assert(ctx);
  115. return ctx ? &ctx->context_stats : NULL;
  116. }
  117. ssize_t utp_write(utp_socket *socket, void *buf, size_t len) {
  118. struct utp_iovec iovec = { buf, len };
  119. return utp_writev(socket, &iovec, 1);
  120. }
  121. }