Ejemplo n.º 1
0
        private void TCPClientMethod(int index)
        {
            Packet receivedPacket;

            //loop to allow continuous communication
            while ((receivedPacket = _clients[index].TCPRead()) != null)
            {
                try
                {
                    switch (receivedPacket.EPacketType)
                    {
                    case PacketType.EMPTY:
                        EmptyPacket emptyPacket = (EmptyPacket)receivedPacket;
                        break;

                    case PacketType.LOGIN:
                        LoginPacket loginPacket = (LoginPacket)receivedPacket;
                        _clients[index].clientData.posX        = loginPacket.SpriteX;
                        _clients[index].clientData.posY        = loginPacket.SpriteY;
                        _clients[index].clientData.playerIndex = (_clients.Count - 1);

                        //confirm connection to joining player
                        JoinConfirmationPacket joinConfirmationPacket = new JoinConfirmationPacket((_clients.Count - 1), GetAllPlayerXValues(), GetAllPlayerYValues());
                        _clients[index].TCPSend(joinConfirmationPacket);

                        PlayerJoinPacket playerJoinPacket = new PlayerJoinPacket(_clients.Count - 1, loginPacket.SpriteX, loginPacket.SpriteY);
                        Console.WriteLine(_clients.Count);
                        TCPSendPacketToAll(playerJoinPacket);
                        Console.WriteLine("PACKETS SENT");
                        break;

                    case PacketType.DISCONNECT:
                        DisconnectPacket disconnectRequestPacket = (DisconnectPacket)receivedPacket;
                        break;
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("[Error] " + e.Message + e.StackTrace);
                    break;
                }
            }

            try
            {
                _clients[index].Close();

                //Take client out of client ConcurrentBag
                _clients.TryRemove(index, out Client c);
            }
            catch (Exception e)
            {
                Console.WriteLine("[Error] " + e.Message + e.StackTrace);
            }
        }
Ejemplo n.º 2
0
        private void TCPProcessServerResponse()
        {
            Packet receivedPacket;

            while ((receivedPacket = TCPRead()) != null)
            {
                try
                {
                    switch (receivedPacket.EPacketType)
                    {
                    case PacketType.EMPTY:
                        EmptyPacket emptyPacket = (EmptyPacket)receivedPacket;
                        break;

                    case PacketType.PLAYERJOIN:
                        PlayerJoinPacket playerJoinPacket = (PlayerJoinPacket)receivedPacket;
                        if (_game.localServerIndex != playerJoinPacket.ExistingPlayers)
                        {
                            _game.InitialiseNewPlayer(playerJoinPacket.X, playerJoinPacket.Y);
                        }
                        break;

                    case PacketType.JOINCONFIRMATION:
                        JoinConfirmationPacket joinConfirmationPacket = (JoinConfirmationPacket)receivedPacket;
                        _game.localServerIndex = joinConfirmationPacket.ServerIndex;

                        if (joinConfirmationPacket.ServerIndex > 0)
                        {
                            _game.LoadExistingPlayers(joinConfirmationPacket.ServerIndex, joinConfirmationPacket.XValues, joinConfirmationPacket.YValues);
                        }

                        _game.InitialiseNewPlayer(0.0f, 0.0f);
                        break;

                    case PacketType.PLAYERPOSITION:
                        PlayerPositionPacket playerPositionPacket = (PlayerPositionPacket)receivedPacket;
                        _game.ChangePlayerPosition(playerPositionPacket.PlayerIndex, playerPositionPacket.X, playerPositionPacket.Y);
                        break;
                    }
                }
                catch (SocketException e)
                {
                    Debug.WriteLine("[Error] " + e.Message + e.StackTrace);
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.Priority = ThreadPriority.Highest;

            ServerInteractor serverInteractor = new ServerInteractor();

            serverInteractor.networkInteractor.server.Start(25566 /* port */);

            Dictionary <NetPeer, string> playerNames = new Dictionary <NetPeer, string>();

            Random playerIdMaker = new Random();

            serverInteractor.efficaxSimulation.entityContainer.entityRegistry.entityIdMap.AddEntity(new PlayerEntity(null, 1, "Gerry boi", new Simulation.Entity.EntityData(213123, new Simulation.Types.Vector2(0, 0), new Simulation.Types.Vector2(0, 0))));
            serverInteractor.efficaxSimulation.entityContainer.Tick(21);

            serverInteractor.networkInteractor.listener.ConnectionRequestEvent += request =>
            {
                NetPeer peer = request.Accept();

                playerNames.Add(peer, request.Data.GetString());
            };

            serverInteractor.networkInteractor.listener.PeerConnectedEvent += peer =>
            {
                foreach (KeyValuePair <(int, NetPeer), PositionData> playerPositionKVP in serverInteractor.playerPositions)
                {
                    PlayerJoinPacket previousPlayer = new PlayerJoinPacket(playerPositionKVP.Key.Item1, playerNames[playerPositionKVP.Key.Item2], playerPositionKVP.Value);
                    peer.Send(previousPlayer.ToPacket(), DeliveryMethod.ReliableOrdered);
                }

                int          playerId  = playerIdMaker.Next();
                PositionData playerPos = new PositionData(0, 0);
                serverInteractor.playerPositions.Add((playerId, peer), playerPos);
                PlayerJoinPacket playerJoinPacket = new PlayerJoinPacket(playerId, playerNames[peer], playerPos);
                serverInteractor.networkInteractor.BroadcastBut(peer.Id, playerJoinPacket.ToPacket(), DeliveryMethod.ReliableOrdered);
            };

            serverInteractor.networkInteractor.listener.NetworkReceiveEvent += (fromPeer, dataReader, deliveryMethod) =>
            {
                serverInteractor.packetRouter.Route(fromPeer, dataReader);
                dataReader.Recycle();
            };

            var lastTick = new Stopwatch();

            lastTick.Start();
            long timerTicks = 0;

            long nextTickId = 0;

            //Console.Title = $"[Efficax Server] Port: 12733";
            Console.Title = $"[Efficax Server] Port: 25566";

            while (!Console.KeyAvailable)
            {
                serverInteractor.networkInteractor.server.PollEvents();
                lastTick.Stop();
                timerTicks += lastTick.ElapsedTicks;
                lastTick.Restart();
                if (timerTicks >= TimePerTick)
                {
                    timerTicks -= TimePerTick;
                    Tick(nextTickId);
                    nextTickId++;
                }
                Thread.Sleep(1);
            }
            serverInteractor.networkInteractor.server.Stop();

            void Tick(long tickId)
            {
            }
        }