/// <summary> /// 非同期送信コールバック /// </summary> /// <param name="ar"></param> private void SendCallBack(IAsyncResult ar) { // オブジェクト変換 SshClientSendStream stream = (SshClientSendStream)ar.AsyncState; // 送信完了 stream.ShellStream.EndWrite(ar); // ロギング this.Logger.InfoFormat("送信データ - {0:#,0} byte:\n{1}", stream.Stream.Length, Common.Diagnostics.Debug.Dump(1, stream.Stream)); // 送信通知 this.OnSendNotify.Set(); // イベント呼出 if (this.OnSend != null) { // イベントパラメータ生成 SshClientSendEventArgs eventArgs = new SshClientSendEventArgs() { ConnectionInfo = stream.ConnectionInfo, Size = (int)stream.Stream.Length, Stream = stream.Stream, }; // イベント呼出し this.OnSend(this, eventArgs); } // ShellStreamフラッシュ stream.ShellStream.Flush(); // 送信通知リセット this.OnSendNotify.Reset(); }
/// <summary> /// 送信 /// </summary> /// <param name="stream"></param> public void Send(MemoryStream stream) { // 送信サイズ判定 if (stream.Length == 0) { // 送信するものがないので、何もしない return; } // Taskオブジェクト生成 using (CancellationTokenSource source = new CancellationTokenSource()) { // タイムアウト設定 source.CancelAfter(this.m_Timeout.Send); // Task開始 Task task = Task.Factory.StartNew(() => { // 送信 this.m_ShellStream.Write(stream.ToArray(), 0, (int)stream.Length); }, source.Token); try { // タスク待ち task.Wait(source.Token); // 通知リセット this.OnSendNotify.Reset(); // イベント呼出判定 if (this.OnSend != null) { // イベントパラメータ設定 SshClientSendEventArgs _SshClientSendEventArgs = new SshClientSendEventArgs() { ConnectionInfo = this.m_ConnectionInfo, Size = (int)stream.Length, Stream = stream, }; // イベント呼出し this.OnSend(this, _SshClientSendEventArgs); } } catch (OperationCanceledException ex) { // 通知リセット this.OnSendNotify.Reset(); // 例外 throw new SshClientExceptions("送信(SSH)に失敗しました:[" + this.m_IpAddress + ":" + this.m_Port.ToString() + "]", ex); } catch (AggregateException ex) { // 通知リセット this.OnSendNotify.Reset(); // 例外 throw new SshClientExceptions("送信(SSH)に失敗しました:[" + this.m_IpAddress + ":" + this.m_Port.ToString() + "]", ex); } } }