I have tried and tried! the error come form the way I lock() and unlock() the mutex.
That is insted bufLock.lock()
, use std::unique_lock<std::mutex> mtx_lock(bufLock);
to lock the deque buffer.
Also Two variable_condition is needed to handle the cases of isEmpty and isFull to stop forwarding, adding to the buffer.
The final code if anyone finds it useful.
NOTE: this code eats a lot memory and consume the CPU, don't use it without revision. And WELCOME to help me with it :)
void UDPStreamSender::SendStream(const char* sendMsg, size_t size)
{
InitializeNewSendSession();
cout << "---- Send Stream starts... ----" << endl;
char* longMsg = new char[size];
memcpy(longMsg, sendMsg, size);
std::thread segThread(&UDPStreamSender::DoSegmentation, this, longMsg, size);
_isRunning = true;
std::thread sendThrad(&UDPStreamSender::SendBuffer, this);
std::thread ackThrad(&UDPStreamSender::AckRecive, this);
std::thread timeOutThread(&UDPStreamSender::ManageTimeout, this);
segThread.join();
sendThrad.join();
_isRunning = false;
ackThrad.join();
timeOutThread.join();
cout << "---- Send Stream done! ----" << endl;
}
void UDPStreamSender::DoSegmentation(const char* longMsg, unsigned int size)
{
Segment* cSeg = new Segment();
cSeg->seqNum = lastSeqNum;
msgLength = size;
msgSegLen = segLength - SEQ_NUM_LEN;
segmentsNumber = (unsigned int)ceil((float)msgLength / (msgSegLen));
for (size_t i = 0; i < segmentsNumber; i++)
{
cSeg->seqNum++;
lastSeqNum = cSeg->seqNum;
cSeg->data = new char[msgSegLen];
int sendMsgSegLen = msgSegLen;
if (i == segmentsNumber - 1)
sendMsgSegLen = msgLength - i*msgSegLen;
memcpy(cSeg->data, longMsg + i*msgSegLen, sendMsgSegLen);
// Add to send buffer
std::unique_lock<std::mutex> mtx_lock(bufLock);
while (sendBuffer->isFull())
{
stopifFull.wait(mtx_lock);
}
cSeg->isSent = false;
sendBuffer->Add(cSeg);
mtx_lock.unlock();
stopIfEmpty.notify_all();
}
}
int UDPStreamSender::SendSegment(const Segment* seg)
{
char* sMsg = new char[segLength];
sMsg[0] = NULL;
memcpy(sMsg, (char*)&seg->seqNum, SEQ_NUM_LEN);
memcpy(sMsg + SEQ_NUM_LEN, seg->data, msgSegLen);
int st = streamSender->Send(sMsg, segLength);
delete sMsg;
return st;
}
void UDPStreamSender::SendBuffer()
{
bufLock.lock();
bool hasElms = sendBuffer->hasElems();
bufLock.unlock();
while (_isRunning || hasElms)
{
std::unique_lock<std::mutex> mtx_lock(bufLock);
size_t firstUnsent = sendBuffer->firstUnsent();
size_t buffCount = sendBuffer->count();
while (!sendBuffer->hasElems())
{
stopIfEmpty.wait_for(mtx_lock, std::chrono::milliseconds(100));
if (_allRecived)
return;
}
for (size_t i = firstUnsent; i < buffCount; i++)
{
Segment sSeg = sendBuffer->at(i);
int st = SendSegment(&sSeg);
if (st >= segLength)
{
sSeg.isSent = true;
DWORD j = GetTickCount();
sSeg.timeOutTick = j + timeOutTicks;
sendBuffer->Replace(i, &sSeg);
sendBuffer->sendSeg();
cout << "SEG sent: SeqNum=" << sSeg.seqNum << endl;
}
}
hasElms = sendBuffer->hasElems();
mtx_lock.unlock();
}
}
void UDPStreamSender::AckRecive()
{
char* ackMessage;
while (!_allRecived)
{
ackMessage = ackReciver->Recive();
string ackMsg(ackMessage);
if (ackMsg.substr(0, 3).compare("ACK") != 0)
continue;
unsigned short ackSeqNum = 0;
memcpy(&ackSeqNum, ackMessage + 3, 2);
cout << "ACK recieved: seqNum=" << ackSeqNum << endl;
std::unique_lock<mutex> mtx_lock(bufLock);
sendBuffer->Ack(ackSeqNum);
_allRecived = !sendBuffer->hasElems() || !_isRunning;
mtx_lock.unlock();
stopifFull.notify_one();
}
}
void UDPStreamSender::ManageTimeout()
{
bufLock.lock();
bool hasElms = sendBuffer->hasElems();
bufLock.unlock();
while (!_allRecived)
{
std::unique_lock<mutex> mtx_lock(bufLock);
while (!sendBuffer->hasElems())
{
stopIfEmpty.wait_for(mtx_lock, std::chrono::milliseconds(100));
if (_allRecived)
return;
}
DWORD segTick = sendBuffer->first().timeOutTick;
DWORD cTick = GetTickCount();
if (sendBuffer->hasElems() && cTick > segTick)
{ // timeout, resend all buffer
sendBuffer->resendAll();
cout << "Timeout: seqNum=" << sendBuffer->first().seqNum << endl;
}
hasElms = sendBuffer->hasElems();
mtx_lock.unlock();
}
}