Esempio n. 1
0
        protected override void OnReceivedData(byte[] data, System.Net.IPEndPoint ep, UInt16 proxyId, bool isText)
        {
            UdpServerSession session = getSession(ep.Address.ToString(), ep.Port, proxyId);

            if (session != null)
            {
                lock (session)
                {
                    session.ReceivedBytes  += (data == null ? 0 : data.Length);
                    session.ActiveTime      = DateTime.Now;
                    session.LastReceiveTime = DateTime.Now;
                }
                OnReceivedData(new ReceivedDataEventArgs(data, session, isText));
                session.RaiseReceivedData(data, isText);
            }
            else
            {
                UdpFrame frame = new UdpFrame(0, 0, 0, UdpCommand.UnConnected, null, proxyId);
                SendFrame(frame, ep);
            }
            //修改主连接会话最后接收时间
            session = getSession(ep.Address.ToString(), ep.Port, 0);
            if (session != null)
            {
                lock (session)
                {
                    session.ActiveTime = DateTime.Now;
                }
            }
        }
Esempio n. 2
0
        public void CloseClient(EndPoint ep, UInt16 proxyId)
        {
            if (proxyId == 0)
            {
                List <UdpServerSession> sl          = getSession(ep);
                UdpServerSession        mainSession = null;
                sl.ForEach(s =>
                {
                    if (s.ProxyID != 0)
                    {
                        CloseClient(s.Target, s.ProxyID);
                    }
                    else
                    {
                        mainSession = s;
                    }
                });

                if (mainSession != null)
                {
                    RemoveSession(mainSession);
                }
            }
            else
            {
                UdpServerSession session = getSession(ep, proxyId);
                if (session != null)
                {
                    RemoveSession(session);
                }
            }
        }
Esempio n. 3
0
        protected virtual void OnClientClosed(UdpServerSession session)
        {
            UdpClientConnectedHandler h = ClientClosed;

            if (h != null)
            {
                h(this, session);
            }
        }
Esempio n. 4
0
        private UdpServerSession getSession(string host, int port, UInt16 proxyId)
        {
            UdpServerSession session = null;

            lock (clientList)
            {
                session = clientList.FirstOrDefault(x => x.Target.Host == host && x.Target.Port == port && x.ProxyID == proxyId);
            }

            return(session);
        }
Esempio n. 5
0
        protected override void OnSendPackageError(UdpPackage package, EndPoint target, UInt16 proxyId)
        {
            UdpServerSession session = getSession(target, proxyId);

            if (session != null)
            {
                lock (session)
                {
                    session.ErrorBytes += (package.Data == null ? 0 : package.Data.Length);
                }
            }
            base.OnSendPackageError(package, target, proxyId);
        }
Esempio n. 6
0
        protected override void OnSendedPackage(UdpPackage package, EndPoint target, UInt16 proxyId)
        {
            UdpServerSession session = getSession(target, proxyId);

            if (session != null)
            {
                lock (session)
                {
                    session.SendedBytes += (package.Data == null ? 0 : package.Data.Length);
                    session.LastSendTime = DateTime.Now;
                    session.ActiveTime   = DateTime.Now;
                }
            }
            base.OnSendedPackage(package, target, proxyId);
        }
Esempio n. 7
0
        protected override void SendData(byte[] data, EndPoint target, UInt16 proxyId)
        {
            UdpServerSession session = getSession(target, proxyId);

            if (session != null)
            {
                try
                {
                    //使用客户端连接自己的UdpClient发送数据
                    session.Client.SendData(data);
                }
                catch (SocketException e)
                {
                    session.Client.IsConnected = false;
                    session.Close();
                    OnClientClosed(session);
                }
            }
        }
Esempio n. 8
0
        private UdpServerSession getSession(EndPoint target, UInt16 proxyId)
        {
            string host = "";
            int    port = 0;

            if (target is IPEndPoint)
            {
                IPEndPoint ip = target as IPEndPoint;
                host = ip.Address.ToString();
                port = ip.Port;
            }
            else if (target is DnsEndPoint)
            {
                DnsEndPoint dns = target as DnsEndPoint;
                host = dns.Host;
                port = dns.Port;
            }
            UdpServerSession session = getSession(host, port, proxyId);

            return(session);
        }
Esempio n. 9
0
        protected virtual void RemoveSession(UdpServerSession session)
        {
            UdpServerSession old = getSession(session.Target.Host, session.Target.Port, session.ProxyID);

            if (old != null)
            {
                Logger.Trace("移除会话:{0} : {1} {2}", session.Target.Host, session.Target.Port, session.ProxyID);

                if (old.ProxyID == 0)
                {
                    RemoveCachePackage(new IPEndPoint(IPAddress.Parse(old.Target.Host), old.Target.Port));
                }

                old.Client.Close();
                lock (clientList)
                {
                    clientList.Remove(old);
                }
                RemoveSendQueue(session.Target, session.ProxyID);
                OnClientClosed(session);
                session.OnClosed();
            }
        }
Esempio n. 10
0
        protected override UdpFrame OnReceivedFrameData(byte[] data, System.Net.IPEndPoint ep)
        {
            if (data == null || data.Length < UdpFrame.HeadLength)
            {
                return(null);
            }

            UdpFrame frame = new UdpFrame(data);

            if (frame.Command == UdpCommand.Connect)
            {
                RemoveCachePackage(ep);
                ILogger l = LoggerManager.GetLogger(ep.Address.ToString() + "_" + ep.Port);
                l.Trace("收到连接命令:{0}", ep.ToString());
                //Logger.Trace("收到连接命令:{0}", ep.ToString());

                //连接成功
                UdpServerSession session = null;
                bool             isNew   = false;
                lock (clientList)
                {
                    session = clientList.FirstOrDefault(x => x.Target.Host == ep.Address.ToString() && ep.Port == x.Target.Port && x.ProxyID == frame.ProxyID);
                    if (session == null)
                    {
                        session = SessionFactory.CreateSession(null, new DnsEndPoint(ep.Address.ToString(), ep.Port)) as UdpServerSession;
                        // session = new UdpServerSession();
                        session.StartTime = DateTime.Now;
                        session.ProxyID   = frame.ProxyID;
                        //session.Target = new DnsEndPoint(ep.Address.ToString(), ep.Port);
                        session.Client     = new UdpServerSocketProxy(this, Client, session);
                        session.StartTime  = DateTime.Now;
                        session.ActiveTime = DateTime.Now;

                        clientList.Add(session);
                        isNew = true;
                        l.Trace("添加会话");
                    }
                    else
                    {
                        session.Client             = null;
                        session.ProxyID            = frame.ProxyID;
                        session.Target             = new DnsEndPoint(ep.Address.ToString(), ep.Port);
                        session.Client             = new UdpServerSocketProxy(this, Client, session);
                        session.Client.IsConnected = true;
                        l.Trace("更新会话");
                    }
                    if (isNew)
                    {
                        OnClientConnected(session);
                    }
                }
                UdpFrame ccFrame = new UdpFrame(0, 0, 0, UdpCommand.ConnectConfirm, null, frame.ProxyID);
                SendFrame(ccFrame, session.Target);
                return(null);
            }
            else if (frame.Command == UdpCommand.Close)
            {
                ILogger          l       = LoggerManager.GetLogger(ep.Address.ToString() + "_" + ep.Port);
                UdpServerSession session = getSession(ep.Address.ToString(), ep.Port, frame.ProxyID);
                if (session != null)
                {
                    session.Client.IsConnected = false;
                    l.Trace("收到回话关闭命令:{0}", session.Target);
                    RemoveSession(session);
                }
            }
            else if (frame.Command == UdpCommand.Pong)
            {
                List <UdpServerSession> sl = null;
                lock (clientList)
                {
                    sl = clientList.Where(x => x.Target.Host == ep.Address.ToString() && x.Target.Port == ep.Port).ToList();
                }
                sl.ForEach(s =>
                {
                    s.ActiveTime      = DateTime.Now;
                    s.LastReceiveTime = DateTime.Now;
                });
                //UdpServerSession session = getSession(ep.Address.ToString(), ep.Port, frame.ProxyID);
                //if (session != null)
                //{
                //    session.ActiveTime = DateTime.Now;
                //    session.LastReceiveTime = DateTime.Now;
                //}
            }
            else if (frame.Command == UdpCommand.Ping)
            {
                //UdpServerSession session = getSession(ep.Address.ToString(), ep.Port, frame.ProxyID);
                //if (session != null)
                //{
                //    Logger.Trace("收到Ping命令:{0}", session.Target);
                //    UdpFrame pong = new UdpFrame(0, 0, 0, UdpCommand.Pong, null, frame.ProxyID);
                //    SendFrame(pong, ep);

                //}

                UdpFrame pong = new UdpFrame(0, 0, 0, UdpCommand.Pong, null, frame.ProxyID);
                SendFrame(pong, ep);

                List <UdpServerSession> sl = null;
                lock (clientList)
                {
                    sl = clientList.Where(x => x.Target.Host == ep.Address.ToString() && x.Target.Port == ep.Port).ToList();
                }
                sl.ForEach(s =>
                {
                    s.ActiveTime      = DateTime.Now;
                    s.LastReceiveTime = DateTime.Now;
                });
            }


            return(frame);
        }