示例#1
0
        private void StartThreads()
        {
            #region Initialize Collections
            _acceptedClients   = !NeutronModule.IsUnityThread ? (INeutronConsumer <TcpClient>) new NeutronBlockingQueue <TcpClient>() : (INeutronConsumer <TcpClient>) new NeutronSafeQueueNonAlloc <TcpClient>();
            _dataForProcessing = !NeutronModule.IsUnityThread ? (INeutronConsumer <NeutronPacket>) new NeutronBlockingQueue <NeutronPacket>() : (INeutronConsumer <NeutronPacket>) new NeutronSafeQueueNonAlloc <NeutronPacket>();
            #endregion

            Player = PlayerHelper.MakeTheServerPlayer(); //* Create the ref player.

            Instance = new Neutron(Player, true);        //* Create the ref instance.
            Instance.Initialize(Instance);               //* Initialize the instance.

            #region Provider
            for (int i = 0; i < NeutronModule.Settings.GlobalSettings.MaxPlayers; i++)
            {
                _pooledIds.Push((NeutronConstants.GENERATE_PLAYER_ID + i) + 1); //* Add all player ids to the list.
            }
            #endregion

            Initialized = true; //* Mark the server as initialized.

            #region Logger
#if NET_STANDARD_2_1
            //*
#endif
            LogHelper.Info("The server is ready, all protocols(TCP, UDP, RUDP) have been initialized.\r\n");
            LogHelper.Info($"Server address: {SocketHelper.GetLocalIPAddress()}\r\n");
            #endregion

            #region Threads
            CancellationToken token = TokenSource.Token;
            if (!NeutronModule.IsUnityThread)
            {
                Thread acptTh = new Thread(async(t) =>
                {
                    while (!token.IsCancellationRequested)
                    {
                        TcpClient tcpClient = await TcpListener.AcceptTcpClientAsync(); //* wait for new client....
                        if (tcpClient != null)
                        {
                            _acceptedClients.Push(tcpClient); //* Add the client to the queue.
                        }
                        else
                        {
                            LogHelper.Error("Client is null!");
                        }
                    }
                })
                {
                    Priority     = System.Threading.ThreadPriority.Lowest,
                    IsBackground = true,
                    Name         = "Neutron acptTh"
                };
                acptTh.Start();
            }
            else
            {
                StartCoroutine(OnAcceptedClientCouroutine(token));
            }

            if (!NeutronModule.IsUnityThread)
            {
                Thread packetProcessingStackTh = new Thread((e) =>
                {
                    while (!token.IsCancellationRequested)
                    {
                        PacketProcessingStack();
                    }
                })
                {
                    Priority     = System.Threading.ThreadPriority.Normal,
                    IsBackground = true,
                    Name         = "Neutron packetProcessingStackTh"
                };
                packetProcessingStackTh.Start();
            }

            if (!NeutronModule.IsUnityThread)
            {
                Thread clientsProcessingStackTh = new Thread((e) =>
                {
                    while (!token.IsCancellationRequested)
                    {
                        ClientsProcessingStack();
                    }
                })
                {
                    Priority     = System.Threading.ThreadPriority.Lowest,
                    IsBackground = true,
                    Name         = "Neutron ClientsProcessingStackTh"
                };
                clientsProcessingStackTh.Start();
            }

            if (NeutronModule.IsUnityThread)
            {
                StartCoroutine(OnReceivingDataCoroutine(token, Protocol.Tcp));
                StartCoroutine(OnReceivingDataCoroutine(token, Protocol.Udp));
            }
            #endregion

            #region Events
            OnStart?.Invoke();
            #endregion
        }