Exemple #1
0
        public long GenerateInstanceId()
        {
            uint time = TimeSinceThisYear();

            if (time > this.lastInstanceIdTime)
            {
                this.lastInstanceIdTime = time;
                this.instanceIdValue    = 0;
            }
            else
            {
                ++this.instanceIdValue;

                if (this.instanceIdValue > IdGenerater.Mask18bit - 1) // 18bit
                {
                    ++this.lastInstanceIdTime;                        // 借用下一秒
                    this.instanceIdValue = 0;

                    Log.Error($"instanceid count per sec overflow: {time} {this.lastInstanceIdTime}");
                }
            }

            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(this.lastInstanceIdTime, GlobalDefine.Options.Process, this.instanceIdValue);

            return(instanceIdStruct.ToLong());
        }
Exemple #2
0
        public static ETTask <IActorResponse> Call(this ActorMessageSenderComponent self, long actorId, IActorRequest message, bool exception = true)
        {
            if (actorId == 0)
            {
                throw new Exception($"actor id is 0: {MongoHelper.ToJson(message)}");
            }

            var tcs = new ETTaskCompletionSource <IActorResponse>();

            int              process          = IdGenerater.GetProcess(actorId);
            string           address          = StartProcessConfigCategory.Instance.Get(process).InnerAddress;
            Session          session          = NetInnerComponent.Instance.Get(address);
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);

            instanceIdStruct.Process = IdGenerater.Process;
            message.ActorId          = instanceIdStruct.ToLong();
            message.RpcId            = ++self.RpcId;

            self.requestCallback.Add(message.RpcId, new ActorMessageSender((response) =>
            {
                if (exception && ErrorCode.IsRpcNeedThrowException(response.Error))
                {
                    tcs.SetException(new Exception($"Rpc error: {MongoHelper.ToJson(response)}"));
                    return;
                }

                tcs.SetResult(response);
            }));
            session.Send(message);

            return(tcs.Task);
        }
Exemple #3
0
        public long GenerateInstanceId()
        {
            uint time = TimeSinceThisYear();

            if (time == this.lastInstanceIdTime)
            {
                ++this.instanceIdThisSecCount;
            }
            else
            {
                this.lastInstanceIdTime     = time;
                this.instanceIdThisSecCount = 1;
            }
            if (this.instanceIdThisSecCount > IdGenerater.Mask18bit - 1)
            {
                Log.Error($"instanceid count per sec overflow: {this.instanceIdThisSecCount}");
            }


            if (++this.instanceIdValue > IdGenerater.Mask18bit - 1) // 18bit
            {
                this.instanceIdValue = 0;
            }
#if SERVER
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(time, Game.Options.Process, this.instanceIdValue);
#else
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(time, 0, this.instanceIdValue);
#endif
            return(instanceIdStruct.ToLong());
        }
Exemple #4
0
        public void EndInit()
        {
            this.Type = EnumHelper.FromString <SceneType>(this.SceneType);
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(this.Process, (uint)this.Id);

            this.SceneId = instanceIdStruct.ToLong();
        }
Exemple #5
0
        public override void EndInit()
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct((int)this.Id, 0);

            this.SceneId = instanceIdStruct.ToLong();
            Log.Info($"StartProcess info: {this.MachineId} {this.Id} {this.SceneId}");
        }
Exemple #6
0
        public ProcessActorId(long actorId)
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);

            this.Process             = instanceIdStruct.Process;
            instanceIdStruct.Process = GlobalDefine.Options.Process;
            this.ActorId             = instanceIdStruct.ToLong();
        }
Exemple #7
0
        private static async ETTask SynAsync(GetRouterComponent self, long gateid, long channelid)
        {
            self.CancellationToken = new ETCancellationToken();
            self.Tcs = ETTask <string> .Create();

            //value是对应gate的scene.
            var  insid      = new InstanceIdStruct(gateid);
            uint localConn  = (uint)((ulong)channelid & uint.MaxValue);
            var  routerlist = await GetRouterListFake();

            if (routerlist == null)
            {
                self.Tcs?.SetResult("");
                self.Tcs = null;
                Log.Error("从cdn获取路由失败");
                return;
            }
            Log.Debug("路由数量:" + routerlist.Length.ToString());
            Log.Debug("gateid:" + insid.Value.ToString());
            byte[] buffer = self.cache;
            buffer.WriteTo(0, KcpProtocalType.RouterSYN);
            buffer.WriteTo(1, localConn);
            buffer.WriteTo(5, insid.Value);
            for (int i = 0; i < self.ChangeTimes; i++)
            {
                string router = routerlist.RandomArray();
                Log.Debug("router:" + router);
                self.socket.SendTo(buffer, 0, 9, SocketFlags.None, NetworkHelper.ToIPEndPoint(router));
                var returnbool = await TimerComponent.Instance.WaitAsync(300, self.CancellationToken);

                if (returnbool == false)
                {
                    Log.Debug("提前取消了.可能连接上了");
                    return;
                }
            }
            await TimerComponent.Instance.WaitAsync(1300, self.CancellationToken);

            self.Tcs?.SetResult("");
            self.Tcs = null;
            Log.Debug("三次失败.获取路由失败");
        }
Exemple #8
0
        public void Dispatch(Session session, MemoryStream memoryStream)
        {
            ushort opcode = 0;

            try
            {
                long actorId = BitConverter.ToInt64(memoryStream.GetBuffer(), Packet.ActorIdIndex);
                opcode = BitConverter.ToUInt16(memoryStream.GetBuffer(), Packet.OpcodeIndex);
                Type   type    = null;
                object message = null;
#if SERVER
                // 内网收到外网消息,有可能是gateUnit消息,还有可能是gate广播消息
                if (OpcodeTypeComponent.Instance.IsOutrActorMessage(opcode))
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = GlobalDefine.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();


                    Entity entity = Game.EventSystem.Get(realActorId);
                    if (entity == null)
                    {
                        type    = OpcodeTypeComponent.Instance.GetType(opcode);
                        message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);
                        Log.Error($"not found actor: {session.DomainScene().Name}  {opcode} {realActorId} {message}");
                        return;
                    }

                    if (entity is Session gateSession)
                    {
                        // 发送给客户端
                        memoryStream.Seek(Packet.OpcodeIndex, SeekOrigin.Begin);
                        gateSession.Send(0, memoryStream);
                        return;
                    }
                }
#endif


                type    = OpcodeTypeComponent.Instance.GetType(opcode);
                message = MessageSerializeHelper.DeserializeFrom(opcode, type, memoryStream);

                if (message is IResponse iResponse && !(message is IActorResponse))
                {
                    session.OnRead(opcode, iResponse);
                    return;
                }

                OpcodeHelper.LogMsg(session.DomainZone(), opcode, message);

                // 收到actor消息,放入actor队列
                switch (message)
                {
                case IActorRequest iActorRequest:
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    int fromProcess = instanceIdStruct.Process;
                    instanceIdStruct.Process = GlobalDefine.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();

                    void Reply(IActorResponse response)
                    {
                        Session replySession = NetInnerComponent.Instance.Get(fromProcess);

                        // 发回真实的actorId 做查问题使用
                        replySession.Send(realActorId, response);
                    }

                    InnerMessageDispatcherHelper.HandleIActorRequest(opcode, realActorId, iActorRequest, Reply);
                    return;
                }

                case IActorResponse iActorResponse:
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = GlobalDefine.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();
                    InnerMessageDispatcherHelper.HandleIActorResponse(opcode, realActorId, iActorResponse);
                    return;
                }

                case IActorMessage iactorMessage:
                {
                    InstanceIdStruct instanceIdStruct = new InstanceIdStruct(actorId);
                    instanceIdStruct.Process = GlobalDefine.Options.Process;
                    long realActorId = instanceIdStruct.ToLong();
                    InnerMessageDispatcherHelper.HandleIActorMessage(opcode, realActorId, iactorMessage);
                    return;
                }

                default:
                {
                    MessageDispatcherComponent.Instance.Handle(session, opcode, message);
                    break;
                }
                }
            }
            catch (Exception e)
            {
                Log.Error($"InnerMessageDispatcher error: {opcode}\n{e}");
            }
        }
Exemple #9
0
        public static long GenerateInstanceId()
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(process, ++MaxConfigSceneId);

            return(instanceIdStruct.ToLong());
        }
Exemple #10
0
        // Scene的InstanceId跟Id一样
        public static long GenerateProcessSceneId()
        {
            InstanceIdStruct instanceIdStruct = new InstanceIdStruct(process, 0);

            return(instanceIdStruct.ToLong());
        }