Skip to content

Commit

Permalink
Fix iOS return-from-idle crash: reopen socket
Browse files Browse the repository at this point in the history
  • Loading branch information
ngocdh committed Jun 7, 2021
1 parent 114460e commit 99a6230
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
/* Implementation *************************************************************/
void CSocket::Init ( const quint16 iPortNumber, const quint16 iQosNumber, const QString& strServerBindIP )
{
// first store parameters, in case reinit is required (mostly for iOS)
iPortNumber_ = iPortNumber;
iQosNumber_ = iQosNumber;
strServerBindIP_ = strServerBindIP;

#ifdef _WIN32
// for the Windows socket usage we have to start it up first

Expand Down Expand Up @@ -121,6 +126,7 @@ void CSocket::Init ( const quint16 iPortNumber, const quint16 iQosNumber, const
// Connections -------------------------------------------------------------
// it is important to do the following connections in this class since we
// have a thread transition
if ( bIsInitRan ) return;

// we have different connections for client and server
if ( bIsClient )
Expand Down Expand Up @@ -148,6 +154,8 @@ void CSocket::Init ( const quint16 iPortNumber, const quint16 iQosNumber, const

QObject::connect ( this, &CSocket::ServerFull, pServer, &CServer::OnServerFull );
}

bIsInitRan = true; // QObject::connect once only
}

void CSocket::Close()
Expand Down Expand Up @@ -194,12 +202,24 @@ void CSocket::SendPacket ( const CVector<uint8_t>& vecbySendBuf, const CHostAddr
UdpSocketOutAddr.sin_port = htons ( HostAddr.iPort );
UdpSocketOutAddr.sin_addr.s_addr = htonl ( HostAddr.InetAddr.toIPv4Address() );

sendto ( UdpSocket,
(const char*) &( (CVector<uint8_t>) vecbySendBuf )[0],
iVecSizeOut,
0,
(sockaddr*) &UdpSocketOutAddr,
sizeof ( sockaddr_in ) );
if (
sendto ( UdpSocket,
(const char*) &( (CVector<uint8_t>) vecbySendBuf )[0],
iVecSizeOut,
0,
(sockaddr*) &UdpSocketOutAddr,
sizeof ( sockaddr_in ) )
< 0 )
{
// qDebug("Socket send exception - mostly happens in iOS when returning from idle");
Init( iPortNumber_, iQosNumber_, strServerBindIP_ ); // reinit
sendto ( UdpSocket,
(const char*) &( (CVector<uint8_t>) vecbySendBuf )[0],
iVecSizeOut,
0,
(sockaddr*) &UdpSocketOutAddr,
sizeof ( sockaddr_in ) );
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class CSocket : public QObject

protected:
void Init ( const quint16 iPortNumber, const quint16 iQosNumber, const QString& strServerBindIP );
quint16 iPortNumber_;
quint16 iQosNumber_;
QString strServerBindIP_;
bool bIsInitRan;

#ifdef _WIN32
SOCKET UdpSocket;
Expand Down

0 comments on commit 99a6230

Please sign in to comment.