예제 #1
0
 //Listener
 public static async Task StartServerSocketAsync()
 {
     _cancellationServerToken = CancellationTokenSource.CreateLinkedTokenSource(new CancellationToken());
     while (!_cancellationServerToken.Token.IsCancellationRequested)
     {
         await Task.Run(async() =>
         {
             TcpClient client = await GameSocket.AcceptTcpClientAsync();
             client.NoDelay   = true;
             ProtocolModel.ConnectionClient connectionClient = new ProtocolModel.ConnectionClient
             {
                 Id     = Guid.NewGuid().ToString(),
                 Client = client
             };
             Clients.Add(connectionClient);
             ReadFromClient(connectionClient);
         }, _cancellationServerToken.Token);
     }
 }
예제 #2
0
        //Listener Process From Client Data
        private static async void ReadFromClient(ProtocolModel.ConnectionClient connectionClient)
        {
            try
            {
                await Task.Run(async() =>
                {
                    while (connectionClient.Client.Connected)
                    {
                        if (connectionClient.Client.GetStream().DataAvailable)
                        {
                            NetworkStream stream = connectionClient.Client.GetStream();
                            byte[] data          = new byte[1024];
                            string json          = string.Empty;
                            var bytes            = stream.Read(data, 0, data.Length);
                            if (bytes > 0)
                            {
                                json = Encoding.ASCII.GetString(data, 0, bytes);
                            }
                            //Data Processing
                            var token = json.Split(new[] { "\\;omansak;\\" }, StringSplitOptions.RemoveEmptyEntries);
                            foreach (var msgToken in token)
                            {
                                ProtocolModel.Base baseMessage = JsonConvert.DeserializeObject <ProtocolModel.Base>(msgToken);
                                switch (baseMessage.Type)
                                {
                                // Join new player and send current players
                                case ProtocolModel.MessageType.RequestJoin:
                                    {
                                        ProtocolModel.Player player = JsonConvert.DeserializeObject <ProtocolModel.Player>(baseMessage.Data.ToString());
                                        player.Id = connectionClient.Id;
                                        Players.Add(player);
                                        BroadcastPlayers();
                                        RequestSendMessage($"--> {Players.First(i => i.Id == connectionClient.Id).Name} is joined.");
                                        break;
                                    }

                                // Lobby Messages
                                case ProtocolModel.MessageType.LobbyMessage:
                                    {
                                        BroadcastFromServer(json);
                                        break;
                                    }

                                // Start Game
                                case ProtocolModel.MessageType.RequestStart:
                                    {
                                        GameRunning = true;
                                        GenerateShapes();
                                        RequestSendMessage($"--> Game started by {Players.First(i => i.Id == connectionClient.Id).Name} (host)");
                                        break;
                                    }

                                // Game Settings
                                case ProtocolModel.MessageType.RequestGameSettings:
                                    {
                                        BroadcastFromServer(JsonConvert.SerializeObject(new ProtocolModel.Base
                                        {
                                            Type = ProtocolModel.MessageType.RequestGameSettings,
                                            Data = new ProtocolModel.GameSetting
                                            {
                                                GameName  = GameName,
                                                Red       = GameShapeRed.ToString(),
                                                Blue      = GameShapeBlue.ToString(),
                                                Yellow    = GameShapeYellow.ToString(),
                                                Timer     = GameRefreshTime.ToString(),
                                                Win       = GameWinPoint.ToString(),
                                                PanelSize = new Point(GamePanelSizeX, GamePanelSizeY)
                                            }
                                        }));
                                        break;
                                    }

                                // Check coor inside objects
                                case ProtocolModel.MessageType.RequestSendCoor:
                                    {
                                        ProtocolModel.ClickCoor point = JsonConvert.DeserializeObject <ProtocolModel.ClickCoor>(baseMessage.Data.ToString());
                                        for (int j = ShapesPanel.Count - 1; j >= 0; j--)
                                        {
                                            var item = ShapesPanel[j];
                                            if (item.Type == 0)
                                            {
                                                if (ContainsEllipse(point.Coor, item.Point))
                                                {
                                                    ShapesPanel.Remove(item);
                                                    Players.First(i => i.Id == connectionClient.Id).Point += GameShapeRed;
                                                    BroadcastShapes();
                                                    BroadcastPlayers();
                                                    RequestSendMessage($"--> {Players.First(i => i.Id == connectionClient.Id).Name} is gained {GameShapeRed} with Red");
                                                    break;
                                                }
                                            }
                                            if (item.Type == 1)
                                            {
                                                if (ContainsRectangle(point.Coor, item.Point))
                                                {
                                                    ShapesPanel.Remove(item);
                                                    Players.First(i => i.Id == connectionClient.Id).Point += GameShapeBlue;
                                                    BroadcastShapes();
                                                    BroadcastPlayers();
                                                    RequestSendMessage($"--> {Players.First(i => i.Id == connectionClient.Id).Name} is gained {GameShapeBlue} with Blue");
                                                    break;
                                                }
                                            }
                                            if (item.Type == 2)
                                            {
                                                if (ContainsTriangle(point.Coor, item.Point))
                                                {
                                                    ShapesPanel.Remove(item);
                                                    Players.First(i => i.Id == connectionClient.Id).Point += GameShapeYellow;
                                                    BroadcastShapes();
                                                    BroadcastPlayers();
                                                    RequestSendMessage($"--> {Players.First(i => i.Id == connectionClient.Id).Name} is gained {GameShapeYellow} with Yellow");
                                                    break;
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        Thread.Sleep(25);
                    }
                });
            }
            finally
            {
                Players.Remove(Players.First(i => i.Id == connectionClient.Id));
                connectionClient.Client.Close();
                BroadcastPlayers();
            }
        }