예제 #1
0
        public virtual async Task <Session> Accept()
        {
            AChannel channel = await this.Service.AcceptChannel();

            Session session = ComponentFactory.CreateWithId <Session, NetworkComponent, AChannel>(IdGenerater.GenerateId(), this, channel);

            session.Parent         = this;
            channel.ErrorCallback += (c, e) =>
            {
                session.Error = e;
                this.Remove(session.Id);
            };

            channel.ReadCallback += (packet) => { session.OnRead(packet); };

            this.sessions.Add(session.Id, session);
            return(session);
        }
예제 #2
0
        /// <summary>
        /// 创建一个新Session
        /// </summary>
        public virtual Session Create(IPEndPoint ipEndPoint)
        {
            try
            {
                AChannel channel = this.Service.ConnectChannel(ipEndPoint);
                Session  session = ComponentFactory.CreateWithId <Session, NetworkComponent, AChannel>(IdGenerater.GenerateId(), this, channel);
                session.Parent         = this;
                channel.ErrorCallback += (c, e) =>
                {
                    session.Error = e;
                    this.Remove(session.Id);
                };

                channel.ReadCallback += (packet) => { session.OnRead(packet); };

                this.sessions.Add(session.Id, session);
                return(session);
            }
            catch (Exception e)
            {
                Log.Error(e);
                return(null);
            }
        }