internal void SendMessage(SftpMessage sftpMessage) { var messageData = sftpMessage.GetBytes(); var data = new byte[4 + messageData.Length]; ((uint)messageData.Length).GetBytes().CopyTo(data, 0); messageData.CopyTo(data, 4); this.SendData(data); }
public SftpDataMessage(uint localChannelNumber, SftpMessage sftpMessage) { this.LocalChannelNumber = localChannelNumber; var messageData = sftpMessage.GetBytes(); var data = new byte[4 + messageData.Length]; ((uint)messageData.Length).GetBytes().CopyTo(data, 0); messageData.CopyTo(data, 4); this.Data = data; }
protected override void OnDataReceived(uint dataTypeCode, byte[] data) { // Add channel data to internal data holder this._data.AddRange(data); while (this._data.Count > 4 + 1) { // Extract packet length var packetLength = (this._data[0] << 24 | this._data[1] << 16 | this._data[2] << 8 | this._data[3]); // Check if complete packet data is available if (this._data.Count < packetLength + 4) { // Wait for complete message to arrive first break; } this._data.RemoveRange(0, 4); // Create buffer to hold packet data var packetData = new byte[packetLength]; // Cope packet data to array this._data.CopyTo(0, packetData, 0, packetLength); // Remove loaded data from _data holder this._data.RemoveRange(0, packetLength); // Load SFTP Message and handle it var response = SftpMessage.Load(this.ProtocolVersion, packetData, this.Encoding); try { var versionResponse = response as SftpVersionResponse; if (versionResponse != null) { this.ProtocolVersion = versionResponse.Version; this._supportedExtensions = versionResponse.Extentions; this._sftpVersionConfirmed.Set(); } else { this.HandleResponse(response as SftpResponse); } } catch (Exception exp) { this.RaiseError(exp); break; } } }
private static SftpMessage Load(uint protocolVersion, byte[] data, SftpMessageTypes messageType, Encoding encoding) { SftpMessage message = null; switch (messageType) { case SftpMessageTypes.Version: message = new SftpVersionResponse(); break; case SftpMessageTypes.Status: message = new SftpStatusResponse(protocolVersion); break; case SftpMessageTypes.Data: message = new SftpDataResponse(protocolVersion); break; case SftpMessageTypes.Handle: message = new SftpHandleResponse(protocolVersion); break; case SftpMessageTypes.Name: message = new SftpNameResponse(protocolVersion, encoding); break; case SftpMessageTypes.Attrs: message = new SftpAttrsResponse(protocolVersion); break; case SftpMessageTypes.ExtendedReply: message = new SftpExtendedReplyResponse(protocolVersion); break; default: throw new NotSupportedException(string.Format(CultureInfo.CurrentCulture, "Message type '{0}' is not supported.", messageType)); } message.LoadBytes(data); message.ResetReader(); message.LoadData(); return(message); }