Example #1
0
//Data link layer function to receive a data frame or an ack from server.
//Also simulates the physical layer receive funtion.
//The data frame received contains an ack in its payload.
int dll_recv_py(int sock, Frame* frame, Ack* ack)
{
	//Set up the receive buffer.
	int size = 150;
	ssize_t numBytes = 0;
	char buffer[size];
	//Use recv() funtion to receive from server.
	numBytes = recv(sock, buffer, size, 0);
	vector<char> objBuf;
	for(int i=0; i<numBytes; i++)
	{
		objBuf.push_back(buffer[i]);
	}
	//Check the frame type field to determie whether it's a data frame or an ack.
	//If it's an ack, return 0, if it's a data frame, return 1.
	if(objBuf.at(2) == 'a')
	{
		Ack tempAck;
		*ack = tempAck.reconstruct(objBuf);
		return 0;
	}
	else
	{
		Frame tempFrame;
		*frame = tempFrame.reconstruct(objBuf);
		return 1;
	}
}
Example #2
0
//Data link layer function to send Ack data frame to network layer.
//Also simulates the check in network layer.
int dll_send_nwl(Frame frame, bool* breakFlg, int seqNum, bool ieop)
{
	//Get the ack from the data frame.
	Ack ack;
	ack = ack.reconstruct(frame.getDataField());
	//If the ack is correct.
	if(ack.reconstruct(frame.getDataField()).isCorrect(seqNum))
	{
		//If this frame is the end of photo, set the break flag.
		if(ieop)
		{
			*breakFlg = true;
		}
		nwl = true;
		dll = false;
		return 1;
	}
	//If the ack is not correct, return -1.
	else
	{
		return -1;
	}
}