@@ -68,13 +68,13 @@ P2PSocketTcpBase::P2PSocketTcpBase(
6868
6969P2PSocketTcpBase::~P2PSocketTcpBase () = default ;
7070
71- void P2PSocketTcpBase::InitAccepted (const net::IPEndPoint& remote_address,
71+ bool P2PSocketTcpBase::InitAccepted (const net::IPEndPoint& remote_address,
7272 std::unique_ptr<net::StreamSocket> socket) {
7373 DCHECK (socket);
7474 remote_address_.ip_address = remote_address;
7575 // TODO(ronghuawu): Add FakeSSLServerSocket.
7676 socket_ = std::move (socket);
77- DoRead ();
77+ return DoRead ();
7878}
7979
8080void P2PSocketTcpBase::Init (
@@ -123,15 +123,18 @@ void P2PSocketTcpBase::OnConnected(int result) {
123123 DCHECK_NE (result, net::ERR_IO_PENDING);
124124
125125 if (result != net::OK) {
126- LOG (WARNING) << " Error from connecting socket, result=" << result;
126+ LOG (WARNING) << " Error from connecting socket, result=" << result
127+ << " , destroying socket" ;
127128 OnError ();
128129 return ;
129130 }
130131
131- OnOpen ();
132+ if (!OnOpen ()) {
133+ LOG (ERROR) << " Socket destroyed in OnConnected/OnOpen" ;
134+ }
132135}
133136
134- void P2PSocketTcpBase::OnOpen () {
137+ bool P2PSocketTcpBase::OnOpen () {
135138 // Setting socket send and receive buffer size.
136139 if (net::OK != socket_->SetReceiveBufferSize (kTcpRecvSocketBufferSize )) {
137140 LOG (WARNING) << " Failed to set socket receive buffer size to "
@@ -144,9 +147,9 @@ void P2PSocketTcpBase::OnOpen() {
144147 }
145148
146149 if (!DoSendSocketCreateMsg ())
147- return ;
150+ return false ;
148151
149- DoRead ();
152+ return DoRead ();
150153}
151154
152155bool P2PSocketTcpBase::DoSendSocketCreateMsg () {
@@ -193,7 +196,7 @@ bool P2PSocketTcpBase::DoSendSocketCreateMsg() {
193196 return true ;
194197}
195198
196- void P2PSocketTcpBase::DoRead () {
199+ bool P2PSocketTcpBase::DoRead () {
197200 while (true ) {
198201 if (!read_buffer_.get ()) {
199202 read_buffer_ = base::MakeRefCounted<net::GrowableIOBuffer>();
@@ -209,14 +212,23 @@ void P2PSocketTcpBase::DoRead() {
209212 const int result = socket_->Read (
210213 read_buffer_.get (), read_buffer_->RemainingCapacity (),
211214 base::BindOnce (&P2PSocketTcp::OnRead, base::Unretained (this )));
212- if (result == net::ERR_IO_PENDING || !HandleReadResult (result))
213- return ;
215+ if (result == net::ERR_IO_PENDING) {
216+ return true ; // not finished, but blocked
217+ }
218+ if (!HandleReadResult (result)) {
219+ return false ; // error, socket deleted
220+ }
214221 }
215222}
216223
217224void P2PSocketTcpBase::OnRead (int result) {
218- if (HandleReadResult (result))
219- DoRead ();
225+ if (!HandleReadResult (result)) {
226+ LOG (ERROR) << " OnRead/HandleReadResult reports socket destroyed" ;
227+ return ;
228+ }
229+ if (!DoRead ()) {
230+ LOG (ERROR) << " OnRead/DoRead reports socket destroyed" ;
231+ }
220232}
221233
222234bool P2PSocketTcpBase::OnPacket (base::span<const uint8_t > data) {
@@ -256,17 +268,17 @@ bool P2PSocketTcpBase::OnPacket(base::span<const uint8_t> data) {
256268 return true ;
257269}
258270
259- void P2PSocketTcpBase::WriteOrQueue (SendBuffer& send_buffer) {
271+ bool P2PSocketTcpBase::WriteOrQueue (SendBuffer& send_buffer) {
260272 if (write_buffer_.buffer .get ()) {
261273 write_queue_.push (send_buffer);
262- return ;
274+ return true ;
263275 }
264276
265277 write_buffer_ = send_buffer;
266- DoWrite ();
278+ return DoWrite ();
267279}
268280
269- void P2PSocketTcpBase::DoWrite () {
281+ bool P2PSocketTcpBase::DoWrite () {
270282 while (!write_pending_ && write_buffer_.buffer .get ()) {
271283 int result = socket_->Write (
272284 write_buffer_.buffer .get (), write_buffer_.buffer ->BytesRemaining (),
@@ -276,9 +288,10 @@ void P2PSocketTcpBase::DoWrite() {
276288 if (result == net::ERR_IO_PENDING) {
277289 write_pending_ = true ;
278290 } else if (!HandleWriteResult (result)) {
279- break ;
291+ return false ; // Error, socket is destroyed.
280292 }
281293 }
294+ return true ;
282295}
283296
284297void P2PSocketTcpBase::OnWritten (int result) {
@@ -287,8 +300,13 @@ void P2PSocketTcpBase::OnWritten(int result) {
287300
288301 write_pending_ = false ;
289302
290- if (HandleWriteResult (result))
291- DoWrite ();
303+ if (!HandleWriteResult (result)) {
304+ LOG (ERROR) << " Socket destroyed in OnWritten/HandleWriteResult" ;
305+ return ;
306+ }
307+ if (!DoWrite ()) {
308+ LOG (ERROR) << " Socket destroyed in OnWritten/DoWrite" ;
309+ }
292310}
293311
294312bool P2PSocketTcpBase::HandleWriteResult (int result) {
@@ -376,13 +394,14 @@ bool P2PSocketTcpBase::SendPacket(base::span<const uint8_t> data,
376394 }
377395 }
378396
379- DoSend (packet_info.destination , data, packet_info.packet_options );
380- return true ;
397+ return DoSend (packet_info.destination , data, packet_info.packet_options );
381398}
382399
383400void P2PSocketTcpBase::Send (base::span<const uint8_t > data,
384401 const P2PPacketInfo& packet_info) {
385- SendPacket (data, packet_info);
402+ if (!SendPacket (data, packet_info)) {
403+ LOG (ERROR) << " Socket destroyed while sending" ;
404+ }
386405}
387406
388407void P2PSocketTcpBase::SendBatch (
@@ -448,7 +467,7 @@ bool P2PSocketTcp::ProcessInput(base::span<const uint8_t> input,
448467 return OnPacket (input.subspan (kPacketHeaderSize , packet_size));
449468}
450469
451- void P2PSocketTcp::DoSend (const net::IPEndPoint& to,
470+ bool P2PSocketTcp::DoSend (const net::IPEndPoint& to,
452471 base::span<const uint8_t > data,
453472 const rtc::PacketOptions& options) {
454473 int buffer_size = kPacketHeaderSize + data.size ();
@@ -467,7 +486,7 @@ void P2PSocketTcp::DoSend(const net::IPEndPoint& to,
467486 send_buffer.buffer ->BytesRemaining () - kPacketHeaderSize ,
468487 options.packet_time_params , rtc::TimeMicros ());
469488
470- WriteOrQueue (send_buffer);
489+ return WriteOrQueue (send_buffer);
471490}
472491
473492// P2PSocketStunTcp
@@ -508,15 +527,15 @@ bool P2PSocketStunTcp::ProcessInput(base::span<const uint8_t> input,
508527 return OnPacket (input.subspan (0 , packet_size));
509528}
510529
511- void P2PSocketStunTcp::DoSend (const net::IPEndPoint& to,
530+ bool P2PSocketStunTcp::DoSend (const net::IPEndPoint& to,
512531 base::span<const uint8_t > data,
513532 const rtc::PacketOptions& options) {
514533 // Each packet is expected to have header (STUN/TURN ChannelData), where
515534 // header contains message type and and length of message.
516535 if (data.size () < kPacketHeaderSize + kPacketLengthOffset ) {
517536 NOTREACHED ();
518537 OnError ();
519- return ;
538+ return false ;
520539 }
521540
522541 int pad_bytes;
@@ -526,7 +545,7 @@ void P2PSocketStunTcp::DoSend(const net::IPEndPoint& to,
526545 if (data.size () != expected_len) {
527546 NOTREACHED ();
528547 OnError ();
529- return ;
548+ return false ;
530549 }
531550
532551 // Add any pad bytes to the total size.
@@ -554,7 +573,7 @@ void P2PSocketStunTcp::DoSend(const net::IPEndPoint& to,
554573 data.size ()),
555574 false );
556575
557- WriteOrQueue (send_buffer);
576+ return WriteOrQueue (send_buffer);
558577}
559578
560579int P2PSocketStunTcp::GetExpectedPacketSize (base::span<const uint8_t > data,
0 commit comments