-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.c
62 lines (57 loc) · 1.83 KB
/
sender.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// sender.c
//
//
// Created by Akmal Fadlurohman on 10/16/17.
//
//
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include "sender.h"
void initSender(Sender *S,int SWS,int sendBufferSize) {
S->SWS = SWS;
S->sendBufferSize = sendBufferSize;
S->seqNum = 0;
S->LAR = -1;
S->LFS = -1;
S->sendBuffer = malloc(sendBufferSize*sizeof(char*));
for (int i=0;i<sendBufferSize;i++) {
S->sendBuffer[i] = malloc(sendFrame_size*sizeof(char));
}
}
void fillFrameBuffer(sendFrame* frameBuffer,char* msgBuffer,int *currentFrameBuffer,int msgLength) {
*currentFrameBuffer = msgLength;
for (int i=0;i<*currentFrameBuffer;i++) {
initFrame(&frameBuffer[i]);
setSeqNum(&frameBuffer[i],i);
setData(&frameBuffer[i],msgBuffer[i]);
setCheckSum(&frameBuffer[i],checkSum(frameBuffer[i]));
}
}
void deleteFromFrameBuffer(sendFrame* frameBuffer,int first, int last,int *currentFrameBuffer) {
for (int i=first;i<=last;i++) {
frameBuffer[i] = frameBuffer[last+i];
}
*currentFrameBuffer = *currentFrameBuffer - (last-first);
}
void fillSendBuffer(Sender *S,sendFrame* frameBuffer,int *currentFrameBuffer,int *currentSendBuffer,int *msgLength) {
if (*msgLength <= (S->sendBufferSize)) {
*currentSendBuffer = *msgLength;
} else {
*currentSendBuffer = S->sendBufferSize;
}
for (int i=0;i<*currentSendBuffer;i++) {
for (int j=0;j<sendFrame_size;j++) {
S->sendBuffer[i][j] = sendFrameToByte(&frameBuffer[i])[j];
}
}
}
void deleteFromSendBuffer(Sender *S,int first, int last,int* currentSendBuffer) {
for (int i=first;i<=last;i++) {
for (int j=0;j<sendFrame_size;j++) {
S->sendBuffer[i][j] = S->sendBuffer[last+i][j];
}
}
*currentSendBuffer = *currentSendBuffer - (last-first);
}