Esempio n. 1
0
        // connect
        public KChannel(uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Connect)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.LocalConn      = localConn;
            this.socket         = socket;
            this.remoteEndPoint = remoteEndPoint;
            this.lastRecvTime   = kService.TimeNow;
            this.CreateTime     = kService.TimeNow;
            this.Connect();
        }
Esempio n. 2
0
        public WChannel(WebSocket webSocket, AService service) : base(service, ChannelType.Connect)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.webSocket = webSocket;

            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);
            this.recvStream   = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            isConnected = false;
        }
Esempio n. 3
0
        public Task WaitTillAsync(long tillTime)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            return(tcs.Task);
        }
Esempio n. 4
0
 void GetEnemys(int count)
 {
     for (int i = 0; i < count; i++)
     {
         ///20190702
         Monster enemy = ComponentFactory.CreateWithId <Monster>(IdGenerater.GenerateId());
         enemy.AddComponent <NumericComponent>();
         enemys.Add(enemy.Id, enemy);
     }
     SetPosition(enemys);
 }
Esempio n. 5
0
        public ComponentQueue(string typeName)
        {
            this.Id       = IdGenerater.GenerateId();
            this.TypeName = typeName;
#if !SERVER
            this.ViewGO       = new GameObject();
            this.ViewGO.name  = this.GetType().Name;
            this.ViewGO.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
            this.ViewGO.transform.SetParent(Global.transform, false);
            this.ViewGO.AddComponent <ComponentView>().Component = this;
#endif
        }
Esempio n. 6
0
        public WChannel(HttpListenerWebSocketContext webSocketContext, AService service) : base(service, ChannelType.Accept)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.webSocketContext = webSocketContext;

            this.webSocket = webSocketContext.WebSocket;

            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            isConnected = true;
        }
Esempio n. 7
0
        // connect
        public KChannel(uint localConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Connect)
        {
            this.InstanceId   = IdGenerater.GenerateId();
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.LocalConn      = localConn;
            this.socket         = socket;
            this.remoteEndPoint = remoteEndPoint;
            this.lastRecvTime   = kService.TimeNow;
            this.createTime     = kService.TimeNow;
            this.Connect();
        }
Esempio n. 8
0
        public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            cancellationToken.Register(() => { this.Remove(timer.Id); });
            return(tcs.Task);
        }
Esempio n. 9
0
        protected Component()
        {
            this.InstanceId = IdGenerater.GenerateId();
#if !SERVER
            if (!this.GetType().IsDefined(typeof(HideInHierarchy), true))
            {
                this.GameObject       = new GameObject();
                this.GameObject.name  = this.GetType().Name;
                this.GameObject.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
                this.GameObject.AddComponent <ComponentView>().Component = this;
            }
#endif
        }
Esempio n. 10
0
        /// <summary>
        /// 即可做client也可做server
        /// </summary>
        public TService(IPEndPoint ipEndPoint, Action <AChannel> acceptCallback)
        {
            this.InstanceId      = IdGenerater.GenerateId();
            this.AcceptCallback += acceptCallback;

            this.acceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            this.innArgs.Completed += this.OnComplete;

            this.acceptor.Bind(ipEndPoint);
            this.acceptor.Listen(1000);

            this.AcceptAsync();
        }
Esempio n. 11
0
        public static Scene CreateScene(SceneType sceneType, string name, Scene parent = null, long id = 0)
        {
            Scene scene = (Scene)Game.ObjectPool.Fetch(typeof(Scene));

            scene.Id        = id != 0 ? id : IdGenerater.GenerateId();
            scene.Name      = name;
            scene.SceneType = sceneType;
            if (parent != null)
            {
                scene.Parent = parent;
            }
            scene.Domain = scene;
            return(scene);
        }
Esempio n. 12
0
        protected Entity()
        {
            this.InstanceId = IdGenerater.GenerateId();

#if !SERVER
            if (!this.GetType().IsDefined(typeof(HideInHierarchy), true))
            {
                this.ViewGO       = new GameObject();
                this.ViewGO.name  = this.GetType().Name;
                this.ViewGO.layer = LayerNames.GetLayerInt(LayerNames.HIDDEN);
                this.ViewGO.transform.SetParent(Global.transform, false);
                this.ViewGO.AddComponent <ComponentView>().Component = this;
            }
#endif
        }
        /// <summary>
        /// 等待指定(毫秒)
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public ETTask WaitForMilliSecondAsync(long time)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = TimeHelper.ClientNowMilliSeconds() + time, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            if (timer.Time < this.minTime)
            {
                this.minTime = timer.Time;
            }
            return(tcs.Task);
        }
Esempio n. 14
0
        public TChannel(IPEndPoint ipEndPoint, TService service) : base(service, ChannelType.Connect)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.socket             = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.socket.NoDelay     = true;
            this.parser             = new PacketParser(this.recvBuffer);
            this.innArgs.Completed += this.OnComplete;
            this.outArgs.Completed += this.OnComplete;

            this.RemoteAddress = ipEndPoint;

            this.isConnected = false;
            this.isSending   = false;
        }
Esempio n. 15
0
        public ETTask WaitAsync(long time)
        {
            ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            if (timer.Time < this.minTime)
            {
                this.minTime = timer.Time;
            }
            return(tcs.Task);
        }
Esempio n. 16
0
        public ETTask WaitTillAsync(long tillTime)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            if (timer.Time < this.minTime)
            {
                this.minTime = timer.Time;
            }
            return(tcs.Task);
        }
Esempio n. 17
0
        public TChannel(Socket socket, TService service) : base(service, ChannelType.Accept)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.socket             = socket;
            this.socket.NoDelay     = true;
            this.parser             = new PacketParser(this.recvBuffer);
            this.innArgs.Completed += this.OnComplete;
            this.outArgs.Completed += this.OnComplete;

            this.RemoteAddress = (IPEndPoint)socket.RemoteEndPoint;

            this.isConnected = true;
            this.isSending   = false;
        }
Esempio n. 18
0
        public ETTask WaitAsync(long time, CancellationToken cancellationToken)
        {
            ETTaskCompletionSource <bool> tcs = new ETTaskCompletionSource <bool>();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            if (timer.Time < this.minTime)
            {
                this.minTime = timer.Time;
            }
            cancellationToken.Register(() => { this.Remove(timer.Id); });
            return(tcs.Task);
        }
Esempio n. 19
0
        public TChannel(Socket socket, TService service) : base(service, ChannelType.Accept)
        {
            this.InstanceId   = IdGenerater.GenerateId();
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.socket             = socket;
            this.socket.NoDelay     = true;
            this.parser             = new PacketParser(this.recvBuffer, this.memoryStream);
            this.innArgs.Completed += this.OnComplete;
            this.outArgs.Completed += this.OnComplete;

            this.RemoteAddress = (IPEndPoint)socket.RemoteEndPoint;

            this.isConnected = true;
            this.isSending   = false;
        }
Esempio n. 20
0
        /// <summary>
        /// 初始化远程端Socket  (这里只是给了远程端的IP,所以并没有和远程端建立连接)
        /// </summary>
        /// <param name="ipEndPoint"></param>
        /// <param name="service"></param>
        public TChannel(IPEndPoint ipEndPoint, TService service) : base(service, ChannelType.Connect)
        {
            this.InstanceId   = IdGenerater.GenerateId();
            this.memoryStream = this.GetService().MemoryStreamManager.GetStream("message", ushort.MaxValue);

            this.socket             = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.socket.NoDelay     = true;            // Socket 使用 Nagle 算法
            this.parser             = new PacketParser(this.recvBuffer, this.memoryStream);
            this.innArgs.Completed += this.OnComplete; //接收到数据的回调
            this.outArgs.Completed += this.OnComplete; //接收到数据的回调

            this.RemoteAddress = ipEndPoint;           //得到远程端IP

            this.isConnected = false;
            this.isSending   = false;
        }
Esempio n. 21
0
        /// <summary>
        /// 设置一个定时器 (定时事件是直接给的时间,而不是多少秒后)
        /// </summary>
        /// <returns>The till async.</returns>
        /// <param name="tillTime">Till time.</param>
        /// <param name="cancellationToken">CancellationToken</param>
        public Task WaitTillAsync(long tillTime, CancellationToken cancellationToken)
        {
            TaskCompletionSource <bool> tcs = new TaskCompletionSource <bool>();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            if (timer.Time < this.minTime)
            {
                this.minTime = timer.Time;
            }
            //监视取消请求。
            cancellationToken.Register(() => { this.Remove(timer.Id); });
            return(tcs.Task);             //如果要返回tcs.Task 那么就需要tcs.SetResult(true) 设置了结果
        }
Esempio n. 22
0
        // accept
        public KChannel(uint conn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.Id             = conn;
            this.Conn           = conn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            kcp = new KCP(this.RemoteConn, this);
            kcp.SetOutput(this.Output);
            kcp.NoDelay(1, 10, 2, 1);              //fast
            kcp.SetMTU(470);
            kcp.WndSize(256, 256);
            this.isConnected = true;

            this.lastRecvTime = kService.TimeNow;
        }
Esempio n. 23
0
        //等待到设定的时间 tillTime
        public ETTask WaitTillAsync(long tillTime, CancellationToken cancellationToken)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = tillTime, tcs = tcs
            };

            this.timers[timer.Id] = timer;
            this.timeId.Add(timer.Time, timer.Id);
            //缓存最近时间点的Task
            if (timer.Time < this.minTime)
            {
                this.minTime = timer.Time;
            }
            //取消的事件 就是将存储的字典的相同key移除掉
            cancellationToken.Register(() => { this.Remove(timer.Id); });
            return(tcs.Task);
        }
Esempio n. 24
0
        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="ipEndPoint">本地IP</param>
        /// <param name="acceptCallback">用于处理接收到消息数据的方法</param>
        public TService(IPEndPoint ipEndPoint, Action <AChannel> acceptCallback)
        {
            this.InstanceId      = IdGenerater.GenerateId();
            this.AcceptCallback += acceptCallback;             //处理接收到消息的函数

            this.acceptor = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            // 端口复用。ReuseAddress选项设置为True将允许将套接字绑定到已在使用中的地址。

            /*端口复用真正的用处主要在于服务器编程:当服务器需要重启时,经常会碰到端口尚未完全关闭的情况,这时如果不设置端口复用,则无法完成绑定,因为端口还处于被别的套接口绑定的状态之中。
             * socket1.Bind(localEP);   socket2.Bind(localEP);
             * 这样Socket1和Socket2便绑定在同一个端口上了。
             * */
            this.acceptor.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            this.innArgs.Completed += this.OnComplete;

            this.acceptor.Bind(ipEndPoint); //绑定本地IP
            this.acceptor.Listen(1000);     //监听远程端数量

            this.AcceptAsync();             //开始监听
        }
Esempio n. 25
0
        void GetSkillItems()
        {
            TaskerItem item11101 = ComponentFactory.CreateWithId <TaskerItem>(IdGenerater.GenerateId());
            TaskerItem item21101 = ComponentFactory.CreateWithId <TaskerItem>(IdGenerater.GenerateId());
            TaskerItem item31101 = ComponentFactory.CreateWithId <TaskerItem>(IdGenerater.GenerateId());
            TaskerItem item41101 = ComponentFactory.CreateWithId <TaskerItem>(IdGenerater.GenerateId());

            //item11101.AddComponent<NumericComponent>().Set(NumericType.ManageAdd, 10);
            item11101.AddComponent <NumericComponent>().Set(NumericType.ValuationAdd, 10);
            item21101.AddComponent <NumericComponent>().Set(NumericType.ValuationAdd, 12);
            item21101.AddComponent <NumericComponent>().Set(NumericType.ValuationAdd, 12);
            //item31101.AddComponent<NumericComponent>().Set(NumericType.MeasureAdd, 10);
            item31101.AddComponent <NumericComponent>().Set(NumericType.ValuationAdd, 10);
            item41101.AddComponent <NumericComponent>().Set(NumericType.CaseAdd, 14);
            item41101.AddComponent <NumericComponent>().Set(NumericType.ValuationAdd, 14);

            idTaskerItems.Add(item11101.Id, item11101);
            idTaskerItems.Add(item21101.Id, item21101);
            idTaskerItems.Add(item31101.Id, item31101);
            idTaskerItems.Add(item41101.Id, item41101);
        }
Esempio n. 26
0
        public override void EndInit()
        {
            try
            {
                this.InstanceId = IdGenerater.GenerateId();

                this.componentDict.Clear();

                if (this.components != null)
                {
                    foreach (Component component in this.components)
                    {
                        component.Parent = this;
                        this.componentDict.Add(component.GetType(), component);
                    }
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Esempio n. 27
0
        //从现在起 等待N毫秒后
        public ETTask WaitAsync(long time)
        {
            ETTaskCompletionSource tcs = new ETTaskCompletionSource();
            //创建Timer 并且生成ID 设定触发时间=当前时间+等待的时间 缓存ETTaskCompletionSource对象tcs
            Timer timer = new Timer {
                Id = IdGenerater.GenerateId(), Time = TimeHelper.Now() + time, tcs = tcs
            };

            //管理所有timer对象的字典
            this.timers[timer.Id] = timer;
            //管理所有时间相关的事件 key:时间 value:timerid 通过timerid可以找到以上的Timer对象
            //然后获取它的tcs ...
            this.timeId.Add(timer.Time, timer.Id);
            //这个事件的时间跟当前最近要触发的时间进行比较 如果小于 那么最近要触发的事件是这个 timer.Time
            if (timer.Time < this.minTime)
            {
                //则进行更新最近要触发的time
                this.minTime = timer.Time;
            }
            //返回tcs.Task 而非tcs,所以等待的是其内部的Task对象
            return(tcs.Task);
        }
Esempio n. 28
0
        // accept
        public KChannel(uint localConn, uint remoteConn, Socket socket, IPEndPoint remoteEndPoint, KService kService) : base(kService, ChannelType.Accept)
        {
            this.InstanceId = IdGenerater.GenerateId();

            this.LocalConn      = localConn;
            this.RemoteConn     = remoteConn;
            this.remoteEndPoint = remoteEndPoint;
            this.socket         = socket;
            this.kcp            = Kcp.KcpCreate(this.RemoteConn, new IntPtr(this.LocalConn));
            Kcp.KcpSetoutput(
                this.kcp,
                (bytes, len, k, user) =>
            {
                KService.Output(bytes, len, user);
                return(len);
            }
                );
            Kcp.KcpNodelay(this.kcp, 1, 10, 1, 1);
            Kcp.KcpWndsize(this.kcp, 256, 256);
            Kcp.KcpSetmtu(this.kcp, 470);
            this.isConnected           = true;
            this.isRecvFirstKcpMessage = false;
            this.lastRecvTime          = kService.TimeNow;
        }
Esempio n. 29
0
 protected AChannel(AService service, ChannelType channelType)
 {
     this.Id          = IdGenerater.GenerateId();
     this.ChannelType = channelType;
     this.service     = service;
 }
Esempio n. 30
0
 protected Component()
 {
     this.InstanceId = IdGenerater.GenerateId();
     Game.EventSystem.Add(this);
 }