You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2005, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/asyncudpsocket.h"
  28. #include "talk/base/logging.h"
  29. namespace talk_base {
  30. static const int BUF_SIZE = 64 * 1024;
  31. AsyncUDPSocket* AsyncUDPSocket::Create(
  32. AsyncSocket* socket,
  33. const SocketAddress& bind_address) {
  34. scoped_ptr<AsyncSocket> owned_socket(socket);
  35. if (socket->Bind(bind_address) < 0) {
  36. LOG(LS_ERROR) << "Bind() failed with error " << socket->GetError();
  37. return NULL;
  38. }
  39. return new AsyncUDPSocket(owned_socket.release());
  40. }
  41. AsyncUDPSocket* AsyncUDPSocket::Create(SocketFactory* factory,
  42. const SocketAddress& bind_address) {
  43. AsyncSocket* socket =
  44. factory->CreateAsyncSocket(bind_address.family(), SOCK_DGRAM);
  45. if (!socket)
  46. return NULL;
  47. return Create(socket, bind_address);
  48. }
  49. AsyncUDPSocket::AsyncUDPSocket(AsyncSocket* socket)
  50. : socket_(socket) {
  51. ASSERT(socket_);
  52. size_ = BUF_SIZE;
  53. buf_ = new char[size_];
  54. // The socket should start out readable but not writable.
  55. socket_->SignalReadEvent.connect(this, &AsyncUDPSocket::OnReadEvent);
  56. }
  57. AsyncUDPSocket::~AsyncUDPSocket() {
  58. delete [] buf_;
  59. }
  60. SocketAddress AsyncUDPSocket::GetLocalAddress() const {
  61. return socket_->GetLocalAddress();
  62. }
  63. SocketAddress AsyncUDPSocket::GetRemoteAddress() const {
  64. return socket_->GetRemoteAddress();
  65. }
  66. int AsyncUDPSocket::Send(const void *pv, size_t cb) {
  67. return socket_->Send(pv, cb);
  68. }
  69. int AsyncUDPSocket::SendTo(
  70. const void *pv, size_t cb, const SocketAddress& addr) {
  71. return socket_->SendTo(pv, cb, addr);
  72. }
  73. int AsyncUDPSocket::Close() {
  74. return socket_->Close();
  75. }
  76. AsyncUDPSocket::State AsyncUDPSocket::GetState() const {
  77. return STATE_BOUND;
  78. }
  79. int AsyncUDPSocket::GetOption(Socket::Option opt, int* value) {
  80. return socket_->GetOption(opt, value);
  81. }
  82. int AsyncUDPSocket::SetOption(Socket::Option opt, int value) {
  83. return socket_->SetOption(opt, value);
  84. }
  85. int AsyncUDPSocket::GetError() const {
  86. return socket_->GetError();
  87. }
  88. void AsyncUDPSocket::SetError(int error) {
  89. return socket_->SetError(error);
  90. }
  91. void AsyncUDPSocket::OnReadEvent(AsyncSocket* socket) {
  92. ASSERT(socket_.get() == socket);
  93. SocketAddress remote_addr;
  94. int len = socket_->RecvFrom(buf_, size_, &remote_addr);
  95. if (len < 0) {
  96. // An error here typically means we got an ICMP error in response to our
  97. // send datagram, indicating the remote address was unreachable.
  98. // When doing ICE, this kind of thing will often happen.
  99. // TODO: Do something better like forwarding the error to the user.
  100. SocketAddress local_addr = socket_->GetLocalAddress();
  101. LOG(LS_INFO) << "AsyncUDPSocket[" << local_addr.ToString() << "] "
  102. << "receive failed with error " << socket_->GetError();
  103. return;
  104. }
  105. // TODO: Make sure that we got all of the packet.
  106. // If we did not, then we should resize our buffer to be large enough.
  107. SignalReadPacket(this, buf_, (size_t)len, remote_addr);
  108. }
  109. } // namespace talk_base