public override void OnCommand(ITFtpCommand command, System.Net.EndPoint endpoint) { switch (command) { case OptionAcknowledgement optionAcknowledgement: { TransferOptionSet acknowledged = new TransferOptionSet(optionAcknowledgement.Options); Context.FinishOptionNegotiation(acknowledged); BeginSendingTo(endpoint); break; } case Acknowledgement acknowledgement when acknowledgement.BlockNumber == 0: Context.FinishOptionNegotiation(TransferOptionSet.NewEmptySet()); BeginSendingTo(endpoint); break; case Error error: // The server denied our request Context.SetState(new ReceivedError(error)); break; default: base.OnCommand(command, endpoint); break; } }
private void Connection_OnCommandReceived(ITFtpCommand command, EndPoint endpoint) { lock (this) { State.OnCommand(command, endpoint); } }
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); // 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); switch (command) { case ReadRequest _: RaiseOnReadRequest(transfer, endpoint); break; case WriteRequest _: RaiseOnWriteRequest(transfer, endpoint); break; default: 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); }
public override void OnCommand(ITFtpCommand command, EndPoint endpoint) { if (command is Commands.Data || command is OptionAcknowledgement) { // The server acknowledged our read request. // Fix out remote endpoint Context.GetConnection().RemoteEndpoint = endpoint; } switch (command) { case Commands.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); break; } case OptionAcknowledgement optionAcknowledgement: // Check which options were acknowledged Context.FinishOptionNegotiation(new TransferOptionSet(optionAcknowledgement.Options)); // the server acknowledged our options. Confirm the final options SendAndRepeat(new Acknowledgement(0)); break; case Error error: Context.SetState(new ReceivedError(error)); break; default: base.OnCommand(command, endpoint); break; } }
public void Send(ITFtpCommand command) { if (m_client == null) { throw new ObjectDisposedException(nameof(UdpChannel)); } if (m_endpoint == null) { throw new InvalidOperationException("RemoteEndpoint needs to be set before you can send TFTP commands."); } using (MemoryStream stream = new MemoryStream()) { m_serializer.Serialize(command, stream); byte[] data = stream.GetBuffer(); m_client.Send(data, (int)stream.Length, m_endpoint); } }
private void UdpReceivedCallback(IAsyncResult result) { IPEndPoint endpoint = new IPEndPoint(0, 0); ITFtpCommand command = null; try { byte[] data; lock (this) { if (m_client == null) { return; } data = m_client.EndReceive(result, ref endpoint); } command = m_parser.Parse(data); } catch (SocketException e) { // Handle receive error RaiseOnError(new NetworkError(e)); } catch (TFtpParserException e2) { // Handle parser error RaiseOnError(new NetworkError(e2)); } if (command != null) { RaiseOnCommand(command, endpoint); } lock (this) { m_client?.BeginReceive(UdpReceivedCallback, null); } }
protected void SendAndRepeat(ITFtpCommand command) { Context.GetConnection().Send(command); m_lastCommand = command; ResetTimeout(); }
private void RaiseOnCommand(ITFtpCommand command, IPEndPoint endpoint) { OnCommandReceived?.Invoke(command, endpoint); }
/// <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); }
public virtual void OnCommand(ITFtpCommand command, EndPoint endpoint) { }