Ejemplo n.º 1
0
        /// <summary>收到新连接时处理</summary>
        /// <param name="client"></param>
        protected virtual void OnAccept(Socket client)
        {
            var session = CreateSession(client);

            // 设置心跳时间
            client.SetTcpKeepAlive(true);

            if (_Sessions.Add(session))
            {
                //session.ID = g_ID++;
                // 会话改为原子操作,避免多线程冲突
                session.ID = Interlocked.Increment(ref g_ID);
                //WriteLog("{0}新会话 {1}", this, client.Client.RemoteEndPoint);
                session.WriteLog("New {0}", session.Remote.EndPoint);

                if (StatSession != null)
                {
                    StatSession.Increment(1);
                }

                if (NewSession != null)
                {
                    NewSession(this, new SessionEventArgs {
                        Session = session
                    });
                }

                // 自动开始异步接收处理
                if (AutoReceiveAsync)
                {
                    session.ReceiveAsync();
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>创建会话</summary>
        /// <param name="remoteEP"></param>
        /// <returns></returns>
        public virtual ISocketSession CreateSession(IPEndPoint remoteEP)
        {
            if (Disposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            var sessions = _Sessions;

            if (sessions == null)
            {
                return(null);
            }

            // 平均执行耗时260.80ns,其中55%花在sessions.Get上面,Get里面有加锁操作

            if (!Active)
            {
                // 根据目标地址适配本地IPv4/IPv6
                Local.Address = Local.Address.GetRightAny(remoteEP.AddressFamily);

                if (!Open())
                {
                    return(null);
                }
            }

            // 需要查找已有会话,已有会话不存在时才创建新会话
            var session = sessions.Get(remoteEP + "");

            if (session == null)
            {
                var us = new UdpSession(this, remoteEP);
                us.Log        = Log;
                us.LogSend    = LogSend;
                us.LogReceive = LogReceive;
                // UDP不好分会话统计
                //us.StatSend.Parent = StatSend;
                //us.StatReceive.Parent = StatReceive;

                session = us;
                if (sessions.Add(session))
                {
                    //us.ID = g_ID++;
                    // 会话改为原子操作,避免多线程冲突
                    us.ID = Interlocked.Increment(ref g_ID);
                    us.Start();

                    if (StatSession != null)
                    {
                        StatSession.Increment(1);
                    }

                    // 触发新会话事件
                    if (NewSession != null)
                    {
                        NewSession(this, new SessionEventArgs {
                            Session = session
                        });
                    }
                }
            }

            return(session);
        }