Exemple #1
0
        public Session(NetworkComponent network, AChannel channel)
        {
            this.network = network;
            this.channel = channel;

            this.StartRecv();
        }
Exemple #2
0
 public Session(NetworkComponent network, AChannel channel, IMessagePacker messagePacker)
 {
     this.network       = network;
     this.channel       = channel;
     this.messagePacker = messagePacker;
     this.StartRecv();
 }
Exemple #3
0
        private async static void RpcDo(AChannel channel, Opcode opcode, byte[] messageBytes)
        {
            byte[] opcodeBuffer;
            int    id = BitConverter.ToInt32(messageBytes, 2);

            byte[] idBuffer = BitConverter.GetBytes(id);
            try
            {
                opcodeBuffer = BitConverter.GetBytes((ushort)Opcode.RpcResponse);
                byte[] result = await World.Instance.GetComponent <MessageComponent>().RunAsync(opcode, messageBytes);

                channel.SendAsync(new List <byte[]> {
                    opcodeBuffer, idBuffer, result
                });
            }
            catch (Exception e)
            {
                opcodeBuffer = BitConverter.GetBytes((ushort)Opcode.RpcException);
                BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All));
                using (MemoryStream stream = new MemoryStream())
                {
                    formatter.Serialize(stream, e);
                    channel.SendAsync(new List <byte[]> {
                        opcodeBuffer, idBuffer, stream.ToArray()
                    });
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// 接收连接
        /// </summary>
        private async void AcceptChannel()
        {
            while (true)
            {
                AChannel channel = await this.service.GetChannel();

                this.ProcessChannel(channel);
            }
        }
        /// <summary>
        /// 创建一个新Session
        /// </summary>
        public Session Create(IPEndPoint ipEndPoint)
        {
            //Initialize(_Protocal, ipEndPoint);
            AChannel channel = this.Service.ConnectChannel(ipEndPoint);
            Session  session = new Session(this, channel);

            this.sessions.Add(session.Id, session);
            return(session);
        }
Exemple #6
0
        public Session(NetworkClient rNetwork, AChannel rChannel)
        {
            this.Id = STATIC_ID++;

            this.mNetwork = rNetwork;
            this.mChannel = rChannel;

            this.StartRecv();
        }
Exemple #7
0
        // channel删除的时候需要清除与unit id的关联
        private void OnChannelDispose(AChannel channel)
        {
            ChannelUnitInfoComponent channelUnitInfoComponent =
                channel.GetComponent <ChannelUnitInfoComponent>();

            if (channelUnitInfoComponent != null)
            {
                this.unitIdChannels.Remove(channelUnitInfoComponent.UnitId);
            }
        }
Exemple #8
0
        /// <summary>
        /// 接收连接
        /// </summary>
        private async void AcceptChannel()
        {
            while (true)
            {
                AChannel channel = await this.service.GetChannel();

                channel.OnDispose += this.OnChannelDispose;
                ProcessChannel(channel);
            }
        }
Exemple #9
0
        public virtual async Task <Session> Accept()
        {
            AChannel channel = await this.Service.AcceptChannel();

            Session session = EntityFactory.Create <Session, NetworkComponent, AChannel>(this, channel);

            channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
            this.sessions.Add(session.Id, session);
            return(session);
        }
        public virtual async Task <Session> Accept()
        {
            AChannel rChannel = await this.mService.AcceptChannel();

            Session rSession = new Session(this, rChannel);

            rChannel.ErrorCallback += (c, e) => { this.Remove(rSession.Id); };
            this.mSessions.Add(rSession.Id, rSession);
            return(rSession);
        }
Exemple #11
0
        private async Task <Session> Accept()
        {
            AChannel channel = await this.Service.AcceptChannel();

            Session session = new Session(this, channel);

            channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
            this.sessions.Add(session.Id, session);
            return(session);
        }
        /// <summary>
        /// 创建一个新Session,不保存到地址缓存中
        /// </summary>
        public Session GetNew(string address)
        {
            string[] ss      = address.Split(':');
            int      port    = int.Parse(ss[1]);
            string   host    = ss[0];
            AChannel channel = this.Service.ConnectChannel(host, port);
            Session  session = new Session(channel);

            channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
            return(session);
        }
Exemple #13
0
        // 消息回调或者超时回调
        public void RpcCallback(AChannel channel, int id, byte[] buffer, RpcResponseStatus responseStatus)
        {
            Action <byte[], RpcResponseStatus> action;

            if (!this.requestCallback.TryGetValue(id, out action))
            {
                return;
            }
            this.requestCallback.Remove(id);
            action(buffer, responseStatus);
        }
Exemple #14
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) => { this.Remove(session.Id); };
            this.sessions.Add(session.Id, session);
            return(session);
        }
        /// <summary>
        /// 创建一个新Session
        /// </summary>
        public Session Create(string address)
        {
            string[] ss      = address.Split(':');
            int      port    = int.Parse(ss[1]);
            string   host    = ss[0];
            AChannel channel = this.Service.ConnectChannel(host, port);
            Session  session = new Session(this, channel, this.messagePacker);

            channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
            this.sessions.Add(session.Id, session);
            return(session);
        }
Exemple #16
0
 /// <summary>
 /// 创建一个新Session
 /// </summary>
 public virtual Session Create(IPEndPoint ipEndPoint)
 {
     try
     {
         AChannel channel = this.Service.ConnectChannel(ipEndPoint);
         Session  session = EntityFactory.Create <Session, NetworkComponent, AChannel>(this, channel);
         channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
         this.sessions.Add(session.Id, session);
         return(session);
     }
     catch (Exception e)
     {
         Log.Error(e.ToString());
         return(null);
     }
 }
        private async void StartAccept()
        {
            while (true)
            {
                if (this.Id == 0)
                {
                    return;
                }

                AChannel channel = await this.Service.AcceptChannel();

                Session session = new Session(channel);
                channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
                this.Add(session);
            }
        }
Exemple #18
0
        //            this.Id = STATIC_ID++;

        //			this.mNetwork = rNetwork;
        //			this.mChannel = rChannel;
        //            mChannel.Start();

        //            this.StartRecv();
        public Session(NetworkClient rNetwork, AChannel aChannel)
        {
            mNetwork     = rNetwork;
            this.channel = aChannel;
            this.requestCallback.Clear();
            this.Id = IdGenerater.GenerateId();
            long id = this.Id;

            channel.ErrorCallback += (c, e) =>
            {
                this.Network.Remove(id);
            };
            channel.ReadCallback += this.OnRead;

            this.channel.Start();
        }
Exemple #19
0
        /// <summary>
        /// Rpc请求
        /// </summary>
        public Task <T> RpcCall <T, K>(string address, K request, int waitTime = 0)
        {
            AChannel channel = this.service.GetChannel(address);

            ++this.requestId;
            byte[] requestBuffer = MongoHelper.ToBson(request);
            Opcode opcode        = EnumHelper.FromString <Opcode>(request.GetType().Name);

            byte[] opcodeBuffer = BitConverter.GetBytes((ushort)opcode);
            byte[] idBuffer     = BitConverter.GetBytes(this.requestId);
            channel.SendAsync(new List <byte[]> {
                opcodeBuffer, idBuffer, requestBuffer
            });
            var tcs = new TaskCompletionSource <T>();

            this.requestCallback[this.requestId] = (messageBytes, status) =>
            {
                switch (status)
                {
                case RpcResponseStatus.Timeout:
                    tcs.SetException(new Exception($"rpc timeout {opcode} {MongoHelper.ToJson(request)}"));
                    return;

                case RpcResponseStatus.Exception:
                    BinaryFormatter formatter = new BinaryFormatter(null, new StreamingContext(StreamingContextStates.All));
                    Exception       exception;
                    using (MemoryStream stream = new MemoryStream(messageBytes, 6, messageBytes.Length - 6))
                    {
                        exception = (Exception)formatter.Deserialize(stream);
                    }
                    tcs.SetException(exception);
                    return;
                }

                // RpcResponseStatus.Succee
                T response = MongoHelper.FromBson <T>(messageBytes, 6);
                tcs.SetResult(response);
            };

            if (waitTime > 0)
            {
                this.service.Timer.Add(TimeHelper.Now() + waitTime,
                                       () => { this.RpcCallback(channel, this.requestId, null, RpcResponseStatus.Timeout); });
            }
            return(tcs.Task);
        }
Exemple #20
0
        /// <summary>
        /// 接收分发封包
        /// </summary>
        /// <param name="channel"></param>
        private static async void ProcessChannel(AChannel channel)
        {
            while (true)
            {
                byte[] messageBytes = await channel.RecvAsync();

                Opcode opcode = (Opcode)BitConverter.ToUInt16(messageBytes, 0);
                if (!OpcodeHelper.IsClientMessage(opcode))
                {
                    continue;
                }

                ObjectId unitId = channel.GetComponent <ChannelUnitInfoComponent>().UnitId;
                Actor    actor  = World.Instance.GetComponent <ActorComponent>().Get(unitId);
                actor.Add(messageBytes);
            }
        }
Exemple #21
0
        private async void StartAccept()
        {
            while (true)
            {
                if (this.Id == 0)
                {
                    return;
                }

                AChannel channel = await this.Service.AcceptChannel();

                Session session = new Session(this.GetOwner <Scene>(), channel);
                channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
                this.sessions.Add(session.Id, session);
                this.AddToAddressDict(session);
            }
        }
 /// <summary>
 /// 创建一个新Session
 /// </summary>
 public virtual Session Create(string rAddress)
 {
     try
     {
         string[] ss       = rAddress.Split(':');
         int      nPort    = int.Parse(ss[1]);
         string   rHost    = ss[0];
         AChannel rChannel = this.mService.ConnectChannel(rHost, nPort);
         Session  rSession = new Session(this, rChannel);
         rChannel.ErrorCallback += (c, e) => { this.Remove(rSession.Id); };
         this.mSessions.Add(rSession.Id, rSession);
         return(rSession);
     }
     catch (Exception e)
     {
         Log.Error(e.ToString());
         return(null);
     }
 }
Exemple #23
0
 /// <summary>
 /// 创建一个新Session
 /// </summary>
 public virtual Session Create(string address)
 {
     try
     {
         string[] ss      = address.Split(':');
         int      port    = int.Parse(ss[1]);
         string   host    = ss[0];
         AChannel channel = this.Service.ConnectChannel(host, port);
         Session  session = new Session(this, channel);
         channel.ErrorCallback += (c, e) => { this.Remove(session.Id); };
         this.sessions.Add(session.Id, session);
         return(session);
     }
     catch (Exception e)
     {
         Log.Error(e.ToString());
         return(null);
     }
 }
Exemple #24
0
        /// <summary>
        /// 接收分发封包
        /// </summary>
        /// <param name="channel"></param>
        private async void ProcessChannel(AChannel channel)
        {
            while (true)
            {
                byte[] messageBytes = await channel.RecvAsync();

                Opcode opcode = (Opcode)BitConverter.ToUInt16(messageBytes, 0);

                // rpc异常
                if (opcode == Opcode.RpcException)
                {
                    int id = BitConverter.ToInt32(messageBytes, 2);
                    this.RpcCallback(channel, id, messageBytes, RpcResponseStatus.Exception);
                    continue;
                }

                // 表示消息是rpc响应消息
                if (opcode == Opcode.RpcResponse)
                {
                    int id = BitConverter.ToInt32(messageBytes, 2);
                    this.RpcCallback(channel, id, messageBytes, RpcResponseStatus.Succee);
                    continue;
                }

                // 如果是server message(发给client的消息),说明这是gate server,需要根据unitid查到channel,进行发送
                if (OpcodeHelper.IsServerMessage(opcode))
                {
                    byte[] idBuffer = new byte[12];
                    Array.Copy(messageBytes, 2, idBuffer, 0, 12);
                    ObjectId unitId = new ObjectId(idBuffer);
                    byte[]   buffer = new byte[messageBytes.Length - 6];
                    Array.Copy(messageBytes, 6, buffer, 0, buffer.Length);
                    World.Instance.GetComponent <GateNetworkComponent>().SendAsync(unitId, buffer);
                    continue;
                }

                // 处理Rpc请求,并且返回结果
                RpcDo(channel, opcode, messageBytes);
            }
        }
 protected void OnError(AChannel channel, SocketError e)
 {
     this.errorCallback(channel, e);
 }
Exemple #26
0
 public void Awake(NetworkComponent net, AChannel c)
 {
     this.channel = c;
     this.requestCallback.Clear();
 }
Exemple #27
0
 public void Awake(NetworkComponent network, AChannel channel)
 {
     this.Get().Awake(network, channel);
 }
Exemple #28
0
 public Session(NetworkComponent network, AChannel channel) : base(EntityType.Session)
 {
     this.network = network;
     this.channel = channel;
     this.StartRecv();
 }
 protected void OnAccept(AChannel channel)
 {
     this.acceptCallback.Invoke(channel);
 }
        public void OnAccept(AChannel channel)
        {
            Session session = new Session(this, channel);

            this.sessions.Add(session.Id, session);
        }