示例#1
0
        void ClientWorkThread()
        {
            try
            {
                while (true)
                {
                    //TODO:when timeout , try to send idle message
                    byte[] data = _udp.Receive(SessionId, TimeSpan.FromSeconds(40));
                    if (data == null)
                    {
                        if (!_reader._removed)
                        {
                            Console.WriteLine("UDPClientStream.ClientWorkThread.Timeout-40");
                        }

                        _reader.OnRemoved();

                        base.OnWorkerThreadExit();

                        return;
                    }

                    try
                    {
                        UDPPackageType pt = UDPMeta.GetPackageType(data);
                        ProcessUDPPackage(pt, data);
                    }
                    catch (Exception x)
                    {
                        TcpMapService.OnError(x);
                    }
                }
            }
            catch (Exception x)
            {
                TcpMapService.OnError(x);
            }
        }
示例#2
0
        void ListenerWorkThread()
        {
            while (this._workthread != null)
            {
                try
                {
                    //TODO: if not cached , and all stream closed , shutdown directly

                    IPEndPoint ep;
                    byte[]     data = _udp.Receive(TimeSpan.FromSeconds(90), out ep);
                    if (data == null)
                    {
                        Console.WriteLine("_udp.Receive return null for a long time...; Shutdown..." + this._udp.LocalEndPoint);
                        this.Close();
                        return;
                    }

                    UDPPackageType pt = UDPMeta.GetPackageType(data);
                    if (pt == UDPPackageType.Unknown)
                    {
                        //Console.WriteLine("_udp.Receive return Unknown;");
                        return;
                    }

                    long         sid = BitConverter.ToInt64(data, 8);
                    ServerStream ss;

                    //Console.WriteLine("_udp.Receive " + sid + ":" + pt);

                    if (pt == UDPPackageType.SessionConnect)
                    {
                        UDPConnectJson udpc = UDPConnectJson.Deserialize(Encoding.UTF8.GetString(data, 16, data.Length - 16));
                        lock (strmap)
                        {
                            if (!strmap.TryGetValue(sid, out ss))
                            {
                                ss          = new ServerStream(this, sid, ep, udpc);
                                strmap[sid] = ss;
                            }
                        }
                        if (ss.ConnectToken != udpc.token)
                        {
                            ss.ForceClose();
                            lock (strmap)
                            {
                                ss          = new ServerStream(this, sid, ep, udpc);
                                strmap[sid] = ss;
                            }
                        }
                        else
                        {
                            ss.Process(pt, data);
                        }
                    }
                    else
                    {
                        lock (strmap)
                        {
                            strmap.TryGetValue(sid, out ss);
                        }
                        if (ss != null)
                        {
                            ss.Process(pt, data);
                        }
                        else
                        {
                            _udp.SendToClient(ep, UDPMeta.CreateSessionError(sid, "NotFound"));
                        }
                    }
                }
                catch (ThreadAbortException)
                {
                    break;
                }
                catch (Exception x)
                {
                    TcpMapService.OnError(x);
                }
            }
        }