示例#1
0
        void ReceiveThread()
        {
            while (IsActive)
            {
                try
                {
                    IPEndPoint ip   = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     data = Receive(ref ip);

                    if (data.Length > 0)
                    {
                        Session c = mService.GetSession(ip);

                        if (MessageBuffer.IsValid(data))
                        {
                            var buffer = new MessageBuffer(data);
                            if (c == null || c.id != buffer.extra())
                            {
                                c = mService.GetSession(buffer.extra());
                            }

                            if (c != null)
                            {
                                if (c.udpAdress == null || c.udpAdress.Equals(ip) == false)
                                {
                                    c.udpAdress = ip;
                                }

                                if (onReceive != null)
                                {
                                    onReceive(new MessageInfo(buffer, c));
                                }
                            }
                        }
                    }

                    Thread.Sleep(1);
                }
                catch (SocketException e)
                {
                    mService.Debug(e.Message);
                    continue;
                }
                catch (Exception e)
                {
                    mService.CatchException(e);
                    throw e;
                }
            }
        }
示例#2
0
        void ReceiveThread()
        {
            while (IsActive)
            {
                try
                {
                    IPEndPoint ip   = new IPEndPoint(IPAddress.Any, 0);
                    byte[]     data = Receive(ref ip);

                    Session c = mService.GetSession(ip);

                    if (c == null)
                    {
                        var sessions = mService.sessions;

                        for (int i = 0; i < sessions.Count; ++i)
                        {
                            sessions[i].OnReceiveKcp(data, ip);
                        }
                    }
                    else
                    {
                        c.OnReceiveKcp(data, ip);
                    }

                    Thread.Sleep(1);
                }
                catch (SocketException e)
                {
                    //mService.Debug(e.Message);
                    continue;
                }
                catch (Exception e)
                {
                    mService.CatchException(e);
                    throw e;
                }
            }
        }