Exemple #1
0
        /**
         * opens another SSH connection via port-forwarded connection
         */
        public SSHConnection OpenPortForwardedAnotherConnection(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, string host, int port)
        {
            ProtocolNegotiationHandler pnh = new ProtocolNegotiationHandler(param);
            ChannelSocket s = new ChannelSocket(pnh);

            SSHChannel ch = ForwardPort(s, host, port, "localhost", 0);

            s.SSHChennal = ch;
            return(SSH1Connection.Connect(param, receiver, pnh, s));
        }
        /**
         * opens another SSH connection via port-forwarded connection
         */
        public SSHConnection OpenPortForwardedAnotherConnection(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, string host, int port)
        {
            ChannelSocket s  = new ChannelSocket(null);
            SSHChannel    ch = ForwardPort(s, host, port, "localhost", 0);

            s.SSHChennal = ch;
            VersionExchangeHandler pnh = new VersionExchangeHandler(param, s);

            s.SetHandler(pnh);

            return(ConnectMain(param, receiver, pnh, s));
        }
 public bool ConnectToChannel(string addr, int port)
 {
     EndPoint = new IPEndPoint(IPAddress.Parse(addr), port);
     _synchronizeHandle.Reset();
     ChannelSocket.BeginConnect(addr, port, ConnectCallback, null);
     _synchronizeHandle.WaitOne();
     // ReSharper disable once ConditionIsAlwaysTrueOrFalse
     if (ChannelSocket.Connected && Client.Identity.Guid != null)
     {
         Client.OnClientConnected.Raise(this, new ClientEventArgs(this.Client, this));
         return(true);
     }
     return(false);
 }
 protected virtual void Dispose(bool disposing)
 {
     if (IsDisposed)
     {
         return;
     }
     if (disposing)
     {
         OnPeerConnected = null;
         ConnectedPeers.ForEach(p => p.Disconnect());
         IsActive = false;
         ChannelSocket.Close();
     }
     IsDisposed = true;
 }
        private void AcceptCallback(IAsyncResult res)
        {
            if (!IsActive)
            {
                return;
            }
            var newSock = ChannelSocket.EndAccept(res);

            Peer.Peer newPeer;
            if (IsMainChannel)
            {
                newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId(), SocketIdentity.GenerateGuid()), newSock, BufferSize, this);
                newPeer.OnPeerDisconnected += (o, e2) =>
                {
                    OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer));
                };
                newPeer.Receive();
                AcceptPeer(newPeer);
            }
            else
            {
                newPeer = new Peer.Peer(new SocketIdentity(Manager.GeneratePeerId()), newSock, BufferSize, this);
                PendingConnections.Add(new PendingPeerConnection(newPeer.Identity.Guid, newPeer));
                newPeer.OnPeerDisconnected += (o, e2) =>
                {
                    OnPeerDisconnected.Raise(o, new ChannelEventArgs(this, newPeer));
                };
                newPeer.Receive();
                newPeer.OnPeerRelocationRequest += (sender, e) =>
                {
                    if (PendingConnections.FirstOrDefault(pc => pc.Guid == e.PeerGuid && !pc.IsCancelled) != null)
                    {
                        AcceptPeer(newPeer);
                        OnPeerConnected.Raise(this, new ChannelEventArgs(this, e.Peer));

                        lock (_lockObj)
                            PendingConnections.Remove(PendingConnections.First(pc => pc.Guid == e.PeerGuid));
                    }
                };
            }
            ChannelSocket.BeginAccept(AcceptCallback, null);
        }
        private void ConnectCallback(IAsyncResult res)
        {
            try
            {
                ChannelSocket.EndConnect(res);
            }
            catch
            {
                _synchronizeHandle.Set();
                return;
            }
            Receive();

            var syncPacket = new SynchronizePacket()
            {
                Guid = Client.Identity.Guid
            };

            Send(syncPacket);
        }
        private void ReadLengthCallback(IAsyncResult res)
        {
            try
            {
                var buffObj = (BufferObject)res.AsyncState;
                var readLen = ChannelSocket.EndReceive(res);

                if (readLen >= 4)
                {
                    buffObj.PacketSize = BitConverter.ToInt32(buffObj.RecBuff, 0);
                    if (buffObj.PacketSize < buffObj.BufferSize)
                    {
                        ChannelSocket.BeginReceive(buffObj.RecBuff, 0, buffObj.PacketSize, 0, ReadDataCallback, buffObj);
                    }
                    else
                    {
                        ChannelSocket.BeginReceive(buffObj.RecBuff, 0, buffObj.BufferSize, 0, ReadDataCallback, buffObj);
                    }
                }
            }
            catch
            {
            }
        }
        private void ReadDataCallback(IAsyncResult res)
        {
            var buffObj = (BufferObject)res.AsyncState;

            buffObj.ReadLen    = ChannelSocket.EndReceive(res);
            buffObj.TotalRead += buffObj.ReadLen;

            buffObj.CompleteBuff.AddRange(FastBuffer.SliceBuffer(buffObj.RecBuff, 0, buffObj.ReadLen));
            if (buffObj.CompleteBuff.Count < buffObj.PacketSize)
            {
                // keep reading
                if (buffObj.BufferSize < (buffObj.PacketSize - buffObj.CompleteBuff.Count))
                {
                    ChannelSocket.BeginReceive(buffObj.RecBuff, 0, buffObj.BufferSize, 0, ReadDataCallback, buffObj);
                }
                else
                {
                    ChannelSocket.BeginReceive(buffObj.RecBuff, 0, (buffObj.PacketSize - buffObj.CompleteBuff.Count),
                                               0, ReadDataCallback, buffObj);
                }
            }
            else
            {
                // full message was received
                var dataBuff    = buffObj.CompleteBuff.ToArray();
                var isOperation = BitConverter.ToBoolean(dataBuff, 0);
                int internalId;
                var operationGuid  = Guid.Empty;
                var connectionGuid = Guid.Empty;
                if (isOperation)
                {
                    operationGuid = new Guid(dataBuff.Slice(2, 16));
                    Debug.Print("Received operation guid: " + operationGuid);
                    connectionGuid = new Guid(dataBuff.Slice(18, 16));
                    internalId     = BitConverter.ToInt32(dataBuff, 34);
                }
                else
                {
                    internalId = BitConverter.ToInt32(dataBuff, 2);
                }

                if (_internalPacketTbl.ContainsKey(internalId))
                {
                    var packet = (IDataPacket)Activator.CreateInstance(_internalPacketTbl[internalId]);
                    var ms     = new MemoryStream(dataBuff)
                    {
                        Position = 2
                    };
                    packet.DeserializeFrom(ms);

                    if (packet is SynchronizePacket)
                    {
                        HandleSynchronizePacket(packet as SynchronizePacket);
                    }
                    else if (packet is SequenceInitPacket)
                    {
                        HandleSequenceInitPacket(packet as SequenceInitPacket);
                    }
                    else if (packet is SequencePacket)
                    {
                        HandleSequencePacket(packet as SequencePacket);
                    }
                    else if (packet is PeerRelocationRequestPacket)
                    {
                        HandleRelocationPacket(packet as PeerRelocationRequestPacket);
                    }
                    else if (packet is SocketOperationRequest)
                    {
                        HandleSocketOperationPacket(packet as SocketOperationRequest);
                    }
                }
                else if (ActiveSocketOperations.ContainsKey(operationGuid))
                {
                    using (var ms = new MemoryStream(dataBuff)
                    {
                        Position = 34
                    })
                    {
                        var packet = PacketProcessor.ParsePacket(ms);
                        packet.DeserializeFrom(ms);
                        ActiveSocketOperations[operationGuid].PacketReceived(packet, this);
                    }
                }
                else
                {
                    RawPacketReceived(dataBuff);
                }
                buffObj.ReadLen = 0;
                buffObj.CompleteBuff.Clear();
                buffObj.PacketSize = 0;
                ChannelSocket.BeginReceive(buffObj.RecBuff, 0, 4, 0, ReadLengthCallback, buffObj);
            }
        }
        private void Receive()
        {
            var buffObj = new BufferObject(_bufferSize);

            ChannelSocket.BeginReceive(buffObj.RecBuff, 0, 4, 0, ReadLengthCallback, buffObj);
        }
        private void SendCallback(IAsyncResult res)
        {
            var sent = ChannelSocket.EndSend(res);

            Debug.Print(sent.ToString());
        }