public TransferPacket GetPacket() { TransferPacket obj = new TransferPacket(); //This is slow. But it is just to test concept. Lets not worry about speed at the moment. for (int i = 0; i < m_transferArrayExists.Count(); i++) { if (m_transferArrayExists[i] == true) { unsafe { Packet p = m_packetList[i]; for (int j = 0; j < Globals.bytesPerPacket; j++) { obj.data[j] = p.m_bytes[j]; } obj.object_id = m_id; obj.packet_id = i; obj.size = m_size; } m_transferArray[i] = 0; m_transferArrayExists[i] = false; break; } } return(obj); }
static void Main(string[] args) { //Do setup. StartupForm introForm = new StartupForm(); introForm.ShowDialog(); Controller MainController = new Controller(); for (int i = 0; i < introForm.m_nodes.Count(); i++) { MainController.AddNode(introForm.m_nodes[i]); } // Setup the controller for sending and recieving packets. MainController.InitClient(introForm.m_sourcePort, introForm.m_destinationPort); TransferObject TransObj = new TransferObject(introForm.m_outgoingID, introForm.m_filename); // We are looking to find any floating packets with the specific ID. MainController.AddRequest(introForm.m_requestID); // Start bouncing like Delay-line memory. MainController.StartBouncing(); //Loop until all data is in the line. bool packetsLeft = true; while (packetsLeft) { Thread.Sleep(1); TransferPacket pack = TransObj.GetPacket(); if (pack.size > 0) { MainController.SendPacket(pack); } else { packetsLeft = false; } } //Just keep running and bouncing data. while (true) { Thread.Sleep(1000); Debug.Write("."); } MainController.StopBouncing(); }
public void AddPacket(TransferPacket _in) { if (m_size < _in.size) { m_size = _in.size; } if (_in.object_id == m_id) { if (!m_recievedIndices.Contains(_in.packet_id)) { if (m_packetList.Count == 0) { stopWatch.Start(); } unsafe { Packet pack = new Packet(); for (int j = 0; j < Globals.bytesPerPacket; j++) { pack.m_bytes[j] = _in.data[j]; } m_packetList.Add(pack); m_recievedIndices.Add(_in.packet_id); } Console.WriteLine("Complete: " + ((float)m_packetList.Count * Globals.bytesPerPacket) / (float)m_size + "%"); } } if ((m_packetList.Count * Globals.bytesPerPacket) >= m_size) { m_full = true; WriteFile(); stopWatch.Stop(); Console.WriteLine("Complete: " + "100%"); TimeSpan ts = stopWatch.Elapsed; double ms = ts.TotalMilliseconds; double s = ts.TotalSeconds; Console.WriteLine("Speed: " + ((float)m_size / 1024.0f) / s + " kb/s."); } }
public bool SendPacket(TransferPacket _packet) { bool anySent = false; outPacket dat = new outPacket(); outPacket[] datArray = { dat }; unsafe { dat.obj_id = _packet.object_id; dat.packet_id = _packet.packet_id; dat.size = _packet.size; for (int j = 0; j < Globals.bytesPerPacket; j++) { dat.fixedBuffer[j] = _packet.data[j]; } byte[] bytePacket = new byte[sizeof(outPacket)]; unsafe { fixed(void *s = &bytePacket[0]) { CopyMemory(s, &dat, sizeof(outPacket)); } } //Buffer.BlockCopy(datArray, 0, bytePacket, 0, bytePacket.Length); for (int i = 0; i < m_knownNodes.Count(); i++) { SendUdp(m_knownNodes[i], 11000, bytePacket); } } return(anySent); }
public void BounceFunction() { //Creates a UdpClient for reading incoming data. UdpClient receivingUdpClient = new UdpClient(11000); //Creates an IPEndPoint to record the IP Address and port number of the sender. // The IPEndPoint will allow you to read datagrams sent from any source. IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); while (m_bouncing) { try { // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = receivingUdpClient.Receive(ref RemoteIpEndPoint); string returnData = Encoding.ASCII.GetString(receiveBytes); int[] recievePacket = new int[3]; recievePacket[0] = BitConverter.ToInt32(receiveBytes, 0); recievePacket[1] = BitConverter.ToInt32(receiveBytes, 4); recievePacket[2] = BitConverter.ToInt32(receiveBytes, 8); byte[] packetData = new byte[Globals.bytesPerPacket]; for (int i = 0; i < Globals.bytesPerPacket; i++) { packetData[i] = receiveBytes[i + 12]; } bool collected = false; for (int i = 0; i < m_requestedIDs.Count; i++) { if (recievePacket[0] == m_requestedIDs[i]) { collected = true; TransferPacket pack = new TransferPacket(); unsafe { pack.object_id = recievePacket[0]; pack.packet_id = recievePacket[1]; pack.size = recievePacket[2]; for (int k = 0; k < Globals.bytesPerPacket; k++) { pack.data[k] = packetData[k]; } } m_requestedObj[i].AddPacket(pack); if (m_requestedObj[i].m_full) { m_finishedObj.Add(m_requestedObj[i]); m_requestedIDs.RemoveAt(i); m_requestedObj.RemoveAt(i); } } } if (!collected) { SendPacket(receiveBytes); } //Console.WriteLine(RemoteIpEndPoint.Address.ToString() + " : OBJID, PACKETID, SIZE, DATA: " + recievePacket[0] // + ", " + recievePacket[1] + " " + recievePacket[2] + " " + recievePacket[3]); } catch (Exception e) { Console.WriteLine(e.ToString()); } } }