Example #1
0
        public void OnUpdate(ServerChannel channel, IChannelListener listener)
        {
            long current = TimeUtils.Get1970ToNowMilliseconds();

            if (current - m_RecvMills > HeartbaetConstants.Timeout_Interval_Mills)
            {
                if (listener != null)
                {
                    listener.OnRemoveChannel(channel);
                }
            }
        }
        public bool RecvReliablePackets(ServerChannel channel, List <Packet> packets, IChannelListener listener, ServerHeartbeatProcessing heartbeat)
        {
            while (true)
            {
                int size = channel.Recv(m_RecvBuffer, 0, m_RecvBuffer.Length);

                if (size <= 0)
                {
                    break;
                }

                if (size == 8)
                {
                    ByteReadMemory memory = new ByteReadMemory(m_RecvBuffer, 0, size);
                    uint           flag   = memory.ReadUInt();
                    uint           conv   = memory.ReadUInt();

                    if (conv == channel.Conv)
                    {
                        if (flag == KcpConstants.Flag_Connect)
                        {
                            channel.SetConnectedStatus(true);
                            if (listener != null)
                            {
                                listener.OnAddChannel(channel);
                            }
                            continue;
                        }

                        if (flag == KcpConstants.Flag_Disconnect)
                        {
                            channel.SetConnectedStatus(false);
                            if (listener != null)
                            {
                                listener.OnRemoveChannel(channel);
                            }
                            continue;
                        }

                        if (flag == KcpConstants.Flag_Heartbeat)
                        {
                            heartbeat.UpdateHeartbeat(channel, m_RecvBuffer, 0, size);
                            continue;
                        }
                    }
                }

                PacketProcessing.Recv(m_RecvBuffer, 0, size, packets);
            }

            return(packets.Count > 0);
        }
Example #3
0
        private void UpdateChannelLooper(object obj)
        {
            try
            {
                List <uint> removes = new List <uint>();
                while (!m_Dispose)
                {
                    long time = TimeUtils.Get1970ToNowMilliseconds();
                    using (IEnumerator <KeyValuePair <uint, ServerChannel> > its = m_Channels.GetEnumerator())
                    {
                        while (its.MoveNext())
                        {
                            ServerChannel channel = its.Current.Value;
                            if (channel.IsDispose)
                            {
                                removes.Add(channel.ChannelId);
                            }
                            else
                            {
                                channel.OnUpdate(time, m_Process, m_Listener);
                            }
                        }
                    }

                    int length = removes.Count;
                    if (length > 0)
                    {
                        for (int i = 0; i < length; ++i)
                        {
                            uint channelId = removes[i];
                            if (m_Channels.TryRemove(channelId, out ServerChannel channel))
                            {
                                if (m_Listener != null)
                                {
                                    m_Listener.OnRemoveChannel(channel);
                                }
                            }
                        }
                        removes.Clear();
                    }

                    Thread.Sleep(5);
                }
            }
            catch (Exception e)
            {
                Logger.Error(e.ToString());
            }
        }