Пример #1
0
        private void createPlayer()
        {
            string player;

            ServerEndPoint = new IPEndPoint(ServerIP, PortIP);

            Client = new TcpClient();

            Client.Connect(ServerEndPoint);

            if (Client.Connected)
            {
                ClientNS = Client.GetStream();

                byte[] BufferLocal = new byte[1024];
                int    BytesRebuts = ClientNS.Read(BufferLocal, 0, BufferLocal.Length);
                //Passem de bytes a string
                player = Encoding.UTF8.GetString(BufferLocal, 0, BytesRebuts);

                Player newPlayerFromSplit = SerializatePlayer.Deserialization(player);

                indexPlayer = newPlayerFromSplit.id;
                dicPlayers.Add(indexPlayer, newPlayerFromSplit);
                CreateBall(newPlayerFromSplit);

                Thread clientConnexion = new Thread(serverLisener);
                clientConnexion.SetApartmentState(ApartmentState.STA);
                clientConnexion.Start(ClientNS);

                Thread sendMovementPlayer = new Thread(ResponseToServer);
                sendMovementPlayer.Start();
            }
        }
Пример #2
0
        private void receivedNewPlayer(object currentClientNSObject)
        {
            string        player = "";
            int           bytesFromBuffer;
            bool          close           = false;
            NetworkStream currentClientNS = (NetworkStream)currentClientNSObject;

            do
            {
                if (currentClientNS.CanRead)
                {
                    do
                    {
                        try
                        {
                            byte[] receivedBuffer = new byte[1024];
                            bytesFromBuffer = currentClientNS.Read(receivedBuffer, 0, receivedBuffer.Length);

                            player = Encoding.UTF8.GetString(receivedBuffer, 0, bytesFromBuffer);
                        }
                        catch (System.IO.IOException ex)
                        {
                            // if the ReceiveTimeout is reached an IOException will be raised...
                            // with an InnerException of type SocketException and ErrorCode 10060
                            var socketExept = ex.InnerException as SocketException;
                            if (socketExept == null || socketExept.ErrorCode != 10060)
                            {
                                // if it is the receive timeout, then reading ended
                                bytesFromBuffer = 0;
                            }
                        }
                    } while (currentClientNS.DataAvailable);
                }
                if (player != "")
                {
                    try
                    {
                        Player newPlayerFromSplit = SerializatePlayer.Deserialization(player);

                        dicPlayers.Add(newPlayerFromSplit.id, newPlayerFromSplit);

                        Dispatcher.Invoke(() =>
                        {
                            CreateBall(newPlayerFromSplit);
                        });
                    }
                    catch (Exception e)
                    {
                    }
                }
            } while (!close);
        }
Пример #3
0
        // Envia un objecte Position serialitzat cap al servidor
        void ResponseToServer()
        {
            while (true)
            {
                Position position = new Position(dicPlayers[indexPlayer].position.PosX, dicPlayers[indexPlayer].position.PosY, dicPlayers[indexPlayer].position.Width, dicPlayers[indexPlayer].position.Height);
                if (position.PosX != dicPlayers[indexPlayer].position.PosX || position.PosY != dicPlayers[indexPlayer].position.PosY)
                {
                    var    stringPlayer     = SerializatePlayer.Serializate(dicPlayers[indexPlayer]);
                    byte[] playerSerialized = Encoding.UTF8.GetBytes(stringPlayer);

                    ClientNS.Write(playerSerialized, 0, stringPlayer.Length);
                }
            }
        }
Пример #4
0
        private void clientListener(NetworkStream currentClientNS)
        {
            bool closed = false;

            // Fem un bucle que actua com "listener" on li afegim un try catch per controlar IOException quan l'usuari tanqui l'aplicació
            while (!closed)
            {
                try
                {
                    byte[] localBuffer     = new byte[1024];
                    int    bytesFromBuffer = currentClientNS.Read(localBuffer, 0, localBuffer.Length);

                    var player = Encoding.UTF8.GetString(localBuffer, 0, bytesFromBuffer);
                    try
                    {
                        Player newPlayerFromSplit = SerializatePlayer.Deserialization(player);
                        if (dicPlayers.ContainsKey(newPlayerFromSplit.id))
                        {
                            dicPlayers[newPlayerFromSplit.id].position.PosX = newPlayerFromSplit.position.PosX;
                            dicPlayers[newPlayerFromSplit.id].position.PosY = newPlayerFromSplit.position.PosY;
                            if (newPlayerFromSplit.position.Width != dicPlayers[newPlayerFromSplit.id].position.Width)
                            {
                                this.Dispatcher.Invoke(() =>
                                {
                                    dicPlayers[newPlayerFromSplit.id].position.Width  = newPlayerFromSplit.position.Width;
                                    dicPlayers[newPlayerFromSplit.id].position.Height = newPlayerFromSplit.position.Height;
                                    CreateBall(newPlayerFromSplit);
                                });
                            }

                            this.Dispatcher.Invoke(() =>
                            {
                                DrawBall(dicPlayers[newPlayerFromSplit.id], dicBallgraphics[newPlayerFromSplit.id]);
                            });
                        }
                    }
                    catch (Exception e)
                    {
                        DrawObject(player);
                    }
                }
                catch (Exception e)
                {
                    closed = true;
                }
            }
        }