diff --git a/src/socket.cpp b/src/socket.cpp index 171a99ca9e..7f63ce2ca2 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -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 @@ -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 ) @@ -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() @@ -194,12 +202,24 @@ void CSocket::SendPacket ( const CVector& vecbySendBuf, const CHostAddr UdpSocketOutAddr.sin_port = htons ( HostAddr.iPort ); UdpSocketOutAddr.sin_addr.s_addr = htonl ( HostAddr.InetAddr.toIPv4Address() ); - sendto ( UdpSocket, - (const char*) &( (CVector) vecbySendBuf )[0], - iVecSizeOut, - 0, - (sockaddr*) &UdpSocketOutAddr, - sizeof ( sockaddr_in ) ); + if ( + sendto ( UdpSocket, + (const char*) &( (CVector) 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) vecbySendBuf )[0], + iVecSizeOut, + 0, + (sockaddr*) &UdpSocketOutAddr, + sizeof ( sockaddr_in ) ); + } } } diff --git a/src/socket.h b/src/socket.h index bbf6c49880..d4d9714492 100644 --- a/src/socket.h +++ b/src/socket.h @@ -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;