From f016e9e022a2c870914d2088ce99ad70dced7bdc Mon Sep 17 00:00:00 2001 From: Volker Fischer Date: Mon, 6 Jan 2014 15:57:40 +0000 Subject: [PATCH] avoid allocating memory in time critical client thread -> improve socket send function in the channel --- src/buffer.h | 2 +- src/channel.cpp | 10 +++------- src/channel.h | 6 ++++-- src/client.cpp | 3 +-- src/server.cpp | 4 +--- src/socket.h | 10 +++++----- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/buffer.h b/src/buffer.h index b7563c4c..903604f7 100755 --- a/src/buffer.h +++ b/src/buffer.h @@ -469,7 +469,7 @@ public: return false; } - CVector Get() + CVector& Get() { iPutPos = 0; return vecsMemory; diff --git a/src/channel.cpp b/src/channel.cpp index 6ee79324..b19e0364 100755 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -621,7 +621,8 @@ EGetDataStat CChannel::GetData ( CVector& vecbyData ) return eGetStatus; } -CVector CChannel::PrepSendPacket ( const CVector& vecbyNPacket ) +void CChannel::PrepAndSendPacket ( CSocket* pSocket, + const CVector& vecbyNPacket ) { QMutexLocker locker ( &Mutex ); @@ -629,13 +630,8 @@ CVector CChannel::PrepSendPacket ( const CVector& vecbyNPacket // block size if ( ConvBuf.Put ( vecbyNPacket ) ) { - // a packet is ready - return ConvBuf.Get(); + pSocket->SendPacket ( ConvBuf.Get(), GetAddress() ); } - - // if the block is not ready we have to initialize with zero length to - // tell the following network send routine that nothing should be sent - return CVector ( 0 ); } int CChannel::GetUploadRateKbps() diff --git a/src/channel.h b/src/channel.h index 22226ba7..2a88f7b2 100755 --- a/src/channel.h +++ b/src/channel.h @@ -33,6 +33,7 @@ #include "buffer.h" #include "util.h" #include "protocol.h" +#include "socket.h" /* Definitions ****************************************************************/ @@ -66,7 +67,8 @@ public: int iNumBytes ); EGetDataStat GetData ( CVector& vecbyData ); - CVector PrepSendPacket ( const CVector& vecbyNPacket ); + void PrepAndSendPacket ( CSocket* pSocket, + const CVector& vecbyNPacket ); void ResetTimeOutCounter() { iConTimeOut = iConTimeOutStartVal; } bool IsConnected() const { return iConTimeOut > 0; } @@ -77,7 +79,7 @@ public: void SetAddress ( const CHostAddress NAddr ) { InetAddr = NAddr; } bool GetAddress ( CHostAddress& RetAddr ); - CHostAddress GetAddress() const { return InetAddr; } + CHostAddress& GetAddress() { return InetAddr; } void ResetInfo() { ChannelInfo = CChannelCoreInfo(); } // reset does not emit a message void SetName ( const QString sNNa ); diff --git a/src/client.cpp b/src/client.cpp index 19bae273..f233fb48 100755 --- a/src/client.cpp +++ b/src/client.cpp @@ -1092,8 +1092,7 @@ void CClient::ProcessAudioDataIntern ( CVector& vecsStereoSndCrd ) } // send coded audio through the network - Socket.SendPacket ( Channel.PrepSendPacket ( vecCeltData ), - Channel.GetAddress() ); + Channel.PrepAndSendPacket ( &Socket, vecCeltData ); } diff --git a/src/server.cpp b/src/server.cpp index f7f8b974..fed072d0 100755 --- a/src/server.cpp +++ b/src/server.cpp @@ -898,9 +898,7 @@ opus_custom_encoder_ctl ( OpusEncoderStereo[iCurChanID], } // send separate mix to current clients - Socket.SendPacket ( - vecChannels[iCurChanID].PrepSendPacket ( vecCeltData ), - vecChannels[iCurChanID].GetAddress() ); + vecChannels[iCurChanID].PrepAndSendPacket ( &Socket, vecCeltData ); // update socket buffer size vecChannels[iCurChanID].UpdateSocketBufferSize(); diff --git a/src/socket.h b/src/socket.h index 473cee1f..c72ce4be 100755 --- a/src/socket.h +++ b/src/socket.h @@ -33,14 +33,14 @@ #include #include #include "global.h" -#include "channel.h" #include "protocol.h" #include "util.h" -// The header file server.h requires to include this header file so we get a -// cyclic dependency. To solve this issue, a prototype of the server class is -// defined here. -class CServer; // forward declaration of CServer +// The header files channel.h and server.h require to include this header file +// so we get a cyclic dependency. To solve this issue, a prototype of the +// channel class and server class is defined here. +class CServer; // forward declaration of CServer +class CChannel; // forward declaration of CChannel /* Definitions ****************************************************************/