public void receive() { while (true) { byte[] Buffer = new byte[socket.SendBufferSize]; int readByte = socket.Receive(Buffer); byte[] receivedData = new byte[readByte]; Array.Copy(Buffer, receivedData, readByte); MPLS.BinaryWrapper receivedMessage = new MPLS.BinaryWrapper(receivedData, true); MPLS.AggregatePacket receivedAggregate = MPLS.MPLSMethods.Deserialize(receivedMessage); int numberOfPackets = receivedAggregate.packets.Length; string currentTime = " <" + DateTime.Now.ToString("hh:mm:ss:fff") + ">\n"; for (int i = 0; i < numberOfPackets; i++) { if (receivedAggregate.packets[i].labels.Last() == 0) { continue; } string receivedString = receivedAggregate.packets[i].data; mainWindow.Invoke(new Action(delegate() { mainWindow.logReceivedPacket(receivedString, currentTime); })); } } }
public void send(string message, int label, int numberOfPacket, int sendingInterval, bool randomization) { sendingAllowance = true; int[] labels = new int[1]; labels[0] = label; if (randomization) { int randomByte; int miliseconds = sendingInterval; int number = numberOfPacket; for (int i = 0; i < number; i++) { if (!sendingAllowance) { break; } string stringToSend = message; Random randomNumber = new Random(); int multiplication = randomNumber.Next(0, 4); for (int j = 0; j < multiplication; j++) { stringToSend = stringToSend + ";" + message; } stringToSend = stringToSend + "(" + (multiplication + 1) + ")"; MPLS.MPLSPacket[] packet = new MPLS.MPLSPacket[1]; packet[0] = new MPLS.MPLSPacket(labels, stringToSend); MPLS.AggregatePacket aggregate = new MPLS.AggregatePacket(packet); MPLS.BinaryWrapper messageToSend = MPLS.MPLSMethods.Serialize(aggregate); messageToSend.interfaceId = gate; randomByte = randomNumber.Next(0, 255); messageToSend.randomNumber = Byte.Parse(randomByte.ToString()); socket.Send(messageToSend.HeaderPlusData()); string currentTime = " <" + DateTime.Now.ToString("hh:mm:ss:fff") + ">\n"; mainWindow.Invoke(new Action(delegate() { mainWindow.logSentPacket(stringToSend, currentTime); })); Thread.Sleep(miliseconds); } } else { MPLS.MPLSPacket[] packet = new MPLS.MPLSPacket[1]; packet[0] = new MPLS.MPLSPacket(labels, message); MPLS.AggregatePacket aggregate = new MPLS.AggregatePacket(packet); MPLS.BinaryWrapper messageToSend = MPLS.MPLSMethods.Serialize(aggregate); messageToSend.interfaceId = gate; Random random = new Random(); int randomByte; int miliseconds = sendingInterval; int number = numberOfPacket; for (int i = 0; i < number; i++) { if (!sendingAllowance) { break; } randomByte = random.Next(0, 255); messageToSend.randomNumber = Byte.Parse(randomByte.ToString()); socket.Send(messageToSend.HeaderPlusData()); string currentTime = " <" + DateTime.Now.ToString("hh:mm:ss:fff") + ">\n"; mainWindow.Invoke(new Action(delegate() { mainWindow.logSentPacket(message, currentTime); })); Thread.Sleep(miliseconds); } } }