示例#1
0
        public UdpSocket(int port, IPEndPoint remote, bool subThread = true, PackType type = PackType.Total, int es = 262144)
        {
            endPoint = remote;
            //Links = new Linker[thread * 1024];
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, port);

            soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //new UdpClient(_port);//new IPEndPoint(IPAddress.Parse(ip),
            soc.Bind(ip);
            soc.ReceiveTimeout = 1000;

            if (type != PackType.None)
            {
                Packaging     = true;
                envelope      = new TcpEnvelope(es);
                envelope.type = type;
            }
            running = true;
            auto    = subThread;
            if (thread == null)
            {
                thread = new ThreadEx(Run);
                thread.Start();
            }
            queue = new QueueBuffer <SocData>();
        }
        public virtual void Dispose()
        {
            running = false;
            thread  = null;
#if UNITY_WSA
            soc.Dispose();
#else
            soc.Close();
#endif
        }
 public void ConnectServer(IPEndPoint remote, IPEndPoint bind = null)
 {
     if (thread != null)
     {
         return;
     }
     localBind = bind;
     close     = false;
     iep       = remote;
     if (thread == null)
     {
         thread = new ThreadEx(Run);
         thread.Start();
     }
 }
        public void Start()
        {
            if (_port == 0)
            {
                _port = FreePort.FindNextAvailableUDPPort(10000);
            }
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, _port);

            soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //new UdpClient(_port);//new IPEndPoint(IPAddress.Parse(ip),
            soc.Bind(ip);
            soc.ReceiveTimeout = 1000;
            running            = true;
            if (thread == null)
            {
                //创建消息接收线程
                thread = new ThreadEx(Run);
                thread.Start();
            }
        }
示例#5
0
        /// <summary>
        /// UdpServer构造
        /// </summary>
        /// <param name="port"></param>
        /// <param name="remote"></param>
        /// <param name="subThread"></param>
        public UdpServer(int port, int remote, bool subThread = true, PackType type = PackType.Total)
        {
            queue      = new Queue <SocData>();
            packType   = type;
            remotePort = remote;
            //udp服务器端口绑定
            IPEndPoint ip = new IPEndPoint(IPAddress.Any, port);

            soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); //new UdpClient(_port);//new IPEndPoint(IPAddress.Parse(ip),
            soc.Bind(ip);

            running = true;
            auto    = subThread;
            links   = new List <UdpLink>();
            if (thread == null)
            {
                //创建消息接收线程
                thread = new ThreadEx(Run);
                thread.Start();
            }
        }
示例#6
0
 void Run()
 {
     while (running)
     {
         var now = DateTime.Now.Ticks;
         try
         {
             Recive();
         }
         catch (Exception ex)
         {
             UnityEngine.Debug.Log(ex.StackTrace);
         }
         long t = DateTime.Now.Ticks;
         t -= now;
         t /= 10000;
         if (t < 10)
         {
             ThreadEx.Sleep(1);
         }
     }
 }
 public void Start()
 {
     if (server == null)
     {
         server = new ThreadEx(AcceptClient);
         server.Start();
     }
     if (threadTimer == null)
     {
         threadTimer         = new ThreadTimer();
         threadTimer.Interal = 1000;
         threadTimer.Tick    = (o, e) => {
             try
             {
                 Heartbeat();
             }
             catch (Exception ex)
             {
                 UnityEngine.Debug.Log(ex.StackTrace);
             }
         };
     }
 }
        void Run()
        {
            while (true)
            {
                if (close)
                {
                    if (client != null)
                    {
                        if (client.Connected)
                        {
                            client.Shutdown(SocketShutdown.Both);
                        }
#if UNITY_WSA
                        client.Dispose();
#else
                        client.Close();
#endif
                    }
                    break;
                }
                if (client != null)
                {
                    if (client.Connected)
                    {
                        Receive();
                        if (redic)
                        {
                            if (client.Connected)
                            {
                                client.Shutdown(SocketShutdown.Both);
                            }
#if UNITY_WSA
                            client.Dispose();
#else
                            client.Close();
#endif
                            Connect();
                        }
                    }
                    if (reConnect)
                    {
                        try
                        {
#if UNITY_WSA
                            client.Dispose();
#else
                            client.Close();
#endif
                        }
                        catch (Exception ex)
                        {
                        }
                        Connect();
                    }
                }
                else
                {
                    Connect();
                }
            }
            thread = null;
            client = null;
        }
示例#9
0
 public LinkThread(int size = 2048) : base(size)
 {
     running = true;
     thread  = new ThreadEx(Run);
     thread.Start();
 }