private void registerPacket(TCPCopycatPacket packet, TCPCopycatReceiveMessageCallback lambda, int waitTime) { if (waitTime < 0) { throw new Exception("waitTime must be positive"); } //Console.WriteLine("registering packet: " + packet.header.sequenceNumber); if (dictionaryRegisteredPacket.ContainsKey(packet.header.sequenceNumber + 1)) { dictionaryRegisteredPacket[packet.header.sequenceNumber + 1] = lambda; } else { dictionaryRegisteredPacket.Add(packet.header.sequenceNumber + 1, lambda); } //Console.WriteLine(dictionaryRegisteredPacket.Count.ToString() + " packets registered"); setPacketResponse(packet.header.sequenceNumber + 1, TCPCopyCatController.responseCode.NONE); ElapsedEventHandler timerCallbackLambda = delegate(object state, System.Timers.ElapsedEventArgs e) { TaskTimer t = (TaskTimer)state; TCPCopycatPacket packetToSend = t.packetToSend; TCPCopycatReceiveMessageCallback l = t.lambda; int w = t.waitTime; t.Dispose(); if (dictionaryRegisteredPacketResponse[packetToSend.header.sequenceNumber + 1] == TCPCopyCatController.responseCode.OK) //unregister timer is called before the timer is even initialized { return; } Console.WriteLine("packet: " + packetToSend.header.sequenceNumber.ToString() + " lost"); unregisterPacket(packetToSend.header.sequenceNumber + 1); setPacketResponse(packetToSend.header.sequenceNumber + 1, TCPCopyCatController.responseCode.NONE); TCPCopyCatController.sendMessageToEndPoint(socket, serverEndpoint, packetToSend); registerPacket(packetToSend, l, w); }; TaskTimer timer = new TaskTimer(); timer.Interval = waitTime; // set the interval as 10000 ms timer.Elapsed += new System.Timers.ElapsedEventHandler(timerCallbackLambda); // set the event to execute timer.AutoReset = false; // false: only execute once timer.Enabled = true; // to decide if execute the event of timer specified timer.packetToSend = packet; timer.lambda = lambda; timer.waitTime = waitTime; registerTimer(packet.header.sequenceNumber + 1, timer); }
public async Task <TCPCopyCatController.responseCode> connectToServer(IPEndPoint serverEndpoint) { const int connectionSequenceNumber = 0; if (socket == null) { socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); } else if (socket.IsBound) { return(await getPacketResponse(connectionSequenceNumber + 1)); } TCPCopyCatController.startListenOnSocketAsync(socket, onPacketReceive); IPEndPoint test = new IPEndPoint(IPAddress.Any, 0); socket.Bind(test); this.serverEndpoint = serverEndpoint; TCPCopycatPacket.TCPCopycatHeader header = new TCPCopycatPacket.TCPCopycatHeader(); header.SYN = 1; header.sequenceNumber = connectionSequenceNumber; header.ACK = 0; header.dataLenght = 0; TCPCopycatPacket connectionPacket = new TCPCopycatPacket(header, new byte[1]); TCPCopycatReceiveMessageCallback receivedMessageCallbackLambda = delegate(TCPCopycatPacket packet, IPEndPoint sender) { if (packet.header.SYN == 1 && packet.header.acknowledgeNumber == (connectionSequenceNumber + 1)) { this.serverEndpoint = sender; return(TCPCopyCatController.responseCode.OK); } return(TCPCopyCatController.responseCode.UNKNOWN_ERROR); }; if (!TCPCopyCatController.sendMessageToEndPoint(socket, serverEndpoint, connectionPacket)) { registerPacket(connectionPacket, receivedMessageCallbackLambda, 1000); return(await getPacketResponse(connectionSequenceNumber + 1)); } else { return(TCPCopyCatController.responseCode.BAD_REQUEST); } }
public void sendFilePacket(TCPCopycatPacket packet) { if (packet == null) { return; } if (packet.header.sequenceNumber == filePackets[filePackets.Length - 1].header.sequenceNumber) { packet.header.FIN = 1; } packet.header.sequenceNumber += sequenceNumberOffset; TCPCopycatReceiveMessageCallback receivedMessageCallbackLambda = delegate(TCPCopycatPacket _packet, IPEndPoint sender) { unregisterPacket(_packet.header.acknowledgeNumber); setPacketResponse(_packet.header.acknowledgeNumber, TCPCopyCatController.responseCode.OK); // this ensure we only send 10 files at a time and also respect the selective repeat structure if (_packet.header.sequenceNumber < windowLowerbound) { Console.WriteLine("************** Already received packet " + (_packet.header.sequenceNumber).ToString() + " ***********"); } else if (_packet.header.sequenceNumber > windowLowerbound) { Console.WriteLine("************** Not in order packet received " + (_packet.header.sequenceNumber).ToString() + " ***********"); } else if (_packet.header.sequenceNumber == windowLowerbound) { Console.WriteLine("************** In order " + (_packet.header.sequenceNumber).ToString() + " ***********"); } translateWindow(); return(TCPCopyCatController.responseCode.OK); }; //Console.WriteLine("Sending file to port: " + serverEndpoint.Port.ToString()); if (!TCPCopyCatController.sendMessageToEndPoint(socket, serverEndpoint, packet)) { registerPacket(packet, receivedMessageCallbackLambda, 2500); } }
public void sendMessageToEndPoint(Socket socket, IPEndPoint receiver, TCPCopycatPacket packet, TCPCopycatReceiveMessageCallback callbackLambda, int timeoutTime = 1000) { sendMessageToEndPoint(socket, receiver, packet); }