protected virtual void ConnectChannel() { m_channel = m_session.openChannel(ChannelType); this.OnChannelReceived(); m_channel.connect(); this.OnConnected(); }
private static AChannel MapMediaChannel( MediaServicesAccountConfig configMediaServicesAccount, MediaService mediaService, MediaChannel mediaChannel ) { var achannel = new AChannel(); string originId = configMediaServicesAccount.OriginMappings .Denull() .Where(x => x.ChannelId == mediaChannel.Id) .Select(x => x.OriginId) .FirstOrDefault(); achannel.Origin = new AOrigin { Id = originId }; achannel.Programs = mediaService.Programs .Denull() .Where(mp => mp.ChannelId == mediaChannel.Id) .Select(mp => new AProgram() { Asset = new AAsset() } ) .ToList(); return(achannel); }
//address表示ip:端口 public Session ConnectServer(string address) { AChannel channel = this.Service.ConnectChannel(address); channel.Start(); Session session = new Session(channel); return(session); }
public Session(ETClient rNetwork, AChannel rChannel) { this.Id = STATIC_ID++; this.mNetwork = rNetwork; this.mChannel = rChannel; this.StartRecv(); }
static bool TransmitPacketB(byte[] buffer, int length) { if (Random.Next(2) == 0) { return(false); } AChannel.ReceivePacket(buffer, 0, length); return(true); }
/// <summary> /// 有用户连接进来了 /// </summary> /// <param name="channel"></param> public void OnAcceptClient(AChannel channel) { Console.WriteLine("一个用户连接进来了"); Session s = new Session(); s.Awake(channel); s.Start(); s.OnReadCallBack += OnRead; }
public virtual async Task <Session> Accept() { AChannel rChannel = await this.mService.AcceptChannel(); Session rSession = new Session(this, rChannel); rChannel.ErrorCallback += (c, e) => { this.Remove(rSession.Id); }; this.mSessions.Add(rSession.Id, rSession); return(rSession); }
private void Ac_ReadCallback(AChannel ac, MemoryStream ms) { var str_rv = Encoding.UTF8.GetString(ms.ToArray()); Log.Debug($"{ac.Id}Msg:{str_rv}"); var word_byts = Encoding.UTF8.GetBytes("Server:" + str_rv); ms.Write(word_byts, 0, word_byts.Length); ms.Position = 0; ac.Send(ms); }
public void Remove(AChannel channel) { UChannel tChannel = channel as UChannel; if (tChannel == null) { return; } this.idChannels.Remove(channel.Id); this.channels.Remove(channel.RemoteAddress); }
public void Remove(AChannel channel) { TChannel tChannel = channel as TChannel; if (tChannel == null) { return; } this.idChannels.Remove(channel.Id); this.channels.Remove(channel.RemoteAddress); this.timerManager.Remove(tChannel.SendTimer); }
/// <summary> /// Creates a directory on the remot server /// </summary> /// <param name="dir">The new directory</param> public override void Mkdir(string dir) { SCP_CheckConnectivity(); AChannel channel = null; Stream server = null; m_cancelled = false; SCP_ConnectTo(out channel, out server, dir, true); SCP_EnterIntoDir(server, dir); channel.disconnect(); }
private async void ServerEvent(IService service) { AChannel channel = await service.GetChannel(); for (int i = 0; i < echoTimes; ++i) { byte[] bytes = await channel.RecvAsync(); CollectionAssert.AreEqual("0123456789".ToByteArray(), bytes); Array.Reverse(bytes); channel.SendAsync(bytes); } }
/// <summary> /// Closes the SSH subsystem /// </summary> public virtual void Close() { if (m_channel != null) { m_channel.disconnect(); m_channel = null; } if (m_session != null) { m_session.disconnect(); m_session = null; } }
private async void ClientEvent(IService service, string hostName, ushort port) { AChannel channel = service.GetChannel(hostName, port); for (int i = 0; i < echoTimes; ++i) { channel.SendAsync("0123456789".ToByteArray()); byte[] bytes = await channel.RecvAsync(); CollectionAssert.AreEqual("9876543210".ToByteArray(), bytes); } this.barrier.RemoveParticipant(); }
/// <summary> /// Copies a file from local machine to a remote SSH machine. /// </summary> /// <param name="localPath">The local file path.</param> /// <param name="remotePath">The path of the remote file.</param> public void To(string localPath, string remotePath, bool _recursive) { SCP_CheckConnectivity(); AChannel channel = null; Stream server = null; m_cancelled = false; try { //if we are sending a single file if (File.Exists(localPath)) { SCP_ConnectTo(out channel, out server, remotePath, _recursive); SCP_SendFile(server, localPath, remotePath); channel.disconnect(); } //else, if we are sending a local directory else if (Directory.Exists(localPath)) { if (!_recursive) { throw new SshTransferException(Path.GetFileName("'" + localPath) + "' is a directory, you should use recursive transfer."); } SCP_ConnectTo(out channel, out server, remotePath, true); ToRecursive(server, localPath, remotePath); channel.disconnect(); } else { throw new SshTransferException("File not found: " + localPath); } } catch (Exception e) { if (Verbos) { Console.WriteLine("Error: " + e.Message); } //SendEndMessage(remoteFile, localFile, filesize,filesize, "Transfer ended with an error."); try { channel.disconnect(); } catch { } throw e; } }
private async void SocketConnectAsync(AChannel channel) { while (true) { try { await channel.ConnectAsync(); break; } catch (Exception e) { Log.Trace(e.ToString()); } await this.Timer.Sleep(5000); } }
public void request(Session session, AChannel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); // send // byte SSH_MSG_CHANNEL_REQUEST(98) // uint32 recipient channel // string request type // "shell" // boolean want reply // 0 packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(StringAux.getBytes("shell")); buf.putByte((byte)(waitForReply() ? 1 : 0)); session.write(packet); }
/// <summary> /// 创建一个新Session /// </summary> public virtual Session Create(string rAddress) { try { string[] ss = rAddress.Split(':'); int nPort = int.Parse(ss[1]); string rHost = ss[0]; AChannel rChannel = this.mService.ConnectChannel(rHost, nPort); Session rSession = new Session(this, rChannel); rChannel.ErrorCallback += (c, e) => { this.Remove(rSession.Id); }; this.mSessions.Add(rSession.Id, rSession); return(rSession); } catch (Exception e) { Log.Error(e.ToString()); return(null); } }
/// <summary> /// Connect a channel to the remote server using the 'SCP From' command ('scp -f') /// </summary> /// <param name="channel">Will contain the new connected channel</param> /// <param name="server">Will contaun the new connected server I/O stream</param> /// <param name="rfile">The remote path on the server</param> /// <param name="recursive">Idicate a recursive scp transfer</param> protected void SCP_ConnectFrom(out AChannel channel, out Stream server, string rfile, bool recursive) { string scpCommand = "scp -f "; if (recursive) { scpCommand += "-r "; } scpCommand += "\"" + rfile + "\""; channel = (ChannelExec)m_session.openChannel(ChannelType); ((ChannelExec)channel).setCommand(scpCommand); server = new LibSterileSSH.CombinedStream (channel.getInputStream(), channel.getOutputStream()); channel.connect(); //SCP_CheckAck(server); }
public void request(Session session, AChannel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); bool reply = waitForReply(); if (reply) { channel.reply = -1; } packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(StringAux.getBytes("subsystem")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putString(StringAux.getBytes("sftp")); session.write(packet); if (reply) { while (channel.reply == -1) { try { System.Threading.Thread.Sleep(10); } catch //(Exception ee) { } } if (channel.reply == 0) { throw new SshClientException("failed to send sftp request"); } } }
public void request(Session session, AChannel channel) { Buffer buf = new Buffer(); Packet packet = new Packet(buf); //byte SSH_MSG_CHANNEL_REQUEST //uint32 recipient_channel //string "window-change" //boolean FALSE //uint32 terminal width, columns //uint32 terminal height, rows //uint32 terminal width, pixels //uint32 terminal height, pixels packet.reset(); buf.putByte((byte)Session.SSH_MSG_CHANNEL_REQUEST); buf.putInt(channel.getRecipient()); buf.putString(StringAux.getBytes("window-change")); buf.putByte((byte)(waitForReply() ? 1 : 0)); buf.putInt(width_columns); buf.putInt(height_rows); buf.putInt(width_pixels); buf.putInt(height_pixels); session.write(packet); }
public void Accept(AChannel ac) { ac.ReadCallback += Ac_ReadCallback; Log.Debug($"{ac.Id}:Accept"); }
/// <summary> /// Copies a file from a remote SSH machine to the local machine using SCP. /// </summary> /// <param name="remoteFile">The remmote file name</param> /// <param name="localPath">The local destination path</param> /// <param name="recursive">Value indicating whether a recursive transfer should take place</param> public void From(string remoteFile, string localPath, bool _recursive) { SCP_CheckConnectivity(); AChannel channel = null; Stream server = null; m_cancelled = false; int filesize = 0; String filename = null; string cmd = null; try { String dir = null; if (Directory.Exists(localPath)) { dir = Path.GetFullPath(localPath); } SCP_ConnectFrom(out channel, out server, remoteFile, _recursive); byte[] buf = new byte[1024]; // send '\0' SCP_SendAck(server); int c = SCP_CheckAck(server); //parse scp commands while ((c == 'D') || (c == 'C') || (c == 'E')) { if (m_cancelled) { break; } cmd = "" + (char)c; if (c == 'E') { c = SCP_CheckAck(server); dir = Path.GetDirectoryName(dir); if (Verbos) { Console.WriteLine("E"); } //send '\0' SCP_SendAck(server); c = (char)SCP_CheckAck(server); continue; } // read '0644 ' or '0755 ' server.Read(buf, 0, 5); for (int i = 0; i < 5; i++) { cmd += (char)buf[i]; } //reading file size filesize = 0; while (true) { server.Read(buf, 0, 1); if (buf[0] == ' ') { break; } filesize = filesize * 10 + (buf[0] - '0'); } //reading file name for (int i = 0; ; i++) { server.Read(buf, i, 1); if (buf[i] == (byte)0x0a) { filename = StringAux.getString(buf, 0, i); break; } } cmd += " " + filesize + " " + filename; // send '\0' SCP_SendAck(server); //Receive file if (c == 'C') { if (Verbos) { Console.WriteLine("Sending file modes: " + cmd); } SCP_ReceiveFile(server, remoteFile, dir == null ? localPath : dir + "/" + filename, filesize); if (m_cancelled) { break; } // send '\0' SCP_SendAck(server); } //Enter directory else if (c == 'D') { if (dir == null) { if (File.Exists(localPath)) { throw new SshTransferException("'" + localPath + "' is not a directory"); } dir = localPath; Directory.CreateDirectory(dir); } if (Verbos) { Console.WriteLine("Entering directory: " + cmd); } dir += "/" + filename; Directory.CreateDirectory(dir); } c = SCP_CheckAck(server); } channel.disconnect(); } catch (Exception e) { if (Verbos) { Console.WriteLine("Error: " + e.Message); } try { channel.disconnect(); } catch { } throw e; } }
private static void AChannelError(BenchmarkClientComponent self, AChannel aChannel, int errorCode) { ShowErrorMessage(self, errorCode); }
private void KService_DisConnectCallback(AChannel ac) { Log.Debug($"{ac.Id}:disconnect"); }
private void Ac_ReadCallback(AChannel ac, MemoryStream ms) { Log.Debug(Encoding.UTF8.GetString(ms.ToArray())); }
static bool TransmitPacketB(byte[] buffer, int length) { AChannel.ReceivePacket(buffer, 0, length); return(true); }
public void OnGUI() { if (ChannelType == NetworkProtocol.TCP) { GUILayout.BeginVertical(); if (GUILayout.Button("Connect", GUILayout.Width(200))) { m_AChannel = (TChannel)m_AService.ConnectChannel(NetHelper.ToIPEndPoint("127.0.0.1", 2500)); m_AChannel.ReadCallback += AChannel_ReadCallback; } if (GUILayout.Button("Send", GUILayout.Width(200))) { using (var mem = new MemoryStream()) { var word_byts = Encoding.UTF8.GetBytes("Hello Tcp!"); mem.Write(word_byts, 0, word_byts.Length); mem.Position = 0; m_AChannel.Send(mem); } } if (GUILayout.Button("DisConnect", GUILayout.Width(200))) { m_AChannel.DisConnect(); } GUILayout.EndVertical(); } else if (ChannelType == NetworkProtocol.KCP) { GUILayout.BeginVertical(); if (GUILayout.Button("Connect", GUILayout.Width(200))) { m_AChannel = m_AService.ConnectChannel(NetHelper.ToIPEndPoint("127.0.0.1", 2000)); m_AChannel.ReadCallback += AChannel_ReadCallback; } if (GUILayout.Button("Send", GUILayout.Width(200))) { using (var mem = new MemoryStream()) { var word_byts = Encoding.UTF8.GetBytes("Hello Kcp!"); mem.Write(word_byts, 0, word_byts.Length); mem.Position = 0; m_AChannel.Send(mem); } } if (GUILayout.Button("DisConnect", GUILayout.Width(200))) { m_AChannel.DisConnect(); } GUILayout.EndVertical(); } else if (ChannelType == NetworkProtocol.WebSocket) { GUILayout.BeginVertical(); if (GUILayout.Button("Connect", GUILayout.Width(200))) { m_AChannel = m_AService.ConnectChannel("ws://127.0.0.1:3000/"); m_AChannel.ReadCallback += AChannel_ReadCallback; } if (GUILayout.Button("Send", GUILayout.Width(200))) { using (var mem = new MemoryStream()) { var word_byts = Encoding.UTF8.GetBytes("Hello websocket!"); mem.Write(word_byts, 0, word_byts.Length); mem.Position = 0; m_AChannel.Send(mem); } } if (GUILayout.Button("DisConnect", GUILayout.Width(200))) { m_AChannel.DisConnect(); } GUILayout.EndVertical(); } }
private void AChannel_ReadCallback(AChannel ac, MemoryStream ms) { Log.Debug($"{ac.Id}Msg:{Encoding.UTF8.GetString(ms.ToArray())}"); }
//收取断开消息 private void AService_DisConnectedCallback(AChannel ac) { Log.Debug($"{ac.Id}:DisConnect"); }