Пример #1
0
        public void Start()
        {
            string ip = ((IPEndPoint)Socket.RemoteEndPoint).Address.ToString();

            Console.WriteLine("Client connected from {0}", ip);

            var packet = new NetworkPacket();

            Vector3 leprechaunPosition = Server.Terrain.GetHeight(Config.PlayerPositions[PlayerId]);

            try
            {
                Send(new NetworkPacketWelcome(PlayerId, Config.PlayerColors[PlayerId], leprechaunPosition, Config.RainbowPositions[PlayerId]));

                Server.ItemsManager.SendAll(this);
                Server.TrapManager.AddPlayer(this);

                const float goldpotRadius = Config.GoldpotCylinderRadius * Config.GoldpotScale;
                const float goldpotHeight = Config.GoldpotCylinderHeight * Config.GoldpotScale;

                Rainbow = new BoundingCylinder(Config.RainbowCylinderRadius, Config.RainbowCylinderHeight, Config.RainbowPositions[PlayerId]);
                Goldpot = new BoundingCylinder(goldpotRadius, goldpotHeight, Config.GoldpotPosition);

                var buffer = new byte[Config.BufferSize];

                while (true)
                {
                    if (Socket.Receive(buffer, 1, SocketFlags.None) == 0)
                    {
                        return;
                    }

                    packet.Type = buffer[0];

                    if (Socket.Receive(buffer, 1, SocketFlags.None) == 0)
                    {
                        return;
                    }

                    packet.Size = buffer[0];

                    int received = 0;

                    while (received < packet.Size)
                    {
                        int count;

                        if ((count = Socket.Receive(packet.Data, received, packet.Size - received, SocketFlags.None)) == 0)
                        {
                            return;
                        }

                        received += count;
                    }

                    ProcessPacket(packet);

                    Server.Send(packet, this);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.WriteLine("Client disconnected from {0}", ip);

                Server.OnClientFinish(this);
            }
        }