Beispiel #1
0
 public override void OnCommand(ITftpCommand command, System.Net.EndPoint endpoint)
 {
     if (command is OptionAcknowledgement)
     {
         TransferOptionSet acknowledged = new TransferOptionSet((command as OptionAcknowledgement).Options);
         Context.FinishOptionNegotiation(acknowledged);
         BeginSendingTo(endpoint);
     }
     else
     if (command is Acknowledgement && (command as Acknowledgement).BlockNumber == 0)
     {
         Context.FinishOptionNegotiation(TransferOptionSet.NewEmptySet());
         BeginSendingTo(endpoint);
     }
     else
     if (command is Error)
     {
         //The server denied our request
         Error error = (Error)command;
         Context.SetState(new ReceivedError(error));
     }
     else
     {
         base.OnCommand(command, endpoint);
     }
 }
Beispiel #2
0
 public void RaiseCommandReceived(ITftpCommand command, EndPoint endpoint)
 {
     if (OnCommandReceived != null)
     {
         OnCommandReceived(command, endpoint);
     }
 }
Beispiel #3
0
 private void RaiseOnCommand(ITftpCommand command, IPEndPoint endpoint)
 {
     if (OnCommandReceived != null)
     {
         OnCommandReceived(command, endpoint);
     }
 }
Beispiel #4
0
 private void connection_OnCommandReceived(ITftpCommand command, EndPoint endpoint)
 {
     lock (this)
     {
         state.OnCommand(command, endpoint);
     }
 }
Beispiel #5
0
        private void serverSocket_OnCommandReceived(ITftpCommand command, EndPoint endpoint)
        {
            //Ignore all other commands
            if (!(command is ReadOrWriteRequest))
            {
                return;
            }

            //Open a connection to the client
            ITransferChannel channel = TransferChannelFactory.CreateConnection(endpoint, new IPEndPoint(localInterface, 0));

            //Create a wrapper for the transfer request
            ReadOrWriteRequest request  = (ReadOrWriteRequest)command;
            ITftpTransfer      transfer = request is ReadRequest ? (ITftpTransfer) new LocalReadTransfer(channel, request.Filename, request.Options) : new LocalWriteTransfer(channel, request.Filename, request.Options);

            if (command is ReadRequest)
            {
                RaiseOnReadRequest(transfer, endpoint);
            }
            else if (command is WriteRequest)
            {
                RaiseOnWriteRequest(transfer, endpoint);
            }
            else
            {
                throw new Exception("Unexpected tftp transfer request: " + command);
            }
        }
        public override void OnCommand(ITftpCommand command, EndPoint endpoint)
        {
            if (!endpoint.Equals(Context.GetConnection().RemoteEndpoint))
            {
                throw new Exception("Received message from illegal endpoint. Actual: " + endpoint + ". Expected: " + Context.GetConnection().RemoteEndpoint);
            }

            command.Visit(this);
        }
Beispiel #7
0
 private byte[] Serialize(ITftpCommand command)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         CommandSerializer serializer = new CommandSerializer();
         serializer.Serialize(command, stream);
         byte[] commandAsBytes = stream.GetBuffer();
         Array.Resize(ref commandAsBytes, (int)stream.Length);
         return(commandAsBytes);
     }
 }
        void UdpReceivedCallback(IAsyncResult result)
        {
            IPEndPoint   endpoint = new IPEndPoint(0, 0);
            ITftpCommand command  = null;

            try
            {
                byte[] data = null;

                lock (this)
                {
                    if (client == null)
                    {
                        return;
                    }

                    data = client.EndReceive(result, ref endpoint);
                }
                command = parser.Parse(data);
            }
            catch (SocketException e)
            {
                //Handle receive error
                RaiseOnError(new NetworkError(e));
                return;
            }
            catch (TftpParserException e2)
            {
                //Handle parser error
                RaiseOnError(new NetworkError(e2));
                return;
            }

            if (command != null)
            {
                RaiseOnCommand(command, endpoint);
            }

            lock (this)
            {
                if (client != null)
                {
                    client.BeginReceive(UdpReceivedCallback, null);
                }
            }
        }
Beispiel #9
0
        public void Send(ITftpCommand command)
        {
            if (client == null)
            {
                throw new ObjectDisposedException("UdpChannel");
            }

            if (endpoint == null)
            {
                throw new InvalidOperationException("RemoteEndpoint needs to be set before you can send TFTP commands.");
            }

            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(command, stream);
                byte[] data = stream.GetBuffer();
                client.Send(data, (int)stream.Length, endpoint);
            }
        }
Beispiel #10
0
        public override void OnCommand(ITftpCommand command, EndPoint endpoint)
        {
            if (command is Data || command is OptionAcknowledgement)
            {
                //The server acknowledged our read request.
                //Fix out remote endpoint
                Context.GetConnection().RemoteEndpoint = endpoint;
            }

            if (command is Data)
            {
                if (Context.NegotiatedOptions == null)
                {
                    Context.FinishOptionNegotiation(TransferOptionSet.NewEmptySet());
                }

                //Switch to the receiving state...
                ITransferState nextState = new Receiving();
                Context.SetState(nextState);

                //...and let it handle the data packet
                nextState.OnCommand(command, endpoint);
            }
            else if (command is OptionAcknowledgement)
            {
                //Check which options were acknowledged
                Context.FinishOptionNegotiation(new TransferOptionSet((command as OptionAcknowledgement).Options));

                //the server acknowledged our options. Confirm the final options
                SendAndRepeat(new Acknowledgement(0));
            }
            else if (command is Error)
            {
                Context.SetState(new ReceivedError((Error)command));
            }
            else
            {
                base.OnCommand(command, endpoint);
            }
        }
Beispiel #11
0
 public virtual void OnCommand(ITftpCommand command, EndPoint endpoint)
 {
 }
Beispiel #12
0
 public void Send(ITftpCommand command)
 {
     SentCommands.Add(command);
 }
Beispiel #13
0
 public void OnCommand(ITftpCommand command, EndPoint endpoint)
 {
     TftpTrace.Trace(GetStateName() + " OnCommand: " + command + " from " + endpoint, transfer);
     decoratee.OnCommand(command, endpoint);
 }
Beispiel #14
0
 public void OnCommand(ITftpCommand command)
 {
     State.OnCommand(command, GetConnection().RemoteEndpoint);
 }
Beispiel #15
0
 protected void SendAndRepeat(ITftpCommand command)
 {
     Context.GetConnection().Send(command);
     lastCommand = command;
     ResetTimeout();
 }
        /// <summary>
        /// Call this method to serialize the given <code>command</code> using the given <code>writer</code>.
        /// </summary>
        public void Serialize(ITftpCommand command, Stream stream)
        {
            CommandComposerVisitor visitor = new CommandComposerVisitor(stream);

            command.Visit(visitor);
        }