Esempio n. 1
0
        private void TransferTcp_Ip(IAsyncResult ar)
        {
            RelationTcp relation = (RelationTcp)ar.AsyncState;
            int         length   = 0;

            EasyOp.Do(() =>
            {
                length = relation.readTcp.GetStream().EndRead(ar);
            }, () =>
            {
                if (length <= 0 || !EasyOp.Do(() =>
                {
                    relation.writeTcp.BeginSend(relation.buffer.Take(length).ToArray());
                    StartTransferTcp_Ip(relation);
                }))
                {
                    SafeClose(relation.readTcp);
                    SafeClose(relation.writeTcp);
                }
            }, ex =>
            {
                SafeClose(relation.readTcp);
                SafeClose(relation.writeTcp);
            });
        }
        private void TransferTcp_Ip(IAsyncResult ar)
        {
            RelationTcp relation = (RelationTcp)ar.AsyncState;

            if (relation.readSs.CanRead)
            {
                int length = 0;
                try
                {
                    length = relation.readSs.EndRead(ar);
                }
                catch { }
                if (length > 0)
                {
                    if (relation.writeSs.CanWrite)
                    {
                        try
                        {
                            relation.writeSs.Write(relation.buffer.Take(length).ToArray(), 0, length);
                            StartTransferTcp_Ip(relation);
                            return;
                        }
                        catch { }
                    }
                }
            }
            relation.readSs.Close(3000);
            relation.writeSs.Close(3000);
            relation.readTcp.Close();
            relation.writeTcp.Close();
        }
        /// <summary>
        /// 直接转发类型的端口监听,接收新tcp回调方法
        /// </summary>
        /// <param name="ar"></param>
        public void AcceptSocket_Ip(IAsyncResult ar)
        {
            ListenSt    st       = (ListenSt)ar.AsyncState;
            TcpListener listener = st.listener;
            PortMapItem item     = st.item;
            Socket      socket   = null;

            try
            {
                socket = listener.EndAcceptSocket(ar);
            }
            catch (Exception ex)
            {
                LogUtils.Error(ex.ToString());
                return;
            }
            listener.BeginAcceptSocket(AcceptSocket_Ip, st);
            P2PTcpClient tcpClient = new P2PTcpClient(socket);
            P2PTcpClient ipClient  = null;

            try
            {
                ipClient = new P2PTcpClient(item.RemoteAddress, item.RemotePort);
            }
            catch (Exception ex)
            {
                tcpClient?.SafeClose();
                LogUtils.Debug($"建立隧道失败:{item.LocalPort}->{item.RemoteAddress}:{item.RemotePort}{Environment.NewLine}{ex}");
            }
            if (ipClient.Connected)
            {
                tcpClient.ToClient = ipClient;
                ipClient.ToClient  = tcpClient;
                RelationTcp toRelation = new RelationTcp();
                toRelation.readTcp  = tcpClient;
                toRelation.readSs   = tcpClient.GetStream();
                toRelation.writeTcp = tcpClient.ToClient;
                toRelation.writeSs  = tcpClient.ToClient.GetStream();
                toRelation.buffer   = new byte[P2PGlobal.P2PSocketBufferSize];
                RelationTcp fromRelation = new RelationTcp();
                fromRelation.readTcp  = toRelation.writeTcp;
                fromRelation.readSs   = toRelation.writeSs;
                fromRelation.writeTcp = toRelation.readTcp;
                fromRelation.writeSs  = toRelation.readSs;
                fromRelation.buffer   = new byte[P2PGlobal.P2PSocketBufferSize];
                StartTransferTcp_Ip(toRelation);
                StartTransferTcp_Ip(fromRelation);
            }
        }
Esempio n. 4
0
 private void StartTransferTcp_Ip(RelationTcp tcp)
 {
     tcp.readTcp.GetStream().BeginRead(tcp.buffer, 0, tcp.buffer.Length, TransferTcp_Ip, tcp);
 }
Esempio n. 5
0
        public void AcceptSocket_Ip(IAsyncResult ar)
        {
            ListenSt    st       = (ListenSt)ar.AsyncState;
            TcpListener listener = st.listener;
            PortMapItem item     = st.item;
            Socket      socket   = null;

            EasyOp.Do(() =>
            {
                socket = listener.EndAcceptSocket(ar);
            }, () =>
            {
                EasyOp.Do(() =>
                {
                    listener.BeginAcceptSocket(AcceptSocket_Ip, st);
                }, ex =>
                {
                    LogUtils.Error($"端口监听失败:{Environment.NewLine}{ex}");
                });
                P2PTcpClient tcpClient = null;
                EasyOp.Do(() =>
                {
                    tcpClient = new P2PTcpClient(socket);
                }, () =>
                {
                    P2PTcpClient ipClient = null;
                    EasyOp.Do(() =>
                    {
                        ipClient = new P2PTcpClient(item.RemoteAddress, item.RemotePort);
                    }, () =>
                    {
                        tcpClient.ToClient       = ipClient;
                        ipClient.ToClient        = tcpClient;
                        RelationTcp toRelation   = new RelationTcp();
                        toRelation.readTcp       = tcpClient;
                        toRelation.writeTcp      = tcpClient.ToClient;
                        toRelation.buffer        = new byte[P2PGlobal.P2PSocketBufferSize];
                        RelationTcp fromRelation = new RelationTcp();
                        fromRelation.readTcp     = toRelation.writeTcp;
                        fromRelation.writeTcp    = toRelation.readTcp;
                        fromRelation.buffer      = new byte[P2PGlobal.P2PSocketBufferSize];
                        EasyOp.Do(() =>
                        {
                            StartTransferTcp_Ip(toRelation);
                            StartTransferTcp_Ip(fromRelation);
                        }, ex =>
                        {
                            LogUtils.Debug($"建立隧道失败:{Environment.NewLine}{ex}");
                            EasyOp.Do(() => ipClient.SafeClose());
                            EasyOp.Do(() => tcpClient.SafeClose());
                        });
                    }, ex =>
                    {
                        LogUtils.Debug($"建立隧道失败:{item.LocalPort}->{item.RemoteAddress}:{item.RemotePort}{Environment.NewLine}{ex}");
                        EasyOp.Do(() => tcpClient.SafeClose());
                    });
                }, ex =>
                {
                    LogUtils.Debug($"处理新接入Tcp时发生错误:{Environment.NewLine}{ex}");
                    EasyOp.Do(() => socket?.SafeClose());
                });
            }, ex =>
            {
                LogUtils.Debug($"获取新接入的Tcp连接失败:{Environment.NewLine}{ex}");
                EasyOp.Do(() =>
                {
                    listener.BeginAcceptSocket(AcceptSocket_Ip, st);
                }, exx =>
                {
                    LogUtils.Error($"端口监听失败:{Environment.NewLine}{exx}");
                });
            });
        }