public void send(string destination, string numberOfPacket, string sendingInterval, string source, string clientName, string label) { int destinationPort, sourcePort; Int32.TryParse(destination, out destinationPort); Int32.TryParse(source, out sourcePort); string[] stringTable = { "woda gazowana", "rum", "mięta", "lód", "brązowy cukier", "limonka" }; int miliseconds = Int32.Parse(sendingInterval); int number = Int32.Parse(numberOfPacket); for (int i = 0; i < number; i++) { Random rnd = new Random(); int stringNumber = rnd.Next(1, stringTable.Length); DTOs.Packet pakiet = new DTOs.Packet(stringTable[stringNumber], "127.0.0.1", sourcePort, "127.0.0.1", destinationPort, Int32.Parse(label)); byte[] intBytes = BitConverter.GetBytes(sourcePort); byte[] result = intBytes; byte[] packet = DTOs.Packet.PacketToByte(pakiet); byte[] newArray = new byte[packet.Length + 8]; packet.CopyTo(newArray, 8); for (int j = 0; j < 4; j++) { newArray[j] = result[j]; } byte[] ifPackage = BitConverter.GetBytes(0); for (int j = 4; j < 8; j++) { newArray[j] = ifPackage[j - 4]; } packet = newArray; socket.Send(packet); Thread.Sleep(miliseconds); } }
public static byte[] switchPacket(byte[] receivedData, List <String[]> switchTables) { DTOs.Packet packet = DTOs.Packet.ByteToPacket(receivedData, 4); byte[] currentport = new byte[4]; for (int k = 0; k < 4; k++) { currentport[k] = receivedData[k]; } int currentport2 = BitConverter.ToInt32(currentport, 0); int signal = 0; recursionSwitch(ref receivedData, switchTables, currentport2, packet, ref signal); if (signal == 0) { throw new Exception(); } return(receivedData); }
public void receive() { while (true) { try { byte[] Buffer = new byte[socket.SendBufferSize]; int readByte = socket.Receive(Buffer); byte[] rData = new byte[readByte]; Array.Copy(Buffer, rData, readByte); byte[] ifPackageBytes = new byte[4]; for (int i = 4; i < 8; i++) { ifPackageBytes[i - 4] = rData[i]; } int ifPackage = BitConverter.ToInt32(ifPackageBytes, 0); if (ifPackage == 0) { byte[] packetBytes = new byte[rData.Length - 4]; for (int i = 0; i < 4; i++) { packetBytes[i] = rData[i]; } for (int i = 8; i < rData.Length; i++) { packetBytes[i - 4] = rData[i]; } DTOs.Packet packet = new DTOs.Packet(); packet = DTOs.Packet.ByteToPacket(rData, 4); string message = packet.Message; int label = packet.Labels.Peek(); mainWindow.Invoke(new Action(delegate() { mainWindow.writeReceipt("Odebrana wiadomość: " + message + " nr " + (packetNumber + 1).ToString() + " z etykietą " + label + "\n"); })); packetNumber++; } else if (ifPackage == 1) { Queue <byte[]> packets = new Queue <byte[]>(); DTOs.Package package = DTOs.Package.ByteToPackage(rData, 8); int g = package.packageQueue.Count; for (int i = 0; i < g; i++) { byte[] receivedData = package.packageQueue.Dequeue(); for (int j = 0; j < 4; j++) { receivedData[j] = rData[j]; } packets.Enqueue(receivedData); } while (packets.Count != 0) { DTOs.Packet packet = new DTOs.Packet(); packet = DTOs.Packet.ByteToPacket(packets.Dequeue(), 4); string message = packet.Message; int label = packet.Labels.Peek(); mainWindow.Invoke(new Action(delegate() { mainWindow.writeReceipt("Odebrana wiadomość: " + message + " nr " + (packetNumber + 1).ToString() + " z etykietą " + label + "\n"); })); packetNumber++; } } } catch (SocketException ex) { } } }
public static void recursionSwitch(ref byte[] receivedData, List <String[]> switchTables, int currentport2, DTOs.Packet packet, ref int signal) { for (int i = 0; i < switchTables.Count; i++) { if (Int32.Parse(switchTables[i][0]) == currentport2 && Int32.Parse(switchTables[i][1]) == packet.Labels.Peek() && Int32.Parse(switchTables[i][3]) == 0) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(Router.RouterMain.GetTimestamp(DateTime.Now) + "\tUsunięto etykietę nr " + packet.Labels.Pop()); signal++; recursionSwitch(ref receivedData, switchTables, currentport2, packet, ref signal); } else if (Int32.Parse(switchTables[i][0]) == currentport2 && Int32.Parse(switchTables[i][1]) == packet.Labels.Peek() && switchTables[i].Length == 5) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(Router.RouterMain.GetTimestamp(DateTime.Now) + "\tUsunięto etykietę nr " + packet.Labels.Pop()); Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(Router.RouterMain.GetTimestamp(DateTime.Now) + "\tDodano etykietę nr " + Int32.Parse(switchTables[i][3])); packet.Labels.Push(Int32.Parse(switchTables[i][3])); Console.WriteLine(Router.RouterMain.GetTimestamp(DateTime.Now) + "\tPakiet skierowany na interfejs wyjściowy: " + Int32.Parse(switchTables[i][2])); byte[] intBytes = BitConverter.GetBytes(Int32.Parse(switchTables[i][2])); Array.Copy(intBytes, result, 4); signal++; recursionSwitch(ref receivedData, switchTables, currentport2, packet, ref signal); } else if (Int32.Parse(switchTables[i][0]) == currentport2 && Int32.Parse(switchTables[i][1]) == packet.Labels.Peek() && switchTables[i].Length == 6) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine(Router.RouterMain.GetTimestamp(DateTime.Now) + "\tDodano etykietę nr " + Int32.Parse(switchTables[i][3])); packet.Labels.Push(Int32.Parse(switchTables[i][3])); signal++; } } byte[] packetbytes = DTOs.Packet.PacketToByte(packet); receivedData = new byte[packetbytes.Length + 4]; Array.Copy(packetbytes, 0, receivedData, 4, packetbytes.Length); for (int j = 0; j < 4; j++) { receivedData[j] = result[j]; } return; }