private async void DequeueTransfer() { //Get the next transfer in the queue var transfer = _queued.Dequeue(); //Wait for the transfer try { await Transfer(transfer); } catch (TransferException ex) { if (ex.InnerException.GetType() == typeof(IOException) && RequeueOnError && ex.Message.EndsWith("is being used by another process")) { transfer.State = TransferState.Requeued; _queued.Enqueue(transfer); } TransferFailed.Invoke(ex); } catch (Exception) { throw; } if (_queued.Count > 0) { DequeueTransfer(); } }
public bool Get(string LocalFile, string RemoteFile, string Host, Modes Mode, int BlockSize, int Timeout) { int recvLen, remoteFileSize = 0, buffer = BlockSize + 4; long bytesReceived = 0; BinaryWriter BWriter = new BinaryWriter(File.Open(LocalFile, FileMode.Create)); TFTPSession.OpCodes opCode = new TFTPSession.OpCodes(); IPHostEntry hInfo = Dns.GetHostEntry(Host); IPAddress address = hInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(address, 69); EndPoint localEP = (remoteEP); Socket UDPSock = new Socket (remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Create initial request and buffer for response byte[] sendData = _packetBuilder.Request(TFTPSession.OpCodes.RRQ, RemoteFile, Mode, BlockSize, 0, Timeout); byte[] recvData = new byte[BlockSize + 4]; UDPSock.ReceiveTimeout = Timeout * 1000; // Send request and wait for response UDPSock.SendTo(sendData, remoteEP); recvLen = UDPSock.ReceiveFrom(recvData, ref localEP); // Get TID remoteEP.Port = ((IPEndPoint)localEP).Port; // Invoke connected event Connected.Invoke(); while (true) { // Read opcode opCode = _packetReader.ReadOpCode(recvData); // DATA packet if (opCode == TFTPSession.OpCodes.DATA) { bytesReceived += recvLen - 4; // Invoke Transferring Event Transferring.Invoke(bytesReceived, remoteFileSize); for (int h = 4; h < recvLen; h++) { BWriter.Write(recvData[h]); } sendData = _packetBuilder.Ack(recvData[2], recvData[3]); // Check if this packet is the last if (recvLen < buffer) { // Send final ACK UDPSock.SendTo(sendData, remoteEP); // Invoked TransferFinished Event TransferFinished.Invoke(); break; } } // OACK packet else if (opCode == TFTPSession.OpCodes.OACK) { remoteFileSize = _packetReader.ReadTransferSize(recvData); sendData = _packetBuilder.Ack(0, 0); } // ERROR packet else if (opCode == TFTPSession.OpCodes.ERROR) { ErrorPacket transferError = _packetReader.ReadError(recvData); TransferFailed.Invoke(transferError.Code, transferError.Message); break; } // Send next packet UDPSock.SendTo(sendData, remoteEP); recvLen = UDPSock.ReceiveFrom(recvData, ref localEP); remoteEP.Port = ((IPEndPoint)localEP).Port; } BWriter.Close(); UDPSock.Close(); // Invoke Disconnected Event Disconnected.Invoke(); return(true); }
public bool Put(string LocalFile, string RemoteFile, string Host, Modes Mode, int BlockSize, int Timeout) { int[] block = new int[2]; int bufferSize = BlockSize; long fileSize, bytesSent = 0; BinaryReader BReader = new BinaryReader(File.Open(LocalFile, FileMode.Open)); FileInfo sendFile = new FileInfo(LocalFile); TFTPSession.OpCodes opCode = new TFTPSession.OpCodes(); IPHostEntry hostInfo = Dns.GetHostEntry(Host); IPAddress address = hostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(address, 69); EndPoint localEP = (remoteEP); Socket UDPSock = new Socket (remoteEP.AddressFamily, SocketType.Dgram, ProtocolType.Udp); // Retrieve filesize for tsize option fileSize = sendFile.Length; // Create initial request and buffer for response byte[] sendData = _packetBuilder.Request(TFTPSession.OpCodes.WRQ, RemoteFile, Mode, BlockSize, fileSize, Timeout); byte[] recvData = new byte[bufferSize]; UDPSock.ReceiveTimeout = Timeout * 1000; // Send request and wait for response UDPSock.SendTo(sendData, remoteEP); UDPSock.ReceiveFrom(recvData, ref localEP); //Get TID remoteEP.Port = ((IPEndPoint)localEP).Port; // Invoke Connected Event Connected.Invoke(); while (true) { // Read opcode opCode = _packetReader.ReadOpCode(recvData); // ACK packet if (opCode == TFTPSession.OpCodes.ACK) { block = _packetBuilder.IncrementBock(recvData, block); sendData = BReader.ReadBytes(bufferSize); bytesSent += sendData.Length; // Invoke Transferring Event Transferring.Invoke(bytesSent, fileSize); sendData = _packetBuilder.Data(sendData, block[0], block[1]); // Check if this packet is the last if (sendData.Length < bufferSize + 4) { // Send final data packet and wait for ack while (true) { UDPSock.SendTo(sendData, remoteEP); UDPSock.ReceiveFrom(recvData, ref localEP); remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal if (_packetReader.CompareBlocks(sendData, recvData)) { break; } } // Invoke TransferFinished Event TransferFinished.Invoke(); break; } } // OACK packet else if (opCode == TFTPSession.OpCodes.OACK) { sendData = BReader.ReadBytes(bufferSize); sendData = _packetBuilder.Data(sendData, 0, 1); bytesSent += sendData.Length - 4; // Invoke Transferring Event Transferring.Invoke(bytesSent, fileSize); if (fileSize == 0) { // Invoke TransferFinished Event TransferFinished.Invoke(); break; } else { // Check if this packet is the last if (sendData.Length < bufferSize + 4) { // Send final data packet and wait for ack while (true) { UDPSock.SendTo(sendData, remoteEP); UDPSock.ReceiveFrom(recvData, ref localEP); remoteEP.Port = ((IPEndPoint)localEP).Port; // Check the blocks and break free if equal if (_packetReader.CompareBlocks(sendData, recvData)) { break; } } // Invoke TransferFinished Event TransferFinished.Invoke(); break; } } } else if (opCode == TFTPSession.OpCodes.ERROR) { ErrorPacket transferError = _packetReader.ReadError(recvData); TransferFailed.Invoke(transferError.Code, transferError.Message); break; } // Send next packet UDPSock.SendTo(sendData, remoteEP); UDPSock.ReceiveFrom(recvData, ref localEP); remoteEP.Port = ((IPEndPoint)localEP).Port; } BReader.Close(); UDPSock.Close(); // Invoke Disconnected Event Disconnected.Invoke(); return(true); }