예제 #1
0
        private void ProcessAcknowledgment(IPEndPoint remoteEndPoint, TftpPacket requestPacket)
        {
            var position = requestPacket.BlockNumber * _blockSize;

            if (position >= _fileInfo.Length)
            {
                Console.WriteLine("File transfer successfully finished");
                return;
            }

            var length = Math.Min(_blockSize, (Int32)_fileInfo.Length - position);

            using (var writer = new TftpPacketWriter(_fileBlock))
            {
                writer.WriteUInt16((UInt16)TftpPacketType.Data);
                writer.WriteUInt16((UInt16)(requestPacket.BlockNumber + 1));

                using (var fileStream = new FileStream(_fileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    fileStream.Position = position;
                    fileStream.Read(_fileBlock, 4, (Int32)length);
                }

                SendData(_fileBlock, length + 4, remoteEndPoint);
            }
        }
예제 #2
0
        private void SendError(TftpError errorCode, String errorMessage, IPEndPoint remoteEndPoint)
        {
            Console.WriteLine("Sending error {0} '{1}'", errorCode, errorMessage);

            using (var writer = new TftpPacketWriter(errorMessage.Length + 5))
            {
                writer.WriteUInt16((UInt16)TftpPacketType.Error);
                writer.WriteUInt16((UInt16)errorCode);
                writer.WriteString(errorMessage);

                SendData(writer.ToArray(), remoteEndPoint);
            }
        }
예제 #3
0
        public Byte[] ToArray()
        {
            using (var writer = new TftpPacketWriter(Type == TftpPacketType.Data ? Data.Length + 4: 1024))
            {
                writer.WriteUInt16((UInt16)Type);

                switch (Type)
                {
                case TftpPacketType.ReadRequest:
                    break;

                case TftpPacketType.WriteRequest:
                    break;

                case TftpPacketType.Data:
                    break;

                case TftpPacketType.Acknowledgment:
                    break;

                case TftpPacketType.Error:
                    break;

                case TftpPacketType.OptionAcknowledgment:
                    var names = Options.GetNames();
                    foreach (var name in names)
                    {
                        writer.WriteString(name);
                        var value = Options.GetString(name);
                        writer.WriteString(value);
                    }
                    break;

                default:
                    Console.WriteLine("Unsupported packet type: {0}", (int)Type);
                    break;
                }

                return(writer.ToArray());
            }
        }