diff --git a/Protocol.cpp b/Protocol.cpp index b243c2c7589d343138e8dcee7c0105b8ff4d68db..d1636b2215d083aa8b632017e540a93445b4f5e3 100644 --- a/Protocol.cpp +++ b/Protocol.cpp @@ -2,6 +2,11 @@ #include "Protocol.h" #include "definitions.h" +typedef struct{ + int index; + bool sent; +}FrameItem; + vector<Message> Protocol::getMessages(){ return messages; } @@ -118,31 +123,35 @@ int Protocol::recvMessage(int sockt){ void Protocol::transmit(int sockt, int window){ int status; - vector<int> frame; + vector<FrameItem> frame; int lastFramed = 0; int messagesLeft = messages.size(); - bool shouldSend = true; while(messagesLeft > 0){ for(int j=0; j < window; ++j) { - frame.push_back(lastFramed++); - if(shouldSend) sendMessage(sockt, frame[j]); + FrameItem fi = {.index = lastFramed++, .sent=false}; + frame.push_back(fi); + if(!frame[j].sent){ + sendMessage(sockt, frame[j].index); + frame[j].sent = true; + } } // TODO: timeout status = recvMessage(sockt); cout << "transmit status:" << status << endl; - shouldSend = true; if(status == 0){ int nackIndex = stoi(getDataAsString()); for(int j=0; j < window; ++j) { - if(frame[j] < nackIndex) { + if(frame[j].index < nackIndex) { frame.erase(frame.begin() + j); --messagesLeft; + }else if(frame[j].index == nackIndex) { + frame[j].sent = false; } } }else if(status == 1){ int ackIndex = stoi(getDataAsString()); for(int j=0; j < window; ++j) { - if(frame[j] <= ackIndex) { + if(frame[j].index <= ackIndex) { frame.erase(frame.begin() + j); --messagesLeft; } @@ -156,7 +165,6 @@ void Protocol::transmit(int sockt, int window){ cout << "Remoto:\n" << ((status == ERROR)?"ERROR: ":"") << getDataAsString() << endl; } else { //TODO: treat error - shouldSend = false; } } }