Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / p2p / socket_host_udp.cc
index db17c5f..3833b62 100644 (file)
@@ -15,7 +15,7 @@
 #include "net/base/io_buffer.h"
 #include "net/base/net_errors.h"
 #include "net/base/net_util.h"
-#include "third_party/libjingle/source/talk/base/asyncpacketsocket.h"
+#include "third_party/webrtc/base/asyncpacketsocket.h"
 
 namespace {
 
@@ -41,7 +41,8 @@ bool IsTransientError(int error) {
          error == net::ERR_ADDRESS_INVALID ||
          error == net::ERR_ACCESS_DENIED ||
          error == net::ERR_CONNECTION_RESET ||
-         error == net::ERR_OUT_OF_MEMORY;
+         error == net::ERR_OUT_OF_MEMORY ||
+         error == net::ERR_INTERNET_DISCONNECTED;
 }
 
 }  // namespace
@@ -51,7 +52,7 @@ namespace content {
 P2PSocketHostUdp::PendingPacket::PendingPacket(
     const net::IPEndPoint& to,
     const std::vector<char>& content,
-    const talk_base::PacketOptions& options,
+    const rtc::PacketOptions& options,
     uint64 id)
     : to(to),
       data(new net::IOBuffer(content.size())),
@@ -65,12 +66,12 @@ P2PSocketHostUdp::PendingPacket::~PendingPacket() {
 }
 
 P2PSocketHostUdp::P2PSocketHostUdp(IPC::Sender* message_sender,
-                                   int id,
+                                   int socket_id,
                                    P2PMessageThrottler* throttler)
-    : P2PSocketHost(message_sender, id),
-      socket_(new net::UDPServerSocket(
-          GetContentClient()->browser()->GetNetLog(),
-          net::NetLog::Source())),
+    : P2PSocketHost(message_sender, socket_id),
+      socket_(
+          new net::UDPServerSocket(GetContentClient()->browser()->GetNetLog(),
+                                   net::NetLog::Source())),
       send_pending_(false),
       last_dscp_(net::DSCP_CS0),
       throttler_(throttler) {
@@ -95,7 +96,7 @@ bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address,
   }
 
   // Setting recv socket buffer size.
-  if (!socket_->SetReceiveBufferSize(kRecvSocketBufferSize)) {
+  if (socket_->SetReceiveBufferSize(kRecvSocketBufferSize) != net::OK) {
     LOG(WARNING) << "Failed to set socket receive buffer size to "
                  << kRecvSocketBufferSize;
   }
@@ -112,7 +113,9 @@ bool P2PSocketHostUdp::Init(const net::IPEndPoint& local_address,
 
   state_ = STATE_OPEN;
 
-  message_sender_->Send(new P2PMsg_OnSocketCreated(id_, address));
+  // NOTE: Remote address will be same as what renderer provided.
+  message_sender_->Send(new P2PMsg_OnSocketCreated(
+      id_, address, remote_address.ip_address));
 
   recv_buffer_ = new net::IOBuffer(kReadBufferSize);
   DoRead();
@@ -172,6 +175,9 @@ void P2PSocketHostUdp::HandleReadResult(int result) {
 
     message_sender_->Send(new P2PMsg_OnDataReceived(
         id_, recv_address_, data, base::TimeTicks::Now()));
+
+    if (dump_incoming_rtp_packet_)
+      DumpRtpPacket(&data[0], data.size(), true);
   } else if (result < 0 && !IsTransientError(result)) {
     LOG(ERROR) << "Error when reading from UDP socket: " << result;
     OnError();
@@ -180,7 +186,7 @@ void P2PSocketHostUdp::HandleReadResult(int result) {
 
 void P2PSocketHostUdp::Send(const net::IPEndPoint& to,
                             const std::vector<char>& data,
-                            const talk_base::PacketOptions& options,
+                            const rtc::PacketOptions& options,
                             uint64 packet_id) {
   if (!socket_) {
     // The Send message may be sent after the an OnError message was
@@ -260,6 +266,9 @@ void P2PSocketHostUdp::DoSend(const PendingPacket& packet) {
   } else {
     HandleSendResult(packet.id, result);
   }
+
+  if (dump_outgoing_rtp_packet_)
+    DumpRtpPacket(packet.data->data(), packet.size, false);
 }
 
 void P2PSocketHostUdp::OnSend(uint64 packet_id, int result) {
@@ -280,15 +289,16 @@ void P2PSocketHostUdp::OnSend(uint64 packet_id, int result) {
 void P2PSocketHostUdp::HandleSendResult(uint64 packet_id, int result) {
   TRACE_EVENT_ASYNC_END1("p2p", "Send", packet_id,
                          "result", result);
-  if (result > 0) {
-    message_sender_->Send(new P2PMsg_OnSendComplete(id_));
-  } else if (IsTransientError(result)) {
+  if (result < 0) {
+    if (!IsTransientError(result)) {
+      LOG(ERROR) << "Error when sending data in UDP socket: " << result;
+      OnError();
+      return;
+    }
     VLOG(0) << "sendto() has failed twice returning a "
-        " transient error. Dropping the packet.";
-  } else if (result < 0) {
-    LOG(ERROR) << "Error when sending data in UDP socket: " << result;
-    OnError();
+               " transient error. Dropping the packet.";
   }
+  message_sender_->Send(new P2PMsg_OnSendComplete(id_));
 }
 
 P2PSocketHost* P2PSocketHostUdp::AcceptIncomingTcpConnection(
@@ -302,9 +312,9 @@ bool P2PSocketHostUdp::SetOption(P2PSocketOption option, int value) {
   DCHECK_EQ(STATE_OPEN, state_);
   switch (option) {
     case P2P_SOCKET_OPT_RCVBUF:
-      return socket_->SetReceiveBufferSize(value);
+      return socket_->SetReceiveBufferSize(value) == net::OK;
     case P2P_SOCKET_OPT_SNDBUF:
-      return socket_->SetSendBufferSize(value);
+      return socket_->SetSendBufferSize(value) == net::OK;
     case P2P_SOCKET_OPT_DSCP:
       return (net::OK == socket_->SetDiffServCodePoint(
           static_cast<net::DiffServCodePoint>(value))) ? true : false;