コード例 #1
0
        private void ClientsProcessingStack()
        {
            CancellationToken token     = TokenSource.Token;
            TcpClient         tcpClient = _acceptedClients.Pull(); //* Take the client from the queue and block the thread if there is no client.

            if (tcpClient != null)
            {
                if (PlayerHelper.GetAvailableID(out int Id))
                {
                    //* Create the player.
                    if (SocketHelper.AllowConnection(tcpClient))
                    {
                        tcpClient.NoDelay           = Helper.GetSettings().GlobalSettings.NoDelay;    //* Set the no delay, fast send.
                        tcpClient.ReceiveBufferSize = Helper.GetConstants().Tcp.TcpReceiveBufferSize; //* Set the receive buffer size.
                        tcpClient.SendBufferSize    = Helper.GetConstants().Tcp.TcpSendBufferSize;    //* Set the send buffer size.
                        var player = new NeutronPlayer(Id, tcpClient, new CancellationTokenSource()); //* Create the player.
                        if (SocketHelper.AddPlayer(player))
                        {
                            Interlocked.Increment(ref _playerCount); //* Increment the player count.

                            #region View
                            NeutronSchedule.ScheduleTask(() =>
                            {
                                GameObject playerGlobalController = GameObject.Instantiate(PlayerGlobalController.gameObject); //* Create the player global controller.
                                playerGlobalController.hideFlags  = HideFlags.HideInHierarchy;                                 //* Hide the player global controller.
                                playerGlobalController.name       = $"Player Global Controller[{player.Id}]";                  //* Set the name of the player global controller.
                                foreach (Component component in playerGlobalController.GetComponents <Component>())
                                {
                                    Type type = component.GetType();
                                    if (type.BaseType != typeof(PlayerGlobalController) && type != typeof(Transform))
                                    {
                                        GameObject.Destroy(component); //* Destroy all components except the player global controller.
                                    }
                                    else
                                    {
                                        if (type.BaseType == typeof(PlayerGlobalController))
                                        {
                                            var controller    = (PlayerGlobalController)component;
                                            controller.Player = player; //* Set the player.
                                        }
                                    }
                                }
                                SceneHelper.MoveToContainer(playerGlobalController, "Server(Container)"); //* Move the player global controller to the container.
                            });
                            #endregion

                            IPEndPoint tcpRemote = (IPEndPoint)player.TcpClient.Client.RemoteEndPoint; //* Get the remote end point.
                            IPEndPoint tcpLocal  = (IPEndPoint)player.TcpClient.Client.LocalEndPoint;  //* Get the local end point.
                            IPEndPoint udpLocal  = (IPEndPoint)player.UdpClient.Client.LocalEndPoint;
                            //***************************************************************************************************************************************************************************************************************************************************************
                            LogHelper.Info($"\r\nIncoming Client -> Ip: [{tcpRemote.Address.ToString().Bold()}] & Port: [Tcp: {tcpRemote.Port.ToString().Bold()} | Udp: {tcpRemote.Port.ToString().Bold()} - {udpLocal.Port.ToString().Bold()}] -> Id: {Id}\r\n");
#if UNITY_EDITOR
                            if (Helper.GetSettings().ServerSettings.FiltersLog)
                            {
                                LogHelper.Info("\r\nFilters(Server->Client): ".Italic() + $"(tcp.SrcPort == {tcpLocal.Port}) or (udp.SrcPort == {udpLocal.Port})".Bold().Color("yellow") + "\r\n");
                                LogHelper.Info("\r\nFilters(Client->Server): ".Italic() + $"(tcp.SrcPort == {tcpRemote.Port}) or (udp.SrcPort == {tcpRemote.Port})".Bold().Color("yellow") + "\r\n");
                                LogHelper.Info("\r\nFilters(Client->Server->Client): ".Italic() + $"((tcp.SrcPort == {tcpRemote.Port}) or (udp.SrcPort == {tcpRemote.Port})) or ((tcp.SrcPort == {tcpLocal.Port}) or (udp.SrcPort == {udpLocal.Port}))".Bold().Color("yellow") + "\r\n");
                            }
                            //***************************************************************************************************************************************************************************************************************************************************************
                            filter_tcp_udp_client_server.Append($"\r\n{((_playerCount > 1) ? " or " : string.Empty)}((tcp.SrcPort == {tcpRemote.Port}) or (udp.SrcPort == {tcpRemote.Port})) or ((tcp.SrcPort == {tcpLocal.Port}) or (udp.SrcPort == {udpLocal.Port}))");
                            filter_udp_client_server.Append($"\r\n{((_playerCount > 1) ? " or " : string.Empty)}((udp.SrcPort == {tcpRemote.Port}) or (udp.SrcPort == {udpLocal.Port}))");
                            filter_tcp_client_server.Append($"\r\n{((_playerCount > 1) ? " or " : string.Empty)}((tcp.SrcPort == {tcpRemote.Port}) or (tcp.SrcPort == {tcpLocal.Port}))");
                            filter_tcp_client.Append($"\r\n{((_playerCount > 1) ? " or " : string.Empty)}(tcp.SrcPort == {tcpRemote.Port})");
                            filter_tcp_server.Append($"\r\n{((_playerCount > 1) ? " or " : string.Empty)}(tcp.SrcPort == {tcpLocal.Port})");
                            filter_udp_client.Append($"\r\n{((_playerCount > 1) ? " or " : string.Empty)}(udp.SrcPort == {tcpRemote.Port})");
                            filter_udp_server.Append($"\r\n{((_playerCount > 1) ? " or " : string.Empty)}(udp.SrcPort == {udpLocal.Port})");
#endif
                            if (!NeutronModule.IsUnityThread)
                            {
                                ThreadPool.QueueUserWorkItem((e) =>
                                {
                                    OnReceivingDataLoop(player.NetworkStream, player, Protocol.Tcp);
                                    OnReceivingDataLoop(player.NetworkStream, player, Protocol.Udp);
                                });
                            }
                        }
                        else
                        {
                            if (!LogHelper.Error("Failed to add Player!"))
                            {
                                player.Dispose();
                            }
                        }
                    }
                    else
                    {
                        if (!LogHelper.Error("Client not allowed!"))
                        {
                            tcpClient.Close();
                        }
                    }
                }
                else
                {
                    if (!LogHelper.Error("Max players reached!"))
                    {
                        tcpClient.Close();
                    }
                }
            }
        }